how to set up key based authentication for ssh

Secure SSH Access: Step-by-Step Guide to Key-Based Authentication Setup

By Joseph Wilson
AD

Secure Your SSH Connections: A Step-by-Step Guide to Key-Based Authentication

In today's digital landscape, securing your SSH (Secure Shell) connections is paramount. While password-based authentication is common, it's not the most secure method. Key-based authentication, on the other hand, provides an extra layer of security and convenience. Here's a comprehensive guide on how to set it up.

Understanding Key-Based Authentication

Key-based authentication uses a pair of keys - a public key and a private key - to authenticate users. The public key is placed on the remote server, while the private key is kept on the client side. When you try to connect to the server, it sends a challenge to your client, which you sign with your private key. The server then verifies this signature using your public key. If the signature is valid, you're granted access.

Generating SSH Key Pair

Before you start, ensure you have OpenSSH installed on your client machine. If not, install it using the following command:

AD
How To Authenticate Over SSH With Keys Instead of Passwords
How To Authenticate Over SSH With Keys Instead of Passwords
How To Authenticate Over SSH With Keys Instead of Passwords
How To Authenticate Over SSH With Keys Instead of Passwords
a table with two different types of text on it and the words ssh pentesting
a table with two different types of text on it and the words ssh pentesting

sudo apt-get install openssh-client (for Ubuntu/Debian)

Now, generate your key pair using the following command:

SSH Key-Based Authentication on webMethods Active Transfer
SSH Key-Based Authentication on webMethods Active Transfer
How to Create an SSH Key in Linux: Easy Step-by-Step Guide | DigitalOcean
How to Create an SSH Key in Linux: Easy Step-by-Step Guide | DigitalOcean
Secure SSH Access with Passwordless Login (Step-by-Step Linux Guide)
Secure SSH Access with Passwordless Login (Step-by-Step Linux Guide)

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

You'll be prompted to enter a file location to save the key and a passphrase for extra security. If you're happy with the default location (usually ~/.ssh/id_rsa), just press Enter. If you want to add a passphrase, enter one and confirm it.

AD

Copying the Public Key to the Remote Server

Now that you have your key pair, you need to copy the public key to the remote server. You can do this manually or use the ssh-copy-id utility. Here's how to use both methods:

SSH Key Management Linux
SSH Key Management Linux
Using SSH on Linux VPS Servers: Google Web Story!
Using SSH on Linux VPS Servers: Google Web Story!
networking ports
networking ports
🔒 Secure SSH on Arch Linux: Ultimate Guide 🐧
🔒 Secure SSH on Arch Linux: Ultimate Guide 🐧
Set of icons with different passwordless authentication methods and login checks including
Set of icons with different passwordless authentication methods and login checks including
Copy a Key in 20 seconds!!
Copy a Key in 20 seconds!!

Manual Method

1. Open the ~/.ssh/authorized_keys file on the remote server in a text editor. If the file doesn't exist, create it.

2. Append your public key (the contents of ~/.ssh/id_rsa.pub) to the file. Ensure there's no extra whitespace or newline characters.

AD

Using ssh-copy-id

This method is simpler and less error-prone. Just run the following command, replacing user@example.com with your username and server's address:

ESET Internet Security Key 2026 — Activation & Buyer's Guide
ESET Internet Security Key 2026 — Activation & Buyer's Guide
How to Build Protected Routes in Next.js (JWT Authentication Guide)
How to Build Protected Routes in Next.js (JWT Authentication Guide)
the instructions for how to use function keys
the instructions for how to use function keys
Protect your Discord account with a Security Key - gHacks Tech News
Protect your Discord account with a Security Key - gHacks Tech News
a golden key sitting on top of a metal puzzle
a golden key sitting on top of a metal puzzle
Beginner Mistakes in Exchange Login Safety
Beginner Mistakes in Exchange Login Safety
Leaked keys from GitHub
Leaked keys from GitHub
How to Secure APIs in Node.js (JWT + Rate Limiting + Validation)
How to Secure APIs in Node.js (JWT + Rate Limiting + Validation)
many different types of padlocks and keys on a black background
many different types of padlocks and keys on a black background
How To Open A Key Safe Without Code
How To Open A Key Safe Without Code
a key with the word secret attached to it
a key with the word secret attached to it
🛡️ Safe Game Key Sites List 2026: Legit or Scam?
🛡️ Safe Game Key Sites List 2026: Legit or Scam?
ISC2 CC : Lesson 13 Security Operations Center (SOC) | Cybersecurity Beginner Notes
ISC2 CC : Lesson 13 Security Operations Center (SOC) | Cybersecurity Beginner Notes
Lock Change or Re-key
Lock Change or Re-key

ssh-copy-id user@example.com

You'll be prompted to enter your password for the remote server. After that, your public key will be appended to the ~/.ssh/authorized_keys file.

AD

Configuring SSH to Use the Key

By default, SSH looks for your keys in the ~/.ssh directory. However, you can specify a different location in your SSH configuration file (~/.ssh/config). Here's an example of how to do this:

Host * IdentityFile ~/.ssh/id_rsa

This tells SSH to use your id_rsa key for all hosts.

Testing Key-Based Authentication

Now that everything is set up, it's time to test your key-based authentication. Try connecting to your remote server using SSH:

ssh user@example.com

If everything is configured correctly, you should be connected to the server without being prompted for a password.

Disabling Password Authentication

For added security, you can disable password authentication on your server. Edit the SSH configuration file (/etc/ssh/sshd_config) and change the following lines:

Before After
PasswordAuthentication yes PasswordAuthentication no
ChallengeResponseAuthentication yes ChallengeResponseAuthentication no

After making these changes, restart the SSH service for them to take effect:

sudo systemctl restart ssh

Now, password authentication is disabled, and SSH will only accept key-based authentication.

That's it! You've successfully set up key-based authentication for SSH. This method provides stronger security and more convenience than password-based authentication. Always remember to keep your private key safe and never share it with anyone.

AD