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?
7 Answers
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"
-
I like this because it doesn't require any special configuration on the server, nice!– RamonJul 6, 2012 at 19:57
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)
-
How does that fulfill the objective of setting things up automatically on any server the OP logs into?– womble ♦Jul 6, 2012 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– krissiJul 6, 2012 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 ... Jul 6, 2012 at 17:27
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.
-
-
Missed that one! While it looks appealing, this will still require setting up each server individually since its on the server-side right?– RamonJul 6, 2012 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!}– lornixJul 6, 2012 at 20:53
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.
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.
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
./myawesomescript.sh
That way you only have to update it in one place.
Look at .ssh/config
Here is a example https://lookherefirst.wordpress.com/2007/12/17/a-simple-ssh-config-file/
-
-
We really do prefer that answers contain content not pointers to content. Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.– user9517Jul 6, 2012 at 15:39