In this article
SSH keys allow passwordless authentication between two hosts given that they support the ssh protocol. In other words most of the time you will be connecting to your remote server via SSH. Setting up SSH key gives us extra security as we can disable password login.
By SSH key we most often refer to RSA key pair, consisting of one private key and a public key. You must keep the private key securely.
- Open your terminal
- Paste the below command in your terminal and press enter
$ ssh-keygen -t rsa -b 4096
- When prompted to name your new key either provide a name or just press enter to use the default
Generating public/private rsa key pair. Enter file in which to save the key (/home/user/.ssh/id_rsa):
- Next you will be prompted to enter a passphrase. Feel free to add one or skip this section. Please note if you add one you will be required to enter it every time you use this key
Add the key to your the ssh agent
- Start the agent
$ eval "$(ssh-agent -s)" > Agent pid 320859
- Add the key to the agent
$ ssh-add ~/.ssh/id_rsa
Copy public key to your server
Once an SSH key has been created, we can use the
ssh-copy-id command to copy the key to our remote server
Paste the following command in your terminal and press enter:
$ ssh-copy-id -i ~/.ssh/mykey username@host_ip
This command establishes ssh connection to your server (change host_ip with the real server ip) and copies. The
-i flag tells the command which key to use. To check that key has been successfully copied you may SSH into your target host and go to
~/.ssh folder. Your public key should be there. If you aren’t sure what you are doing just add
-n flag the
ssh-copy-id command. This is run the command in dry run, which means key will not be effectively copied. Dry run is just a mere simulation of what the actual command will perform.