How to Set up SSH Public Key Auth for Remote Access
Secure Shell (SSH) is a widely-used cryptographic network protocol for securely connecting to remote servers and systems. While traditional password-based auth is still common, using SSH keys provides an additional layer of security and convenience. SSH public key auth relies on asymmetric cryptographic algorithms that generate a pair of separate keys (a key pair), one “private” and the other “public”. You keep the private key a secret and store it on the computer you use to connect to the remote system. In this blog, we’ll walk you through the process of generating an SSH key, copying the public key to a remote server, and connecting to it using SSH key auth. Let’s start .
Step 1: Generating an SSH Key
The first step is to generate an SSH key pair on your local machine. To do this, open a terminal or command prompt and execute the following command:
ssh-keygen -t rsa
You’ll be prompted to choose a location to save the key pair. The default location is usually `~/.ssh/id_rsa`
for the private key and `~/.ssh/id_rsa.pub`
for the public key. If you don’t need to use a specific passphrase, you can press Enter to leave it empty. However, adding a passphrase adds an extra layer of security, making it necessary to provide the passphrase each time you use the key.
Step 2: Copying the Public Key to the Remote Server
Now that you have generated your SSH key pair, you need to copy the public key to the remote server. To do this, you can use the scp
command (Secure Copy Protocol) as follows:
scp ~/.ssh/id_rsa.pub username@remote_system_ip:~/.ssh/authorized_keys
Replace username
with your remote server username, and remote_system_ip
with the actual IP address or domain name of the remote server. The command will copy the contents of your public key file (id_rsa.pub
) to the authorized_keys
file in the .ssh
directory on the remote server.
Step 3: Connecting to the Remote Server using SSH Key Authentication
With the public key copied to the remote server, you can now connect to it using SSH key authentication. Open a terminal and execute the following command:
ssh -p port username@remote_system_ip
Replace port
with the SSH port of the remote server (typically port 22 unless you have configured it differently), username
with your remote server username, and remote_system_ip
with the actual IP address or domain name of the remote server.
If you have set a passphrase for your private key during Step 1, you’ll be prompted to enter it before establishing the SSH connection. Once you provide the correct passphrase (if applicable), the SSH client will use the private key to authenticate with the remote server, and if the public key matches what’s in the authorized_keys
file, you’ll be granted access without needing to enter a password.
Conclusion
By following this step-by-step guide, you have successfully generated an SSH key pair, copied the public key to the remote server, and established an SSH connection using key-based authentication. Using SSH keys adds an extra layer of security to your remote connections and eliminates the need for passwords, making the whole process more secure and convenient.
It’s recommended to use specialized hosting platforms like Godaddy, Hostinger, AWS, or DigitalOcean.
Remember to keep your private key safe and never share it with anyone. Regularly updating your keys and using passphrases are good security practices that you should adopt to safeguard your systems from unauthorized access. Happy secure remote server management!
Also Read : How to Deploy Your Django Website to Netlify