creating ssh keys using ssh-keygen and copying to server
Creating SSH keys using ssh-keygen command on Linux :
We can create SSH key using ssh-keygen command in CentOS and ubuntu Linux system. Please use the following command to create your SSH private and public keys
1 |
ssh-keygen -t rsa |
Once you enter the above command, It will ask you for the keyphrase, Keyphrase adds an additional layer of security but if you want to make SSH or SCP through scripts then it is a good idea to leave it blank. Here i’m not using any keyphrase for simplicity.
Sample Output :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
root@ubuntu:/home/venkatesh/Desktop/python/parse# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa already exists. Overwrite (y/n)? y Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: :321:dsa:219 root@ubuntu The key's randomart image is: +--[ RSA 2048]----+ | | | | | | | | | S o E| | .oo. = o+| | .o.... Oo+| | ..oo. = ==| | o+ =-.| +-----------------+ root@ubuntu:/home/venkatesh/Desktop/python/parse# |
You can see your Public key and private key under your home directory ex : /root/.ssh folder
1 2 |
root@ubuntu:/home/venkatesh/Desktop/python/parse# ls /root/.ssh/id_rsa.pub /root/.ssh/id_rsa.pub |
Moving SSH key’s to Server using ssh-copy-id command:
Run the following command to move your SSH key’s to your server.
1 |
ssh-copy-id -i /root/.ssh/id_rsa username@serverAddress.com |
Sample output :
1 2 3 4 5 6 7 8 9 |
root@ubuntu:/home/venkatesh/Desktop/python/parse# ssh-copy-id -i /root/.ssh/id_rsa root@test.sillycodes.com /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 root@test.sillycodes.com's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@test.sillycodes.com'" and check to make sure that only the key(s) you wanted were added. |
Testing the SSH key:
Now, let’s try to log into the server using SSH.
1 |
ssh root@yourserver.com -p Port_number |
Sample Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
root@ubuntu:/home/venkatesh/Desktop/python/parse# ssh root@test.sillycodes.com Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-83-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage Get cloud support with Ubuntu Advantage Cloud Guest: http://www.ubuntu.com/business/services/cloud 82 packages can be updated. 0 updates are security updates. *** System restart required *** Last login: Wed Oct 11 20:11:41 2017 from 183.83.77.58 |
That’s it your successfully created SSH key’s and uploaded it into server.