Normal case is that every time you connect to a server by SSH, you are prompted for a password. This tutorial is about how to set up ssh key to enable passwordless automatic login.
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/theuser/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/theuser/.ssh/id_rsa.
Your public key has been saved in /home/theuser/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:X+k7bT8m/47oq3zSv7MuefVViGH3tgNKG3IwrFv8ksA [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| .o. . .o |
| o.+ o = |
| . o + + = o . . |
| E o o o ... .|
| + o S .o .|
| . o . . o o|
| . ...o .o|
| .. *+=+.|
| o==OXB*|
+----[SHA256]-----+
When passphrase is prompted, just hit enter. Please be aware that without the passphrase, anybody can connect to the server if your private key is known (id_rsa), so keep your private key in safe!
Let’s assume that the target server where you want to have the passwordless login is target-host.com
and the user in that server is user
. With following command you can easily copy the public key to the server. Make sure that the folder .ssh
exists.
$ ssh-copy-id -i /home/theuser/.ssh/id_rsa.pub [email protected]
You are propably being asked for a password to connect to the server - just enter the password.
Now you should be ready. You can test the connection by trying to log in:
$ ssh [email protected]
This assumes that your private key resides in your current user’s .ssh
folder where it is read automatically. In case you created the private key elsewhere, you can connect like this:
$ ssh -i /home/theuser/some-folder/id_rsa [email protected]