SSH Client Configuration
I have a VPS which is host to many websites. Some of those sites are permitted ssh access for their admins. However I am the admin to several sites myself. Each site has a different username (login/password) for administration. Additionally I have changed the ssh port to a different number (instead of the default 22
) to avoid some script/bot attacks.
All of this makes for very inconvenient ssh usage and plenty of typing errors. For example:
# ssh -p33333 username_site1@site1domain.com
# ssh -p33333 username_site2@site2domain.net
Fortunately ssh provides a client configuration file to make “shortcuts” for things like this.
If you start by reading the ssh_config
man page:
# man ssh_config
It will reveal 4 useful options:
Host
- A “shortcut” name which can be used instead of the full hostname address.Hostname
- The real host name which is the actual server to log into.Port
- Port number on the host server.User
- The username used to log in. Typically ssh will use the current unix username if not specified.
So using the above example. I created the the file: ~/.ssh/config
:
[mirandam@atlas ~]$ cd .ssh
[mirandam@atlas .ssh]$ touch config
with the following contents:
Host site1
Hostname site1domain.com
Port 33333
User username_site1
Host site2
Hostname site2domain.net
Port 33333
User username_site2
Now I can ssh to either site with a simpler command. These do exactly the same as the previous ssh commands:
# ssh site1
# ssh site2
NOTE: Read the man page carefully. If you see the following error:
Bad owner or permissions on /home/mirandam/.ssh/config
This means you did not properly set the permissions on the config file. To fix:
# chmod 600 ~/.ssh/config
There are many other options in the config file for users who might have more specific options (X11 Forwarding, Timeouts, Compression, etc.).
For anyone with multiple ssh accounts on different servers, this is very convenient to implement. Note this also works for scp
and sftp
.
Posted in: Hosting, Miscellaneous, Server, Tips,
1 Comments:
suvayu on September 5, 2009 - 05:05 AM
thank you for this tip. very helpful :)