<\/span><\/h2>\nTo generate the SSH key pair, simply run the following command from the terminal on your local Ubuntu 16.04 based computer:<\/p>\n
ssh-keygen<\/pre>\nYou can just press Enter to leave the default values to all the questions. Optionally, you can also set a passphrase to add an extra layer of security, or just press Enter again if you don’t want to.<\/p>\n
Generating public\/private rsa key pair.\r\nEnter file in which to save the key (\/root\/.ssh\/id_rsa):\r\nEnter passphrase (empty for no passphrase):\r\nEnter same passphrase again:\r\nYour identification has been saved in \/root\/.ssh\/id_rsa.\r\nYour public key has been saved in \/root\/.ssh\/id_rsa.pub.\r\nThe key fingerprint is:\r\nSHA256:2QB\/eJ39\/4N7Q3PRSghNJmglX6hh2pSqQ3uGHhfd7Og root@rosehosting\r\nThe key's randomart image is:\r\n+---[RSA 2048]----+\r\n| . .+o++ |\r\n| oB+o=.o |\r\n| O+=+.o.. .|\r\n| . + +*o . .o.|\r\n| . + .So. . .o|\r\n| * + . . .oo|\r\n| . * . o +|\r\n| . E . +.|\r\n| .o +|\r\n+----[SHA256]-----+<\/pre>\nThis will create a 2048 bit private and public key (id_rsa and id_rsa.pub) in the \/root\/.ssh\/ directory on your local system.<\/p>\n
<\/span>Upload the Public Key to the Ubuntu 16.04 server with ssh-copy-id<\/span><\/h2>\nThe next step requires that you upload the generated public key to your remote Ubuntu server. There is more than one method to do this.<\/p>\n
The most simple way is to use the ssh-copy-id<\/strong> utility. In order for this to work, you should be able to connect to your server via SSH using password authentication.<\/p>\nYou can then use the following command to upload the public key to your remote server:<\/p>\n
ssh-copy-id root@remote_server<\/pre>\nYou will be asked to enter your user password (in our example that would be the root password) and press Enter. After the connection has been established, the contents of the id_rsa.pub (the public key) will be copied into the \/root\/ssh\/authorized_keys file on the remote server.<\/p>\n
The following output should be displayed:<\/p>\n
Number of key(s) added: 1\r\n\r\nNow try logging into the machine, with: \"ssh 'root@remote-host'\"\r\nand check to make sure that only the key(s) you wanted were added.<\/pre>\nFrom now on, you should now be able to authenticate to your Ubuntu server using your SSH keys.<\/p>\n
<\/span>Upload the Public Key to the Ubuntu server manually<\/span><\/h2>\nIf the ssh-copy-id<\/strong> tool is not available on your system, you can log in to your server and do this manually.<\/p>\nFirst, connect yo your server via SSH with:<\/p>\n
ssh root@remote_server<\/pre>\nOnce connected, you will need to create a new .ssh directory on the remote server with the following command:<\/p>\n
mkdir \/root\/.ssh<\/pre>\nNext, you need to copy the public key into the authorized_keys file on the remote server. You can create and then add the contents of the public key using the nano text editor:<\/p>\n
nano \/root\/.ssh\/authorized_keys<\/pre>\nYou will also need to open the SSH configuration file:<\/p>\n
nano \/etc\/ssh\/sshd_config<\/pre>\nand make sure the following lines exist and are not commented:<\/p>\n
RSAAuthentication yes\r\nPubkeyAuthentication yes\r\nAuthorizedKeysFile %h\/.ssh\/authorized_keys<\/pre>\nSave the changes and restart the SSH service:<\/p>\n
systemctl restart ssh<\/pre>\nThat’s it. From now on, you will be using the SSH keys every time you try to connect to your remote server.<\/p>\n
<\/span>2. Generating SSH keys on Windows based system with PuTTY and PuTTYGen<\/span><\/h2>\nIf you are using Windows operating system on your local machine, then you will need PuTTY for connecting to your server via SSH and PuTTYgen to generate your SSH keys. You can download them over here (https:\/\/www.chiark.greenend.org.uk\/~sgtatham\/putty\/latest.html).<\/p>\n
To generate the SSH keys, go ahead and start PuTTYGen on your computer.<\/p>\n
At the bottom, you can choose the number of bits for your generated key. Type in 4096 and then click on Generate. Start moving your mouse around over the blank area on the PuTTYgen screen in order to generate a unique key based on your mouse movement input.<\/p>\n
When the green progress bar fills in, the public key will be generated. You can copy they key and save it inside a .txt file on your computer.<\/p>\n
To save the private key, you will need to click on the Save private key button. Additionally, you can also set a Key Passphrase for your private key.<\/p>\n
Now you can open PuTTY and to assign the location of your private key, click on SSH under the Connection menu and then click on Auth. Click on the browse button and enter the location of your saved private key file.<\/p>\n
Next, we need to upload the public key to your server. To do this, click on Session, enter your IP address and the SSH port number of your server, select SSH for Connection type and click on Open.<\/p>\n
You will be asked for your user name and password. You can log in as user root, using your root password.<\/p>\n
Once logged in to your server, first create the .ssh directory:<\/p>\n
mkdir \/root\/.ssh<\/pre>\nThen create the authorized_keys file inside the .ssh directory and add the contents of your public key inside this file:<\/p>\n
nano \/root\/.ssh\/authorized_keys<\/pre>\nYou can also open the SSH configuration file on your server with:<\/p>\n
nano \/etc\/ssh\/sshd_config<\/pre>\nand make sure that the following lines exist and are not commented:<\/p>\n
RSAAuthentication yes\r\nPubkeyAuthentication yes\r\nAuthorizedKeysFile %h\/.ssh\/authorized_keys<\/pre>\nIf any changes have been made, dont forget to restart the sshd service with:<\/p>\n
systemctl restart ssh<\/pre>\nWith this, the SSH keys has been succesfully configurad.You can now disconnect from your server and exit PuTTY. The next time your try to connect to your server, PuTTY will use the public key that you have set up in order to establish the connection.<\/p>\n