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

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
User root

Host server1
User root
Port 1022

Host staff
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

Once those are setup, you can run them like this

ssh server1

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

This entry was posted in HOWTO and tagged , , . Bookmark the permalink.

Comments are closed.