Using SSH keys instead of traditional passwords is one of the best ways to secure your server. SSH keys offer stronger encryption, eliminate the risks of brute-force attacks, and make it easier to manage automated scripts or secure user access.
In this tutorial, we’ll show you step-by-step how to generate, install, and use SSH keys on your Linux server.
✅ Why Use SSH Keys Instead of Passwords?
Here’s why switching to SSH keys is a smart move:
- ✅ Stronger Security: 2048-bit or 4096-bit SSH keys are almost impossible to brute-force.
- ✅ No Password Guessing: SSH keys eliminate password-based attacks.
- ✅ Easier Automation: Perfect for servers running scripts, cronjobs, or backups.
- ✅ Two-Factor-Like Protection: Optionally add a password to your private key for double protection.
🧰 What You’ll Need
- A Linux server with SSH access
- A local computer (Windows, macOS, or Linux)
- Terminal access or an SSH client (like PuTTY)
👉 Need a secure VPS?
Check out high-performance VPS servers at Servers9
🔹 Step 1: Generate SSH Key Pair
On your local machine:
ssh-keygen
You’ll be asked:
- File to save the key: (Press Enter to accept default
~/.ssh/id_rsa
) - Passphrase: (Optional, but recommended for extra security)
This generates:
- A private key:
~/.ssh/id_rsa
- A public key:
~/.ssh/id_rsa.pub
Important: Keep your private key safe and never share it!
🔹 Step 2: Copy Public Key to Server
Option 1: Using ssh-copy-id
(easiest)
ssh-copy-id -i ~/.ssh/id_rsa.pub user@your-server-ip
Replace user
with your username (often root
) and your-server-ip
with your server’s IP.
This automatically appends your public key to the server’s authorized list.
Option 2: Manual Method
If ssh-copy-id
isn’t available:
- SSH into the server with your password: bashCopyEdit
ssh user@your-server-ip
- Create the
.ssh
directory and file: bashCopyEditmkdir -p ~/.ssh nano ~/.ssh/authorized_keys
- Paste your public key (
id_rsa.pub
) content intoauthorized_keys
. - Set proper permissions: bashCopyEdit
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
- Exit.
🔹 Step 3: Test SSH Key Login
On your local machine:
ssh user@your-server-ip
✅ If configured correctly, it should log in without asking for a password.
If you set a passphrase during key generation, it will ask for the key passphrase (not the server password).
🔒 Step 4: (Recommended) Disable Password Authentication
Once you confirm SSH key login is working:
- SSH into your server (with key-based login).
- Edit the SSH configuration:
nano /etc/ssh/sshd_config
Find and change:
PasswordAuthentication no
ChallengeResponseAuthentication no
Restart SSH:
systemctl restart sshd
✅ Now only SSH keys can access your server — password logins are disabled!
🛡️ Bonus Tips for SSH Security
- ✅ Use 4096-bit keys for even stronger encryption:
ssh-keygen -b 4096
- ✅ Backup your private keys securely
- ✅ Rotate keys periodically
- ✅ Use Fail2Ban or CSF to block failed SSH attempts
📌 Common SSH Key Commands Cheat Sheet
Command | Purpose |
---|---|
ssh-keygen | Generate a new SSH key pair |
ssh-copy-id | Copy your public key to server |
ssh-add | Add key to SSH agent (for auto-login) |
ssh -i /path/to/key user@host | Connect using a specific private key |
🚀 Ready to Lock Down Your Server?
👉 Get a pre-secured VPS with SSH keys, CSF, and Fail2Ban at Servers9