So we have a bunch of servers that we need constantly managed trough ssh. Constantly typing in your password gets annoying pretty fast. So lets look how we can use ……. to make our life little bit simpler and save valuable time we can waste on memes.
- Lets generate a key pair on the local computer.
– Don’t use passphrase for key.
Output should be similar to this:
user@user ~ $ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/user/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user/.ssh/id_rsa. Your public key has been saved in /home/user/.ssh/id_rsa.pub. The key fingerprint is:Now there is a private key in your .ssh folder “id_rsa”, and public key “id_rsa.pub”.
- Now we need to copy the public key file from our local computer to the server we want to connect without password. For that we use “ssh-copy-id”.
ssh-copy-id -i ~/.ssh/id_rsa.pub UserName@RemoteServer
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub" /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 user@server's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'user@server'" and check to make sure that only the key(s) you wanted were added.
Now you can see on your server that in “.ssh” folder there is a file called “authorized_keys” and if you check the content, you can see that your public key info is in that file.