SSH KEY Authentication Linux How To

Hi,

If you want to have a workstation with (Linux or Mac) to SSH into a server without a passphrase, this is how you can do it.

On your workstation generate a “key” with ssh-keygen

ssh-keygen

This will create a id_rsa and id_rsa.pub under your home dir/.ssh

mac : /Users/your_account/.ssh/
linux : /home/your_account/.ssh/

id_rsa : this is your private key, you keep it safe on your machine!!
id_rsa.pub : this is the key you copy to each server you want to authenticate without a password.

Copy the id_rsa.pub to the server, you can use scp for that

scp /Users/your_account/.ssh/id_rsa.pub root@myserver.com:/root/.ssh/id_rsa_youraccount.pub

WARNING : the id_rsa.pub must be copy under the account you want to authenticate on the server. So if normally on your server you log in as root (not recommend), you will copy the id_rsa.pub under the root.

WARNING2 : I strongly suggest you rename the id_rsa.pub on the target for id_rsa_youraccount.pub because a file can already exist and might be overwritten.

Now. The id_rsa_youraccount.pub is on the server.
SSH to the server (yes with your credential, for the last time ;))

You need to import your key into a special file called authorized_keys

with the scp command I copied the file into the root.

cd /root/.ssh/

now import the id_rsa_youraccount.pub into the authorized_keys

cat id_rsa_youraccount.pub >> authorized_keys

Quit your current ssh.

Retry ssh with :

ssh root@myserver.com

No password should be asked.

 

Leave a Reply