Operating System
Linux add public key to authorized_keys—that first step toward password-free SSH logins—still feels like magic to me, no matter how many times I’ve done it, especially when I set it up in my uncle’s basement. ✨ workshop in 1998, watching the terminal light up after generating my first key pair.
That moment changed how I work forever.
The process hinges on three commands that create a secure handshake between your local machine and the server.
First, generate your key pair with ssh-keygen -t ed25519 (or -t rsa if needed), then append the public key to ~/.ssh/authorizedkeys on the target server, and finally set permissions with chmod 600 ~/.ssh/authorized_keys.
Most permission errors vanish once you nail these steps—trust me, I’ve debugged this a dozen times.
Once configured, you’ll log in with just ssh username@server, and the system will silently verify your identity. This isn’t just faster—it’s more secure, as keys are harder to brute-force than passwords.
The setup takes under five minutes once you know the flow, and I’ll walk you through every step, including troubleshooting common hiccups like missing directories or permission denials.
We’ll also cover disabling password authentication after setup (a security best practice) and verifying your key works before cutting the old method. Ready to make SSH feel like second nature? Let’s get started with the commands that’ll transform how you access remote machines.
📚 In This Guide
- What you need
- Instructions
- Tips and common mistakes
- Wrapping up and next steps
What you need
- ● Linux System: Any modern distribution (Ubuntu, Debian, CentOS, Fedora, etc.) with SSH access.
- ● SSH Key Pair: A pre-generated RSA or Ed25519 key pair (public + private key). If you don’t have one, generate it with: ssh-keygen -t ed25519 -C "youremail@example.com" (or -t rsa -b 4096 for RSA).
- ● Terminal Access: Command-line access (local or remote) to both the client and server.
- ● Text Editor: nano, vim, or gedit for editing files.
- ● Permissions: Admin/sudo access to modify the ~/.ssh/ directory on the target server.
- ● SSH Agent: To manage keys securely (run eval "$(ssh-agent -s)" and add your key with ssh-add ~/.ssh/id_ed25519).
- ● Key Backup: A secure location (e.g., encrypted USB or password manager) to store your private key.
- ● Key Comment: A descriptive comment in your public key (e.g., -C "laptop-2024") for easier tracking.
Step-by-Step instructions for configuring SSH key authentication
Here's how to set up secure SSH access in minutes—no complex steps required.
🔧 Step 1: Generate and Copy the SSH Key Pair
Open a terminal and run ssh-keygen -t ed25519 to generate a new key pair. This creates both a private key (stay on your local machine) and a public key (what you'll add to the server). I always use ed25519 for its balance of security and performance—it's the modern standard.
Press Enter to accept the default file location when prompted. You won't need a passphrase for automated servers, but I recommend one for personal machines. When asked for a passphrase, either leave it blank or enter a strong one. The key pair will be saved in ~/.ssh/ided25519 and ~/.ssh/ided25519.pub.
⌨️ Step 2: Transfer the Public Key to the Server
Use ssh-copy-id -i ~/.ssh/ided25519.pub user@serverip to copy the public key to the server. Replace user with your username and serverip with the actual IP address or hostname. This command handles the transfer securely and appends the key to ~/.ssh/authorizedkeys automatically.
If ssh-copy-id isn't available, manually append the key by running echo "yourpublickeyhere" >> ~/.ssh/authorizedkeys on the server. Make sure the file permissions are set correctly—run chmod 600 ~/.ssh/authorizedkeys afterward. The ~/.ssh directory itself needs 700 permissions.
💡 Step 3: Verify and Test the SSH Connection
Switch to the server and verify the key was added correctly by running cat ~/.ssh/authorizedkeys. You should see your public key listed. If the ~/.ssh directory doesn't exist, create it with mkdir ~/.ssh and set permissions first.
Test the connection by running ssh user@serverip from your local machine. If everything is set up correctly, you should log in without being prompted for a password. If you still get a password prompt, double-check that the key was added to authorizedkeys and that the permissions are correct.
⏰ Step 4: Secure the SSH Configuration (Optional but Recommended)
Edit the SSH server configuration file with sudo nano /etc/ssh/sshd_config and add or modify these lines: PubkeyAuthentication yes, PasswordAuthentication no, and ChallengeResponseAuthentication no. This ensures only key-based authentication is allowed, adding an extra layer of security.
Restart the SSH service with sudo systemctl restart sshd to apply the changes. Now your server is fully protected against brute-force attacks. I always enable fail2ban alongside this for even stronger defense, but that's an extra step for another guide.
Tips & tricks for secure SSH key authentication
These simple but powerful tips will help you avoid common pitfalls and get key-based SSH authentication working like a pro—no headaches included.
Key Generation Best Practice: When running ssh-keygen -t ed25519 in Step 1, resist the urge to use a passphrase for automated servers. While passphrases add security, they create unnecessary friction for scripts and CI/CD pipelines. For personal machines, use a strong passphrase with at least 12 characters—mix uppercase, lowercase, numbers, and symbols. I've seen too many admins use "password123" as their passphrase—don't be that person.
Permissions Are Everything: The most common failure point in Step 2 happens with file permissions. After manually adding your key to authorizedkeys, run chmod 600 ~/.ssh/authorizedkeys and chmod 700 ~/.ssh—these exact permissions are non-negotiable. Too-permissive settings (like 644) will cause SSH to reject your key silently. Pro tip: Create a quick script to verify permissions with ls -la ~/.ssh after each change.
Troubleshooting Connection Issues: If you still get password prompts after Step 3, the issue is almost always permissions or missing keys. First verify the key exists with cat ~/.ssh/authorizedkeys on the server. Then check if SSH is actually using the key by running ssh -v user@serverip—look for "Offering public key" in the verbose output. If you see "no such identity," your key isn't in the agent. Add it with ssh-add ~/.ssh/ided25519.
Optional Security Hardening: For Step 4, consider adding PermitRootLogin no to your sshdconfig if you don't need root access. This prevents even key-authenticated users from getting root privileges. I also recommend adding LoginGraceTime 2m to limit brute-force attempts. After making changes, always test with ssh user@serverip before restarting the service—no sense locking yourself out!
Pro Tips for Linux Add Public Key To Authorized_keys
- These simple but powerful tips will help you avoid common pitfalls and get key-based SSH authentication working like a pro—no headaches included.
- Key Generation Best Practice: When running ssh-keygen -t ed25519 in Step 1, resist the urge to use a passphrase for automated servers.
- Permissions Are Everything: The most common failure point in Step 2 happens with file permissions.
Frequently asked questions
Got questions about adding public keys to authorizedkeys? You’re not alone! Here are answers to the most common queries to keep your SSH access smooth and secure.
Why do I need to add my public key to authorizedkeys?
Adding your public key to authorizedkeys enables passwordless SSH login, boosting security and convenience. Without it, you’ll need to enter a password every time—risking exposure or lockouts. Think of it as a digital keycard for your server!
What if I accidentally overwrite authorizedkeys?
No worries! Back up the file first with cp ~/.ssh/authorizedkeys ~/.ssh/authorizedkeys.bak. If you mess up, restore it with cp ~/.ssh/authorizedkeys.bak ~/.ssh/authorizedkeys. Always double-check permissions afterward (chmod 600 ~/.ssh/authorizedkeys).
Can I add multiple public keys to authorizedkeys?
Just append new keys to the file using cat newkey.pub >> ~/.ssh/authorizedkeys. Each key should be on its own line. This lets you switch identities (e.g., personal vs. work access) without password prompts.
Why am I still getting a password prompt after adding the key?
Double-check these steps:
- Permissions: Ensure
~/.sshis700andauthorizedkeysis600. - Key format: Verify the key is public (ends with
.pub) and correctly pasted. - SSH config: Confirm
PubkeyAuthentication yesin/etc/ssh/sshdconfigand restart SSH (sudo systemctl restart sshd).
ssh -v user@host for verbose debugging.
Is there a safer alternative to manually editing authorizedkeys?
Yes! Use ssh-copy-id for a foolproof method:
ssh-copy-id -i ~/.ssh/idrsa.pub user@host
This tool handles permissions and appends keys automatically. For automation, scripts or tools like Ansible can also manage authorized_keys securely across servers.
Wrapping up and next steps
Adding your public key to authorizedkeys is a game-changer for secure SSH access—no more password prompts, just seamless, encrypted logins! 🔐 With just a few commands, you’ve fortified your server against brute-force attacks and streamlined workflows.
Now that you’re all set, why not level up? Explore SSH config tweaks or set up key-based authentication for multiple servers—your future self will thank you! 🚀
