How to setup passwordless login to the SSH server
Background
- When you work on the remote host (SSH server) regularly (e.g. for big data analysis), it is convenient to set up passwordless login to the SSH server from your local host (SSH client).
- SSH (Secure Shell) is a software package that enables SSH client to securely connect to an SSH server over a insecure network.
- SSH is secure and encrypts the connection between the SSH client and SSH server. By using it, you can protect your data, remote server authorizations, file transfers, and remote server work (e.g. commands) from network hacking attacks.
- SSH protocol uses the client-server model to make a secure connection between the SSH client and SSH server. In this model, the client and server interact with each other using SSH keys to make a secure connection.
How to setup passwordless login
SSH is a default software package on Linux, Mac OS, and Windows, and hence you do not have to install it. In addition, you can also use other SSH clients such as PuTTY and MobaXterm on Windows platform.
This tutorial is intended for Linux and Mac OS platforms only.
1. Check if public and private SSH key pair exists
You can use ls
commands to see the files ending with either .rsa
or .dsa
in ~/.ssh
folder
ls ~/.ssh
If you see a file either of id_rsa
or id_dsa
and id_rsa.pub
or id_dsa.pub
, it means the SSH key pair already
exists. In this case, you can either replace the existing key pair by creating new one or skip to next step.
If you do not see these files, you can create SSH key pairs as described in following step.
2. Generate public and private SSH key pairs
SSH keys can be generated using ssh-keygen
. You can use RSA or DSA algorithm for the generation of SSH key pair using
ssh-keygen
. The RSA is preferred over the DSA algorithm for SSH key pair generation. You can see the difference
between these two algorithms here
ssh-keygen -t rsa
# follow the instructions from this command until you create a SSH keys
You may have to enter a passphrase for generating SSH keys. The passphrase is similar to a password and is used for encrypting the private key.
This will create id_rsa
(private key) and id_rsa.pub
(public key) under the ~/.ssh
folder. The key size is 2048 bits
which is recommended for RSA. You can also increase the key size to 4096 bits by using ssh-keygen -t rsa -b 4096
3. Copy public key to SSH server
The public key (id_rsa.pub
) need to be copied into authorized_keys
file into SSH server using ssh-copy-id
ssh-copy-id username@remote_host
At this step, you may have to enter the password for the SSH server
You should never copy private key (
id_rsa
) to any remote host
4. Login to SSH server
Once the public key is added to the SSH server authorized file, you should be able to login to a SSH server without a
password. But at this step, it may ask you to enter a passphrase that you have created during the ssh-keygen
step.
ssh username@remote_host
If it asks for a passphrase for connection to a SSH server, follow the below steps to add the private key to
SSH Agent (ssh-agent
).
Start SSH agent,
eval `ssh-agent`
Add SSH private key (listed in ~/.ssh
) to the SSH agent
ssh-add
Now, you should be able to login to a SSH server without a password and passphrase,
ssh username@remote_host
Summary
In this article, the background of the SSH client-server model is explained, along with how it works. SSH provides a secure connection between the local host and remote host. This article taught you how to log into a SSH server without a password and passphrase.
Enhance your skills with courses on Python and pandas
- Python for Data Analysis: Pandas & NumPy
- Mastering Data Analysis with Pandas: Learning Path Part 1
- Data Analysis Using Python
- Python for Everybody Specialization
References
This work is licensed under a Creative Commons Attribution 4.0 International License
Some of the links on this page may be affiliate links, which means we may get an affiliate commission on a valid purchase. The retailer will pay the commission at no additional cost to you.