SSH equivalent of .profile/.bashrc

There's nothing wrong with doing it in expect. The other way that I've done it is pretty dirty too, with a script, first scp the profile you want to run, then ssh in, run it and stay connected.

So, place all the profile settings in a local file .<local username>-<hostname>-init.sh, and run the script below to log in to the remote host.

#!/bin/sh
[ $# -eq 0 ] && { echo "syntax: $0 <host> [<ssh-option>...]" 1>&2 ; exit 1 ; }
host=$1 ; shift    # use any remaining args as ssh options
initfile=".$USER-`hostname`-init.sh"
scp -q ~/.ssh-init.sh "$host:/var/tmp/$initfile"
ssh -t "$@" $host "bash --rcfile /var/tmp/$initfile"

You could put your configuration in .bashrc and only let them execute when you log in. To make this possible you could pass an environment variable through ssh.

.bashrc modifications

# common stuff
if [ -n "$IAMTHEGREATEST" ]; then
  # my personal cool stuff
fi;
# other global stuff

sshd_config modifications:

AcceptEnv ... IAMTHEGREATEST

.ssh/config modifications (client side):

Host ...
  SendEnv IAMTHEGREATEST

.bashrc modifications (client side):

alias ssh='IAMTHEGREATEST="forsure" ssh'

(untested, but should do)


From the ssh(1) man page, if you create a file ~/.ssh/rc, it will be executed BEFORE the user's login shell is executed, thereby giving the chance to do 'pre-setup' tasks before logging in... the example given was to mount network shares before logging in.

If you don't want to use individual ~/.ssh/rc files, you can do the same with /etc/ssh/sshrc. A quick test to check for your particular username or some identifyiing method could restrict others from even noticing this was in place.

/etc/ssh/sshrc is ONLY sourced if ~/.ssh/rc does not exist, so you can effectively achieve two layers of complexity.