Connect to a server with SSH without password using Public/Private keypair (github / digitalocean)

1) Checkinf if ssh keys already exist
go into your base directory and check which key you already have on your server:
ls -la ~/.ssh
this will list all the public key you already have (if you have)
2) Create a new keypair
ssh-keygen -t RSA -b 4096 -c "user@email"
a prompt will ask you to select a name for the files to where the keys will be written (otherwise a default generic name is chosen)
I usually name the file with something related to which service I want to connect to.
(For exemple I will give the following name: "github_rsa").
Then I can check again on my .ssh folder, I should have 2 new files:
github_rsa
github_rsa.pub
3) Restart the ssh server
We then need to restart the SSH server to make sure that he will integrate this new keypair when we try to connect to the newly added server. 
# start the ssh-agent in the backgroundeval "$(ssh-agent -s)"# Agent pid 59566ssh-add ~/.ssh/github_rsa
4) Add your newly created Public Key to the server where you want to connect.
to display it:
cat ~/.ssh/github_rsa.pub  
Then paste it to the "ssh_keys" settings on the service you want to connect to.
(a similar web page on "my settings", on digitalOcean and gitHub)
4) Test the connection to the server
When you try to connect on the server for the first time it should propose to add the server's remote public key to your "known_hosts" file on .ssh folder.
ssh -Tv git@github.com
Note:  for the case of github it should not connect to the ssh, but should return a specific denying message with your user name on it (that will show that the remote server has recognised you through the key).
Hi username! You've successfully authenticated, but GitHub does not
# provide shell access.

Commentaires