Learn SSH: Config File

Use the ~/.ssh/config file to connect to servers by name, with identity files, multiple hosts, ProxyJump through a jump server, and wildcard patterns.

On this page

This is part three of the series. In part two, every connection meant typing a long command with a username, an address, a key and maybe a jump server. The SSH config file lets you save all of that once and connect by a short name.

Basic Setup

I start with a simple server entry and then show how to set up jump servers.

The SSH config file is in the .ssh folder of your home directory:

~/.ssh/config

For each server, add a block of text starting with Host followed by a memorable name for that server.

Host favorite_ssh

Add information like the address of the host on the following line with indentation:

Host favorite_ssh
        HostName ip_or_address_of_server
        User username_at_the_server

Now save the file. To connect to the server, in a terminal window simply type ssh followed by the name you chose in the config file:

$ ssh favorite_ssh

This is equivalent of typing the following command:

$ ssh username_at_the_server@ip_or_address_of_server

Adding an Identity File

You can add the identity file location to the config file as well:

Host favorite_ssh
        HostName ip_or_address_of_server
        User username_at_the_server
        IdentityFile ~/.ssh/my_private_key

which is equivalent of the following command:

$ ssh username_at_the_server@ip_or_address_of_server -i ~/.ssh/my_private_key

Multiple Servers

You can add multiple servers to the config file:

Host favorite_ssh
        HostName ip_or_address_of_server
        User username_at_the_server
        IdentityFile ~/.ssh/my_private_key

Host favorite_ssh_2
        HostName ip_or_address_of_server_2
        User username_at_the_server_2
        IdentityFile ~/.ssh/my_private_key_2

Host favorite_ssh_3
        HostName ip_or_address_of_server_3
        User username_at_the_server_3
        IdentityFile ~/.ssh/my_private_key_3

Proxy / Jump Servers

You can add a proxy or jump server to the config file as well and use it to connect to your favorite_ssh:

Host proxy_1
        HostName ip_or_address_of_proxy_server
        User username_at_proxy_server
        IdentityFile ~/.ssh/proxy_server_private_key

Host favorite_ssh
        HostName ip_or_address_of_server
        User username_at_the_server
        IdentityFile ~/.ssh/my_private_key
        ProxyJump proxy_1

Using Wildcards

You can also use wildcards to apply the same setting to a range of IP addresses (10.0.* and 10.1.0.*):

Host proxy_1
        HostName ip_or_address_of_proxy_server
        User username_at_proxy_server
        IdentityFile ~/.ssh/proxy_server_private_key

Host 10.0.*
        User username_at_these_servers
        IdentityFile ~/.ssh/my_private_key_for_all_the_servers
        ProxyJump proxy_1

Host 10.1.0.*
        ProxyJump proxy_1

Now to connect to a server with IP address of 10.0.0.1, you can use the following command:

$ ssh 10.0.0.1

Or to connect to 10.1.0.2 with a username my_user, you can run:

$ ssh my_user@10.1.0.2
Written by Sina Fathi-Kazerooni

I write about reinforcement learning, transformers, multi-agent systems, and the tools I build along the way.