Auto propagation of bash settings across many machines?

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.


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:

  • Your unofficial guide to dotfiles on GitHub.
  • Using Git And Github To Manage Your Dotfiles

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.


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.


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