Configure ssh to connect to a server

By Jake Morrison in DevOps on Wed 01 May 2019

This article describes how to configure ssh to connect to a server using an ssh key for access. Using ssh keys is more secure than passwords, and makes it easier to automate systems using tools like Ansible.

First, create an ssh key, if you don't have one already.

Configure your ssh config file

If your server only has an IP address, you can make a host alias to make it easier to use. Create a file on your local machine called ~/.ssh/config. Add the server to it:

Host web-server
    HostName 123.45.67.89

The ssh config file supports a lot more options. For example, you can specify the userid to use on the remote server, the port, or the key.

Set the file permissions on ~/.ssh/config:

chmod 600 ~/.ssh/config

ssh is picky about file permissions. For security, the files and directories need to only be readable by you, and ssh will refuse to work if they are wrong.

Test it by connecting to the server:

ssh user@web-server

If it doesn't work, run ssh with -v flags to see what the problem is. You can add more verbosity, e.g. -vvvv if you need more detail.

ssh -vv user@web-server

For example:

chown $USER:staff ~/.ssh
chmod 700 ~/.ssh

chown $USER:staff ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa