Have you ever wanted to login to your servers via SSH without using a password?
Here is a very short explanation of how to do it.
When it comes to SSH the computer you’re on now is a client machine. It connects to an SSH server. To allow a client and server to connect without a password they have to have a shared key.
On your client machine you must generate a key, which will identify you to the server.
ssh-keygen -t rsa
This will ask you several questions and generate a key in your ~/.ssh
directory.
The server must also have an ~/.ssh
directory so we can store the key you just generated in it.
To do this issue an SSH command to create a remote directory to your remote server like so. Be sure to replace your username and server location.
ssh username@serverlocation mkdir -p .ssh
The -p
tells mkdir
to not throw an error if the directory already exists.
Next we want to copy the SSH key we just generated over to the server’s authorized_keys
file. Most likely this file doesn’t exists, but we want to assume it does and has other SSH key in it. Thus we want to add to the file, not override it so we will be using the >>
operator to do so. We will also be using the cat
command, which will read the contents of a file.
cat ~/.ssh/id_rsa.pub | ssh username@serverlocation 'cat >> .ssh/authorized_keys'
That’s it.
This reads the SSH key we generated, send its contents over SSH to the server, which then adds its contents to the authorized_keys
file.
You can now login to your remote server without using a password.
The next time you try to login SSH will send the server your key, the server will find that key in its authorized_keys
file and then allow you to login because your key is authorized.
Leave a Reply
You must be logged in to post a comment.