How to Use a Custom .bashrc File on SSH Login

bashbashrcconfigurationssh

I've found that with the new company I'm working with I often have to access linux servers with relatively short lifetimes. On each of these servers I have an account, but whenever a new one is created, I have to go through the hassle of transferring over my .bashrc. It's possible however that in about a months time that server won't be around anymore. I also have to access many other servers for short periods of times (minutes) where it's just not worth it to transfer over my .bashrc but since I'm working on a lot of servers, this adds up to a lot of wasted time.

I don't want to change anything on the servers, but I was wondering if there was a way to have a "per-connection" .bashrc, so whenever I would SSH to a server my settings would be used for that session.

If this is possible, it would be nice if I could do the same thing with other configuration files, like gitconfig files.

Best Answer

I think what you want is (in your .ssh/config on the machine you connect from):

PermitLocalCommand yes
LocalCommand scp yourname@someserver:/dir/dotbash /local/home/dir/.bashrc

then you can lead out with:

source .bashrc

and be on your merry way. LocalCommand executes the command on the server you are connecting to when it gets there, right before your actual session.

I would also make sure sshds on the servers are configured with the PermitLocalCommand yes

There are a lot of ways you can tweak that LocalCommand to make it work in your specific environment -- you could curl from an internal web server, or pull from an nfs mount for example.