← blog

Connecting to a remote host using RSA keys

1. Create a new key pair (specify a name: for example, myhost. The file name is specified with the full path to the directory ~/.ssh/myhost)

$ ssh-keygen -t rsa -C "user@gmail.com"

2. Add the created key to ssh-agent.

$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/myhost

3. Copy the public key to the host.

$ ssh-copy-id -i ~/.ssh/myhost.pub username@hostname.com

4. Create a configuration file for convenient connection to the host.

$ nano ~/.ssh/config

File contents:

Host myhost
  HostName hostname.com
  User username
  RSAAuthentication yes
  IdentityFile ~/.ssh/myhost

5. Profit! You can connect to the host using:

$ ssh myhost