Saving some typing and time with SSH configs


If you are like me, then you may find yourself connecting via SSH to multiple machines a lot during the day. There is an easy way to make short cuts to save you remembering ip addresses, user names, or other things.
This tutorial is for people using Linux primarily, people using Windows should be able to save ‘sites’ in putty or what they use.

There are multiple ways to achieve this, the first is probably more common, adding an alias into .bashrc or .profile

alias sshserver="ssh user@serverhost"

This works, but you need a new alias or each server you run. It can be handy if you want to login to multiple servers or run multiple commands
eg

ALLHOSTS="127.0.0.1 10.40.1.3 10.33.6.95"
for ihost in $ALLHOSTS ; do ssh root@${ihost} "/etc/init.d/apache2 restart" ; done

This would login to all 3 IP addresses, restart apache and log out.

The second method would be more for just logging to do general work. You need to edit a file in your ~/.ssh/config and add configuration options. If this file does not exist, feel free to create it, chmod 600 ~/.ssh/config , and it will just work.

Some examples would look like this

Host backupspace
HostName backupspace.rimuhosting.com
User root

Host server1
Hostname 10.40.1.99
User root
Port 1022

Host staff
Hostname 10.40.2.33
User lizq
ForwardAgent yes

If you want that connection to stay alive longer you can add options like this

ServerAliveInterval 30
ServerAliveCountMax 120

There are a ton of configurations, you can find most in this page http://www.openbsd.org/cgi-bin/man.cgi?query=ssh_config&sektion=5

Once those are setup, you can run them like this

ssh server1

This will log you into server1 config on IP 10.40.1.99 with username root and port 1022. It will save you from typing the alternative of ssh -p1022 root@10.40.1.99 , and it also works just fine for scp also.