29

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.

gsgx
  • 392

6 Answers6

16

I think sshrc is what you're looking for: http://web.archive.org/web/20190325123227/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.

keen
  • 103
10

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.

1

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.

1

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:

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

For instance:

$> echo "alias q=exit" > ~/.config/kyrat/sshrc
$> kyrat myuser@myserver.com
myserver.com $> q
exit
Patronics
  • 103
0

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.

0

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.
Oliver
  • 6,076