1.1) Run the following commands in your local machine to generate (public and private) key pair
ssh-keygen -t rsa
1.2) It will ask for the path to save the keys, and will also show the default path
Enter file in which to save the key (/home/demo/.ssh/id_rsa):
Press enter
key to use the suggested default path or you can enter a custom path followed by enter
key
1.3) Next it will ask for a passphrase.
Enter passphrase (empty for no passphrase):
Passphrase is nothing but a secondary password which will be used while logging through ssh. You can keep it blank if you don't want to use (Press enter
to skip).
At the end the two keys will get saved in that particular path mentioned in step 1.2
The private key will get saved as id_rsa
and
the public key as id_rsa.pub
Login into your droplet using normal login (Using username and password) and copy the file from your local machine
cat ~/.ssh/id_rsa.pub | ssh demo@your_droplet_ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys"
Note: Replace key path (
~/.ssh/id_rsa.pub
) if you have used any other path in step 1.2 This will ask for your login password.
Now login without password
ssh root@your_droplet_ip