Auto propagation of bash settings across many machines?

8

3

I need to console to many machines through SSH. Including Linux, Mac, mobile devices, virtual machines. Sometimes I need to login to more than 10 machines in one day.

I enjoy my personal .bashrc or .bash_profile settings, like prompt (PS1), history control, aliases and other useful settings. But keeping these settings across many machines is nothing but pain.

So if anyone has idea how to automate this? Best would be to 'carry' the settings with me from my 'motherworld-PC'.

exebook

Posted 2013-04-26T03:41:51.687

Reputation: 270

Check out base on GitHub. It addresses this issue comprehensively. More details in this answer.

– codeforester – 2019-07-13T12:54:26.817

Answers

7

You can use the approach described at https://serverfault.com/a/400782

You will have to use PermitLocalCommand and LocalCommand to accomplish that.

I think that solution is Ok, but I didn't like it since it will replace the .bashrc of the remote machine by scping my local .bashrc to the remote .bashrc. Also I couldn't make it work exactly as I expected.

Another solution is to use git to manage all your dot files, like described in

http://blog.smalleycreative.com/tutorials/using-git-and-github-to-manage-your-dotfiles/

But a great problem for me is that I would have to setup each server with my dot files. Even If I had the time (and patience) to deploy my dot files to each server, I would still have one problem.

Imagining a context where more than one user login to the same server using the same login, it would be unfair to change the server defaults to my defaults.

Then...


So, I developed my own solution for that, hope you like it and find it useful:

You will need to:

  • Create a (private) gist at https://gist.github.com/ of your .bashrc
    • You will need a login at github to create a gist
    • Pay attention to sensitive informations like passwords in .bashrc enviroment variables. Anyway that kind of info should'nt be there in the first place.
    • Take not for the name you gave for the gist file. I will call this GISTNAME. Choose a name that is unlikely to find in the remote dir. Don't name it ".bashrc" please. =) A good name would be mycustombashrc.sh
    • After saving the gist, get the raw version link of it by clicking on the <> button. You will need this link, the raw gist link or RAWGISTLINK

Then in your local .bashrc put the following at the end:

function customssh(){
  ssh $@ -t 'cd $HOME ; 
  wget --quiet -N "RAWGISTLINK" ;
  cp GISTNAME .'$USER'bashrc  ; 
  bash --rcfile $HOME/.'$USER'bashrc'

}

Remember to replace GISTNAME and RAWGISTLINK

Now run

source ~/.bashrc

And then just use customssh (you'll still have the regular ssh)

customssh mylogin@remoteserver

Just try one of your aliases now!

Notes

  • Yes, this will probably download the gist file in every login, but believe me, it's REALLY fast. Don't worry.
  • To update the shared .bashrc
    • Go the the gist url, and make your changes (or copy your local .bashrc to it).
    • Then update customssh function replacing RAWGISTLINK with the new RAWGISTLINK (the gist link and GISTNAME is the same, but raw gist link changes when you make changes to the file).
    • Now just run source ~/.bashrc. That's it! No need to change anything in your remote servers!
  • My solution is entirely Ubuntu based, maybe it'll need some adjustments to make it work with other distros
  • You probably can use other services like gist instead of github's gist. Pastebin is one example, but there are others. It's entirely possible but I haven't tried it yet.
  • Also you could wget your custombashrc.sh from other places. A public webserver for example.
  • Another option, if your local machine is visible in the internet through ssh, you could just scp the file from local server (using the public address) to the remote server. I didn't use this solution because it's hard to assume that your local machine won't be hidden behind a router or firewall (In my case, I'm behind a router)
  • Maybe you can find issues across different versions of ubuntu, but I couldn't come across them yet.

The best thing about this approach is that it changes nothing in the remote servers and with little effort in the local machine and with gist you get all the fun of your local .bashrc in any server that you want.

And you still can easily "disable" this by using ssh instead of customssh.

Onilton Maciel

Posted 2013-04-26T03:41:51.687

Reputation: 316

3

One common approach to this is putting your dotfiles into github or another version control system. Not only can you easily update them, but you keep a history of the changes you've made for when you need to revert something.

Quick google searches for "dotfiles in github" and "dotfiles in version control" gave me a couple pages that look useful:

After setting this up, you could automate the updates with a cronjob on each server or simply adding a "git pull" in your .bash_profile.

Doug Harris

Posted 2013-04-26T03:41:51.687

Reputation: 23 578

1

I recently came across this question and thought I'd add my answer on how I pushed my .bashrc file around to multiple hosts.

In this hypothetical example there are 18 "vcn" servers that needed my .bashrc file. If you're fortunate enough to have a standardized naming convention, you can take advantage of curly braces in bash and use a loop to iterate server names appended in a numerical fashion. Then just scp the local .bashrc file to them.

for vcn in r1-vcn{1..9} r2-vcn{1..9}; do scp ~/.bashrc $vcn:/home/user/; done;

jrsdav

Posted 2013-04-26T03:41:51.687

Reputation: 11

1

You can use your bashrc without having to download it or modify anything on the server your're on if you do the following. The file gets curled into source, meaning it only exists in memory and only for that session.

source <(curl -s -A '' https://somedomain.com/bashrc);

Xenobius Bishop

Posted 2013-04-26T03:41:51.687

Reputation: 11

1

Something like rsync would probably fit your needs. If that's not an option, use Dropbox to sync the files across each device and create a symlink between the locally-synced file and where you would otherwise need it.

int_541

Posted 2013-04-26T03:41:51.687

Reputation: 200

1

You may find this tool be useful, althought github is probably the way to go if these are all your machines and not shared machines:

sshrc works just like ssh, but it also sources ~/.sshrc after logging in remotely.

$ echo "echo welcome" > ~/.sshrc
$ sshrc me@myserver
welcome

$ echo "alias ..='cd ..'" > ~/.sshrc
$ sshrc me@myserver
$ type ..
.. is aliased to `cd ..'

You can use this to set environment variables, define functions, and run post-login commands. It's that simple, and it won't impact other users on the server - even if they use sshrc too. For more advanced configuration, continue reading. https://github.com/Russell91/sshrc

RussellStewart

Posted 2013-04-26T03:41:51.687

Reputation: 201

0

The pain you are facing is quite common. I have the same problem and came up with this framework called Base to solve the issue. This framework not only solves the basic issue of keeping the Bash settings in sync across multiple machines, it also includes a simple but powerful method to share settings, scripts, and libraries among team members as well as across multiple teams or at the company level if you are working for one.

Check it out here: https://github.com/codeforester/base

codeforester

Posted 2013-04-26T03:41:51.687

Reputation: 150