How to Generate an SSH Key Pair and Add it to Your Remote Server on Linux
SSH (Secure Shell) keys provide a secure way to authenticate your identity when connecting to remote servers. They consist of a private key (kept secret) and a public key (shared with remote servers). This guide will walk you through generating an SSH key pair and adding it to your remote server for secure access.
Step 1: Generating SSH Key Pair
On Your Local Machine
- Open your terminal.
- Generate an SSH key pair using the
ssh-keygen
command:
- You'll be prompted to enter a file to save the key. Press
Enter
to save it in the default location (~/.ssh/id_rsa
), or specify a different path and filename if preferred. - Optionally, you can set a passphrase for added security. It's a password used to protect the private key.
Absolutely! Here's a blog post guide on generating an SSH private-public key pair and adding it to a remote server:
How to Generate an SSH Key Pair and Add it to Your Remote Server
SSH (Secure Shell) keys provide a secure way to authenticate your identity when connecting to remote servers. They consist of a private key (kept secret) and a public key (shared with remote servers). This guide will walk you through generating an SSH key pair and adding it to your remote server for secure access.
Step-by-step guide
Step 1: Generating SSH Key Pair
- Open your terminal on your local machine.
- Generate an SSH key pair using the
ssh-keygen
command:bashCopy codessh-keygen -t rsa -b 4096 -C "[email protected]"
Replace"[email protected]"
with your email address. This command generates an RSA key pair (4096-bit) associated with your email. - You'll be prompted to enter a file to save the key. Press
Enter
to save it in the default location (~/.ssh/id_rsa
), or specify a different path and filename if preferred. - Optionally, you can set a passphrase for added security. It's a password used to protect the private key.
Step 2: Adding Public Key to Remote Server
- On your local machine, display the public key using:
- Copy the entire content of the public key.
- Gain access to your remote server. Either using password-based SSH such as
ssh username@remote_server_ip
or by accessing it on premise (with the public key file at hand). - Create an
authorized_keys
file if it doesn’t exist:
mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys
- Open the
authorized_keys
file in a text editor, such asnano
orvim (advanced users)
- Paste the copied public key content into this file.
- Set appropriate permissions for
.ssh
directory andauthorized_keys
file:
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys
- Optionally (but highly recommended), disable password authentication to enforce key-based authentication (after confirming the key-based login works):
sudo nano /etc/ssh/sshd_config
- Find the line
PasswordAuthentication
and set it tono
. - Save the file and restart SSH:
sudo systemctl restart sshd
Step 3: Testing the Connection
- Open a new terminal window on your local machine.
- Attempt to connect to the remote server using SSH:
ssh username@remote_server_ip
You should be logged in without being prompted for a password, confirming successful key-based authentication.
Conclusion
Generating and adding an SSH key pair to your remote server enhances security by providing a secure method of authentication. Key-based authentication eliminates the need for passwords, offering a more secure and convenient way to access your server remotely.
With these steps, you can confidently generate an SSH key pair and securely add it to your remote server for seamless and secure access.