Take the 2-minute tour ×

I am looking for a way to automatically define some aliases inside my session on any server I ssh to. I can't put them in the .bashrc files on the server because the user accounts I log in with are shared by other people and besides there are dozens of them and maintaining a script on every machine would be painful. I know I could use expect to type the aliases automatically but I was just wondering if OpenSSH has anything built-in that could conceivably be used to achieve this?

share|improve this question
add comment

7 Answers

up vote 7 down vote accepted

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.

[ $# -eq 0 ] && { echo "syntax: $0 <host> [<ssh-option>...]" 1>&2 ; exit 1 ; }
host=$1 ; shift    # use any remaining args as ssh options
scp -q ~/.ssh-init.sh "$host:/var/tmp/$initfile"
ssh -t "$@" $host "bash --rcfile /var/tmp/$initfile"
share|improve this answer
I like this because it doesn't require any special configuration on the server, nice! –  Ramon Jul 6 '12 at 19:57
add comment

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
# other global stuff

sshd_config modifications:


.ssh/config modifications (client side):

Host ...

.bashrc modifications (client side):

alias ssh='IAMTHEGREATEST="forsure" ssh'

(untested, but should do)

share|improve this answer
How does that fulfill the objective of setting things up automatically on any server the OP logs into? –  womble Jul 6 '12 at 15:57
It does not. It solves the "the user accounts I log in with are shared by other people" part of the question –  krissi Jul 6 '12 at 16:28
This is almost exactly what I do, but with one difference - instead of accepting that environment variable from outside, I set it in the SSH key itself. This means that .ssh/authorized_keys contains lines starting with environment="IAMTHEGREATEST=forsure" ssh-dss ... –  Bron Gondwana Jul 6 '12 at 17:27
add comment

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.

share|improve this answer
ding ding ding - we have a winner! –  Dennis Williamson Jul 6 '12 at 19:06
Missed that one! While it looks appealing, this will still require setting up each server individually since its on the server-side right? –  Ramon Jul 6 '12 at 20:04
yup, server side, but you'd only have to scp one file over, once. There's not really an easy way to send a stream of commands to be processed immediately upon login. {Grin} Give me a bit though, maybe write something... {not today!} –  lornix Jul 6 '12 at 20:53
add comment

Aliases are defined and interpreted by the shell.

Unfortunately there's no way for ssh to pass them to the remote shell.

You could write an ssh wrapper on the client side that sets up a nice environment on the remote side, but it wouldn't be pretty.

share|improve this answer
add comment

If possible, I would store a small script that initialize/define everything you want on a server accessible from everywhere and execute it after login in each server.

share|improve this answer
add comment

One possible solution would be to put your script that sets everything up on a webserver that all the other servers can access.

Then each server should just need a small script to do something like:

wget http://myserver.com/myawesomescript.sh
chmod +x myawesomescript.sh

That way you only have to update it in one place.

share|improve this answer
add comment

Look at .ssh/config

Here is a example https://lookherefirst.wordpress.com/2007/12/17/a-simple-ssh-config-file/

share|improve this answer
Please provide more details about why you think this will help. –  Zoredache Jul 6 '12 at 15:30
add comment

Your Answer


