Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

5 Answers 5

up vote 2 down vote accepted

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.

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

share|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 at 7:51
    
I'm working on fixing all of them! –  singular Sep 12 at 18:02
    
Done. First time I had been tempted to do a short link-only post. Now I know better. –  singular Sep 12 at 18:07

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.

share|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

like

/home/foobar/configs/.bashrc
/home/foobar/configs/.foo/bar.conf
...

and then a

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

That saves time.

share|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.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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