6.12. Tailoring SSH per Host

Problem

You want to simplify a complicated SSH command line, or tailor SSH clients to operate differently per remote host.

Solution

Create a host alias in ~/.ssh/config:

               ~/.ssh/config:
Host mybox
        HostName mybox.whatever.example.com
        User smith
        ...other options...

Then connect via the alias:

$ ssh mybox

Discussion

OpenSSH clients obey configurations found in ~/.ssh/config. Each configuration begins with the word Host followed by an hostname alias of your invention.

Host work

Immediately following this line, and continuing until the next Host keyword or end of file, place configuration keywords and values documented on the ssh(1) manpage. In this recipe we include the real name of the remote machine (HostName), and the remote username (User):

Host work
        HostName mybox.whatever.example.com
        User smith

Other useful keywords (there are dozens) are:

IdentityFile ~/.ssh/my_alternate_key_dsa           Choose a private key file
Port 12345                                         Connect on an alternative port
Protocol 2                                         Use only the SSH-2 protocol

See Also

ssh_config(5) defines the client configuration keywords.

Get Linux Security Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.