Introduction
SSH (Secure Shell) is an encrypted protocol used to administer and communicate with servers.. It is one of the secure way of logging into server and are recommended for all users.
Step 1 – Creating SSH Key-Pair
First create it on the clinet machine.
$ ssh-keygen
By default recent versions of ssh-keygen it
will create a 3072-bit RSA key pair. Press enter the save the ssh-keygen.
If you have generated the ssh-keygen
earlier it will prompt to overwrite (y/n) and then enter passphrase. You can leave it blank or enter a passphrase for more security.
Output /home/your_home/.ssh/id_rsa already exists. Overwrite (y/n)? ..... ..... Output Enter passphrase (empty for no passphrase): ..... .... Output Your identification has been saved in /your_home/.ssh/id_rsa Your public key has been saved in /your_home/.ssh/id_rsa.pub The key fingerprint is: SHA256:/hk7MJ5n5aiqdfTVUZr+2Qt+qCiS7BIm5Iv0dxrc3ks user@host The key's randomart image is: +---[RSA 3072]----+ | .| | + | | + | | . o . | |o S . o | | + o. .oo. .. .o| |o = oooooEo+ ...o| |.. o *o+=.*+o....| | =+=ooB=o.... | +----[SHA256]-----+
Now you have a public and private key which help to authenticate.
Step 2 – Copying public SSH Key-Pair to Ubuntu Server
Method-1
Using ssh-copy-id
$ ssh-copy-id username@remote_host
Method-2
or, If system do not support ssh-copy-id
then we can upload the keys using a conventional SSH method.
$ sudo cat ~/.ssh/id_rsa.pub | ssh username@remote_host "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"
You would see following output make sure to enter and then put the same passphrase as used in client system.
Output The authenticity of host '203.0.113.1 (203.0.113.1)' can't be established. ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe. Are you sure you want to continue connecting (yes/no)? yes ..... .... Output /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys username@203.0.113.1's password: ..... ..... Output Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'username@203.0.113.1'" and check to make sure that only the key(s) you wanted were added.r
Method-3
Or, we can also do manually
$ sudo cat ~/.ssh/id_rsa.pub
Output ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCqql6MzstZYh1TmWWv11q5O3pISj2ZFl9HgH1JLknLLx44+tXfJ7mIrKNxOOwxIxvcBF8PXSYvobFYEZjGIVCEAjrUzLiIxbyCoxVyle7Q+bqgZ8SeeM8wzytsY+dVGcBxF6N4JS+zVk5eMcV385gG3Y6ON3EG112n6d+SMXY0OEBIcO6x+PnUSGHrSgpBgX7Ks1r7xqFa7heJLLt2wWwkARptX7udSq05paBhcpB0pHtA1Rfz3K2B+ZVIpSDfki9UVKzT8JUmwW6NNzSgxUfQHGwnW7kj4jp4AT0VZk3ADw497M2G/12N0PPB5CnhHf7ovgy6nL1ikrygTKRFmNZISvAcywB9GVqNAVE+ZHDSCuURNsAInVzgYo9xgJDW8wUw2o8U77+xiFxgI5QSZX3Iq7YLMgeksaO4rBJEa54k8m5wEiEE1nUhLuJ0X/vh2xPff6SQ1BL/zkOhvJCACK6Vb15mDOeCSq54Cr7kvS46itMosi/uS66+PujOO+xt/2FWYepz6ZlN70bRly57Q06J+ZJoc9FfBCbCyYH7U/ASsmY095ywPsBo1XQ9PqhnN1/YOorJ068foQDNVpm146mUpILVxmq41Cj55YKHEazXGsdBIbXWhcrRf4G2fJLRcGUr9q8/lERo9oxRm5JFX6TCmj6kmiFqv+Ow9gI0x8GvaQ== demo@test
Create a SSH directory
sudo mkdir -p ~/.ssh sudo echo public_key_string >> ~/.ssh/authorized_keys
Set the right permission
sudo chmod -R go= ~/.ssh
Also make sure that the ~/.ssh
directory belongs to the user and not to root:
chown -R user:user ~/.ssh
Step 3 – Authenticating Ubuntu Server using SSH Keys
$ ssh username@remote_host
Output The authenticity of host '203.0.113.1 (203.0.113.1)' can't be established. ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe. Are you sure you want to continue connecting (yes/no)? yes ....
You should now be able to access the key-based authentication to another system.
Step 4 – Finale
Hurray, now we have SSH-key-based authentication configured on your server, allowing you to sign in without providing an account password.