SSH Key Passwordless Authentication With Remote Servers

Last updated on July 28, 2017

On your linux machine run the following command

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
techbear@techbear-desktop:~$ ssh-keygen
techbear@techbear-desktop:~$ ssh-keygen
techbear@techbear-desktop:~$ ssh-keygen

You will be asked the below now. Save the key to the default path and then choose if you want to put a passphrase in. If you do you’ll have to enter it each time you use your key, if you don’t then depending on how you have your setup it *may* be less secure. I use other methods to lock down both my desktop and servers so I’m happy with no password for ease.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Generating public/private rsa key pair.
Enter file in which to save the key (/home/techbear/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/techbear/.ssh/id_rsa.
Your public key has been saved in /home/techbear/.ssh/id_rsa.pub.
Generating public/private rsa key pair. Enter file in which to save the key (/home/techbear/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/techbear/.ssh/id_rsa. Your public key has been saved in /home/techbear/.ssh/id_rsa.pub.
Generating public/private rsa key pair.
Enter file in which to save the key (/home/techbear/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/techbear/.ssh/id_rsa.
Your public key has been saved in /home/techbear/.ssh/id_rsa.pub.

Now your private and public key are created you need to copy your public key to your remote server so that you can connect without password going forward. Run the following command to do this. You need ssh access to the server to do this so if it’s firewalled or turned off you’ll need to address that first. You can do the below for any linux user on the server, if you want to connect as a webspace user use that username, if you want to connect as root just use that username (also you’ll need to ensure direct root login is enabled if it isn’t).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
techbear@techbear-desktop:~$ ssh-copy-id <yourserveruser>@<yourserverhostname>
techbear@techbear-desktop:~$ ssh-copy-id <yourserveruser>@<yourserverhostname>
techbear@techbear-desktop:~$ ssh-copy-id <yourserveruser>@<yourserverhostname>

You should get the following after issuing the command

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/techbear/.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
<yourserveruser>@<yourserverhostname>'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '<yourserveruser>@<yourserverhostname>'"
and check to make sure that only the key(s) you wanted were added.
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/techbear/.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 <yourserveruser>@<yourserverhostname>'s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '<yourserveruser>@<yourserverhostname>'" and check to make sure that only the key(s) you wanted were added.
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/techbear/.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
<yourserveruser>@<yourserverhostname>'s password:

Number of key(s) added: 1
Now try logging into the machine, with: "ssh '<yourserveruser>@<yourserverhostname>'"
and check to make sure that only the key(s) you wanted were added.

Lastly check you can now login to your remote linux server without a password as it mentions by doing

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ssh '<yourserveruser>@<yourserverhostname>'
ssh '<yourserveruser>@<yourserverhostname>'
ssh '<yourserveruser>@<yourserverhostname>'

If everything has worked correctly you should be able to connect to your remote server without login now.