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.
I think sshrc is what you're looking for: https://github.com/Russell91/sshrc
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.
I think what you want is (in your .ssh/config on the machine you connect from):
then you can lead out with:
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.
If you've never hit the server before, there will be no entry in ~/.ssh/known_hosts for it.
You can search for a given known host with "ssh-keygen -F ", but you'll have to test that output (grep) as ssh-keygen doesn't return false for a miss. Note that if you refer to a host by different identifiers (IP address, hostname, FQDN), these each are treated as separate instances.
You could write a wrapper for ssh that transfers your user environment to that host on the first login:
ssh-newenv () { if ! ssh-keygen -F $1 | grep -q "^# host $1 found:"; then rsync ~/.bashrc ~/.bash_profile ~/.bash_logout $1:.; fi; ssh $1; }
If you want to make this more robust, you could check for the existence of a known environment file, hash, or other marker on the remote host.
I think that https://github.com/fsquillace/kyrat (formerly Pearl-ssh) does what you need.
I wrote it long time ago before sshrc was born and it has more benefits compared to sshrc:
For instance:
I don't know if there is a per session .bashrc .
Another solution would be to write a small script that transfers all your favourite configs to your new home folder.
Maybe just make folder with all your configs with paths and just transfer them with scp
like
and then a
That saves time.
I don't think that is possible, given that ssh has nothing to do with your
.bashrc
. It is the shell which loads that file, not ssh.Some ideas:
SendEnv
option (see thessh_config
man page for more information). If the server is correctly set up (i.e. it has the adequateAcceptEnv
in sshd_config), you can useSendEnv
to copy your environment variables over to the remote host.ProxyCommand
setting (seessh_config
man page) to send over your.bashrc
or other files prior to connecting to the remote host. That would require some testing, though. Also, be prepared that this might interfere with scp.