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/configSSH 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.comThe 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@productionYou 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 usernameNow you only need to provide the alias:
ssh productionYou 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_keyIf the server listens on a nonstandard port, add a Port directive:
Host production
HostName server.example.com
User username
IdentityFile ~/.ssh/production_key
Port 2222Every 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 productionThe SSH client reads ~/.ssh/config and applies the matching connection settings automatically. This keeps your commands short and makes each server easier to remember.
Join the discussion!
Comments
Want to comment on this lesson?
Log in to leave comments.