Home > All, Linux > SSH configuration: single host, multiple users.

SSH configuration: single host, multiple users.

A colleague of mine recently reminded me of the per-user ssh config file the other day, which typically lives at ~/.ssh/config or /home/user/.ssh/config

In this file you can define multiple ssh connections that you frequent and this becomes even more useful if you have many hosts/accounts to connect to.

Consider the following:

ssh [email protected] -p2222

Sure, it’s easy to remember, but what if you have many hosts, each with different ports?

Whilst reminding me about the config file however, he complained that you can’t define 2 different users for the same system. This isn’t true, and this is how it’s done:

Host user1
HostName ssh.example.com
User user1
Port 2222

Host user2
HostName ssh.example.com
User user2
Port 2222

That’s all! Now, when using ssh, you can just type “ssh ” and the first few characters of a Host and then hit tab to get a list of all available accounts. The autocompletion comes from the “Host” definition. Inside any Host profile, you can basically use any other parameter documented in `man ssh_config`. Definition for Host as follows:

Host Restricts the following declarations (up to the next Host keyword) to be only for those hosts that match one of the patterns given after the keyword.  If more than one pattern is provided, they should be separated by whitespace.  A single ‘*’ as a pattern can be used to provide global defaults for all hosts.  The host is the hostname argument given on the command line (i.e. the name is not converted to a canonicalized host name before matching).

Edit: As pointed out by Leonid, you will need `yum install bash-completion` or its equivalent for host autocompletion to work.

  1. No comments yet.
  1. No trackbacks yet.