Using SSH keys in Linux
Creating a Key Pair
To create a key pair use the ssh-keygen
command. Before running the command check to see if you might already have a key pair. Keys are located in the .ssh/
directory in your home directory. The default key names are id_rsa
for the private key and id_rsa.pub
for the public key. The command
ssh-keygen -b 2048 -t rsa
will generate output similar to the following
Generating public/private rsa key pair. Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/ubuntu/.ssh/id_rsa. Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub. The key fingerprint is: ef:87:b5:b1:4d:7e:69:95:3f:62:f5:0d:c0:7b:f1:5e ubuntu@test-key The key's randomart image is: +--[ RSA 2048]----+ | | | | | . | | o . | | S o o.| | . + +oE| | .o O.oB| | .. +oo+*| | ... o..| +-----------------+
on vous demandera d'entrer une phrase de passe. Si vous avez déjà sauvegardé une paire de clés avec leurs noms par défaut, vous pourriez nommer le fichier pour conserver les nouvelles clés afin de ne pas détruire les clés existantes.
Connecting using a Key Pair
Once your key pair has been created copy the public key (the /home/ubuntu/.ssh/id_rsa.pub
key file in the above on your local machine) to the /home/USERNAME/.ssh/authorized_keys
file on the server you wish to connect to. If you had to create the file /home/USERNAME/.ssh/authorized_keys
ensure the file authorize_keys
and directory .ssh
have the correct permissions with chmod 600 ~/.ssh/authorized_keys
and chmod 700 ~/.ssh
respectively.
Finally test the new key by sshing to the remote machine from the local machine with
ssh -i /home/ubuntu/.ssh/id_rsa USERNAME@ADDRESS
où /home/ubuntu/.ssh/id_rsa identifie le fichier de la clé privée; USERNAME est le nom d'utilisateur sur le serveur; et ADDRESS l'adresse du serveur.