SSH config basics

/ Jack Lot


An SSH config file records the hostname, username and identity file for each server you connect to, under a nickname. Instead of a long EC2 hostname and the path to a key, you type ssh dev-box. I found out about it embarrassingly late and have never gone back.

The cheatsheet

Connecting without a config

ssh <username>@<hostname>

The plain version, which prompts you for a password.

ssh <username>@<hostname> -i <path to identity file>

The same connection using a key instead. -i points at your private key file, which is more secure but long enough that you stop wanting to type it.

Creating the config file

touch ~/.ssh/config

A plain text file in the hidden .ssh folder in your home directory.

Adding a host

Host dev-box
    HostName ec2-203-0-113-25.compute-1.amazonaws.com
    User ubuntu
    IdentityFile ~/.ssh/aws-dev.pem

Host is the nickname you’ll type, and the three lines under it are the username, hostname and identity file you were passing on the command line.

Setting a non-default port

Host web
    HostName example.com
    User jack
    Port 2222

A second entry in the same file. Each one can set different options.

Connecting by nickname

ssh dev-box

Connects using everything under that entry.

Going further

The identity file is half of an SSH key pair, and if you have not generated one yet that’s SSH Explained. A shell alias solves the same problem and lives alongside the tweaks in my 4 ZSH terminal essentials.


TAGS: videos, tutorials, terminal