How to use a custom .bashrc file on SSH login

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.


I think sshrc is what you're looking for: https://github.com/Russell91/sshrc

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.