Skip to content

Use SSH Config to Remember Connection Details


You don’t need to remember all of your SSH connection details. And there is a better way than digging through history or creating aliases for them. If you frequently have to use the same connection or it has many arguments to specify, SSH config is what you are looking for.

Setting up SSH Config

Let’s say we use the following command to connect to our personal website’s server.

ssh -i ~/.ssh/ec2-keys/ec2.pem [email protected]

And we use this command to connect to our project’s server that we are currently working on.

ssh -i ~/.ssh/ec2-keys/another-key [email protected] -p 2345

Instead of typing all of that every time, let’s create a configuration that remembers these details for us. Start with opening the configuration file.

nano ~/.ssh/config

If the file doesn’t exist yet, that’s okay, run the command anyway because it will create the file for you on save. Each connection you want to save must be named. You specify the name after the Host keyword. After that, every keyword and it’s corresponding argument is sectioned to that connection until a specification for another Host or the end of the file. To specify the IP address of the server use the HostName keyword, and for the port use the Port keyword. The user to log in with is specified with the User keyword. And the SSH key to use for this connection is specified with the IdentityFile keyword. You can also write comments, which can be done by prepending a # to the text.

# Example of a configuration

Host personal-site
        User ec2-user
        IdentityFile ~/.ssh/ec2.pem

Host awesome-app
        Port 2345
        User ubuntu
        IdentityFile ~/.ssh/work/another-key

The correct permissions for the config file should be that only the owner, which is your user, can read and write to this file. Use ls -l to verify the permissions

ls -l
-rw------- 1 yourUsername yourUsername  124 Apr  2 10:27 config

If the permissions are not set correctly, you can use the following command to achieve the aforementioned setting.

chmod 600 ~/.ssh/config

Using the Configuration

After creating the config for a connection, you can now use it by referencing its name. Instead of typing all the details, you only pass the name you specified after the Host keyword.

ssh personal-website
ssh awesome-app

You can press the tab key to auto-complete the specified Host names. This configuration can also be used with the scp and sftp programs.

scp filename personal-website:/home/ec2-user/filename