Video Lesson: Simplify connections with SSH config files

Simplify connections with SSH config files

Learn how to simplify SSH connections by defining reusable host aliases and connection settings in a configuration file.


Lesson Content

An SSH config file gives you a simple way to connect to a server without remembering every connection detail. Instead of entering the username, hostname, port, and key each time, you define them once and connect using an alias.

Create and open the file at ~/.ssh/config:

vi ~/.ssh/config

SSH config files use a simple block format. Each block starts with a Host directive, which defines the alias you use when connecting. Add a HostName directive to specify the actual server hostname:

Host production
    HostName server.example.com

The Host value is an alias, while HostName identifies the remote server. You can now connect with the alias instead of typing the full hostname, still passing the username:

ssh username@production

You can make the connection even simpler by adding a User directive. This sets the username for the connection:

Host production
    HostName server.example.com
    User username

Now you only need to provide the alias:

ssh production

You can also specify which private key SSH uses with the IdentityFile directive. This is useful when the connection requires a key other than your default SSH key:

Host production
    HostName server.example.com
    User username
    IdentityFile ~/.ssh/production_key

If the server listens on a nonstandard port, add a Port directive:

Host production
    HostName server.example.com
    User username
    IdentityFile ~/.ssh/production_key
    Port 2222

Every directive in the block is optional, so include only the settings you need. Once the block is configured, connect using the alias from the Host directive:

ssh production

The SSH client reads ~/.ssh/config and applies the matching connection settings automatically. This keeps your commands short and makes each server easier to remember.

Maggie

Hey, I'm Maggie! 👋

Ask me anything.

Want to chat with Maggie?

Log in for AI tutor access.

Comments

Join the discussion!

Notes

Keep track of what you learn!