October 03, 2011

Per host username configuration for ssh

Using ssh to access many remote servers is a common scenario and more often the user names on these servers might be different from the one on the local machine and/or different among themselves. Ssh allows one to set the default username to use per host. You need to edit either the user specific configuration file (~/.ssh/config) or the system wide configuration file (/etc/ssh/ssh_config) and add the following :

1     Host <hostname>
2          User <username>

You can add as many entries as you want like above and the next time you can just do

$ ssh <hostname>

and ssh will try to log you in with the default username specified for that host in the configuration file.
More information on ssh can be found on the ssh man page (man ssh) and more information about the ssh configuration options can be found on the ssh_config man page(man ssh_config). (BTW, setting up passphrase-less ssh authentication is suggested if you connect to the remote servers very often and if security is not of high priority).