Is there a way to run a custom setup script on a shared ssh login, but only for me?

I share an ssh login with my colleagues. Although generally fine people, they have certain flaws – for example a preference for emacs-style bash editing, their own (incorrect) alias preferences, etc.

I end up typing set -o vi multiple times a day.

What’s the easiest way to have my preferred environment set up for me automatically on ssh login, while leaving the vanilla config for everyone else? Since the login user is always the same, I cant just put it in the .bashrc. Can I send some kind of info from the client that .bashrc can key off?

One more complexity is that I access this server via a VPN and my client IP will likely be that same as other clients.

Asked By: Mark W

||

If…

  • you have PermitUserEnvironment enabled in the sshd configuration (which it is not by default), and
  • you are using ssh keys to authenticate, rather than passwords

…then you can set an environment variable specific to your ssh key in the ~/.ssh/config file for the shared user, and use that as a conditional in your bash initialization script. That is, you would make the shared user .ssh/config file look something like this:

environment="REALUSER=markw" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACA...

And then in your .bashrc you would do something like:

if [[ $REALUSER = markw ]]; then
  set -o vi
fi

If you are using password authentication, then you can use the ssh client SetEnv option, except that this again requires changes to the server’s sshd configuration: in this case, you must list the matching environment variable in the AcceptEnv option. See the ssh_config and sshd_config man pages for more detail.

Answered By: larsks
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.