Simplifying SSH connections using SSH config

Using SSH to connect to a remote server is as simple as remembering the details: username, host, and password, plus any connection-specific quirks, like non-standard port.

That said, the more remote servers to which you need to connect on a regular basis—each with its own access configuration—the more complicated a task it can become to keep all these details straight.

Fortunately you can create shorthand for each of these connections in your SSH config file, located in the .ssh subdirectory inside your home directory (~/.ssh/config).

If either the subdirectory or the file don’t exist, create them:

## create ".ssh" subdirectory
mkdir -p ~/.ssh

## create "config" file and assign permissions
touch ~/.ssh/config && chmod 0600 ~/.ssh/config

Then, create a new Host entry in this file for each connection, including in it all of the access parameters for that connection. Generally this includes:

The “alias” for the connection. Make it something memorable, short, and easy to type.

Not to be confused with Host, this is the domain or IP address for the server to which you want to connect.

The username for the connection.

SSH will use port 22 by default. This only needs to be included if you’re connecting on a different port.

If you’ve configured a public / private key pair for this connection (bravo) this is the path to the private key. If you’re using one default public / private key pair for all connections (yikes) this can be omitted.

[Want more options? Click here for the full list.]

Let’s assume we, t_anderson, want to make a connection to on port 2222, using a private key located at ~/.ssh/metacortex. Here’s the old-fashioned way of doing it:

# first we associate the private key with the connection
ssh -i ~/.ssh/metacortex

# then we create the connection
ssh -p 2222

And now the SSH config way. We add a Host entry to ~/.ssh/config, using “mc” for our connection shorthand:

Host mc
    User t_anderson
    Port 2222
    IdentityFile ~/.ssh/metacortex

And now all we need to do to make the connection is type:

ssh mc

Followed by saying “I’m in“, of course.