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.

  • This is not a per-connection solution but you may still find it valuable. Check out base on GitHub. More details in this answer on Super User community. – codeforester Jul 13 '19 at 13:04

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.

| improve this answer | |
  • A couple of notes. First of all, I think you switched the order of the files in the scp command, since I want to transfer my bash to the server, and I think you have it reversed. Secondly, you can just add ; source /path/to/.bashrc after the scp command to have it sourced automatically. Finally, a not very important negative of this method is that I have to type my password twice. But in the end it worked, so thanks. – gsingh2011 Jun 21 '12 at 16:19
  • sorry about that! Was working out of my head, so sorry about the mixup. Are you able to setup passwordless key setups in your environment? We have that in our environment and it is a beautiful thing. Glad that worked though! – Quinn Murphy Jun 21 '12 at 16:31
  • @gsingh2011: That source /path/to/.bashrc will be executed on the machine you are connecting from. Commands in LocalCommand have no access to the ssh session. – Oliver Jun 22 '12 at 10:45
  • @Oliver You're right. I've settled for automating the sourcing of the .bashrc from the remote computer. I have to change a few things on the server side on the first login, but it's much more convenient than what I had to do before. – gsingh2011 Jun 22 '12 at 12:27

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

$ 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.

| improve this answer | |
  • Please stop spamming with link-only posts. You are perfectly able to add comments (which your sshrc-related posts are, anyway). – Deer Hunter Sep 12 '14 at 7:51
  • 3
    I'm working on fixing all of them! – RussellStewart Sep 12 '14 at 18:02
  • Done. First time I had been tempted to do a short link-only post. Now I know better. – RussellStewart Sep 12 '14 at 18:07
  • 1
    I wrote long time ago (before sshrc) a simil sshrc: github.com/fsquillace/pearl-ssh It is just more efficient, small and with no xxd dependencies. – user967489 Jun 6 '15 at 17:24

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.

| improve this answer | |

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



and then a

scp -r /home/foobar/configs/* foo@example.com:/home/foo/

That saves time.

| improve this answer | |

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:

  1. Set up your remote home directory when you first log in.
  2. Use NFS to mount your home directory containing your .bashrc on every server. That way, you have the same settings everywhere.
  3. If you are OK with just some environment variables transmitted to the remote servers, you should check out the SendEnv option (see the ssh_config man page for more information). If the server is correctly set up (i.e. it has the adequate AcceptEnv in sshd_config), you can use SendEnv to copy your environment variables over to the remote host.
  4. I don't know if that works, but maybe you can mis-use the ProxyCommand setting (see ssh_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.
| improve this answer | |

I think that https://github.com/fsquillace/pearl-ssh does what you need.

I wrote it long time ago before sshrc was born and it has more benefits compared to sshrc:

  • It does not require dependencies on xxd for both hosts (which can be unavailable on remote host)
  • Pearl-ssh uses a more efficient encoding algorithm
  • It is just ~20 lines of code (really easy to understand!)

For instance:

$> echo "alias q=exit" > ~/.config/pearl/sshrc
$> ssh_pearl myuser@myserver.com
myserver.com $> q
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.