Tip & Trick
To linux add a public key to your authorized_keys file marks the start of password-free SSH access—something I still remember setting up for the first time back in my uncle’s basement workshop in 1998.
That single command saved me hours of typing passwords every time I logged into a remote server. The process hasn’t changed much since then, and it’s shockingly simple once you know where to look.
You’ll need three things: a public key (usually ending in .pub), SSH access to the target machine, and permission to edit the ~/.ssh/authorized</em>keys file. The key itself likely lives on your local machine—maybe in ~/.ssh/id<em>rsa.pub—while the authorized</em>keys file sits on the server you want to access.
Think of it like a digital skeleton key: once installed, you’ll log in instantly without passwords.
Here’s where it gets interesting: the actual setup takes under 30 seconds. You’ll use cat to append the key to the file, then chmod to lock down permissions—no fancy tools, just terminal commands.
I’ve done this hundreds of times, and the only thing that trips people up is forgetting to set the right file permissions. We’ll cover that (and other gotchas) in the next section.
This method works for any Linux server, from a cloud VM to a Raspberry Pi in your living room. Once configured, you’ll never type ssh user@host with a password again. Let’s make it happen—step by step, with no surprises.
📚 In This Guide
- What you need
- Instructions
- Tips and common mistakes
- Wrapping up and next steps
What you need
- ● A Linux system (Ubuntu, Debian, CentOS, etc.) with: SSH server installed (OpenSSH by default on most distros)
- ● Root or sudo access (to modify system files)
- ● A public SSH key (e.g., idrsa.pub or id_ed25519.pub) generated from your local machine (Windows, macOS, or Linux).
- ● A text editor (pre-installed or install one like nano, vim, or gedit).
- ● Terminal access (to run commands on both local and remote machines).
- ● SSH key generator (e.g., ssh-keygen if you haven’t created a key pair yet).
- ● File transfer tool (e.g., scp or rsync) to copy the public key remotely.
- ● Key management tool (like ssh-add) for caching private keys locally.
Step-by-Step instructions for adding a public key to authorizedKeys
Here's the straightforward process I use to set up secure SSH access with minimal fuss.
🔧 Step 1: Locate and Prepare the AuthorizedKeys Directory
Start by opening a terminal window on your Linux system. You'll need to navigate to the directory where SSH stores authorized public keys. The path is typically ~/.ssh/, so type cd ~/.ssh and press Enter. If the .ssh directory doesn't exist, create it by running mkdir ~/.ssh.
Next, verify the directory has the correct permissions. Run ls -ld ~/.ssh to check the permissions. They should be set to 700 (drwx------). If not, fix them with chmod 700 ~/.ssh. This ensures only your user can access the directory, maintaining security.
💻 Step 2: Create or Edit the AuthorizedKeys File
Now, you'll create or edit the authorizedkeys file. Run touch ~/.ssh/authorizedkeys to create the file if it doesn't exist. Then, open it in a text editor with nano ~/.ssh/authorizedkeys (or use vim if you prefer).
If the file already exists, you can simply append the new public key to the end. If it's empty, you'll add the key directly. Here's the thing—make sure to paste the entire public key, including the ssh-rsa or ecdsa-sha2-nistp256 header and the base64-encoded data. A single missing character can break the connection.
⌨️ Step 3: Add the Public Key and Set Proper Permissions
Once you've pasted the public key into the authorizedkeys file, save it and exit the editor. For nano, press Ctrl+O to save, then Ctrl+X to exit. Now, set the correct permissions on the file with chmod 600 ~/.ssh/authorizedkeys. This ensures only you can read and write to the file, which is crucial for security.
Verify the permissions again with ls -l ~/.ssh/authorized_keys. You should see -rw------- next to the file. If the permissions aren't correct, SSH will reject the key and you'll get connection errors. Double-check your work here—it's the most critical step.
💡 Step 4: Test the SSH Connection
With the key added, it's time to test your setup. Open a new terminal window and attempt to connect via SSH using ssh username@hostname. Replace username with your actual username and hostname with the server's IP or domain.
If everything is set up correctly, you should connect without being prompted for a password. You'll see something like Welcome to Ubuntu or similar, indicating a successful key-based login. If you're still prompted for a password, double-check the key was pasted correctly and the permissions are set to 600.
Tips & tricks for secure SSH key setup
Setting up SSH keys should be straightforward, but these small details make all the difference between a seamless experience and hours of troubleshooting.
Directory Permissions Are Non-Negotiable: When you create the ~/.ssh directory in Step 1, those 700 permissions aren't just recommended—they're essential. I've seen systems where SSH silently fails because someone used 755 instead. Think of it like a vault door: if anyone else can peek inside, your security is compromised. Double-check with ls -ld ~/.ssh after setting permissions.
Key Paste Precision Matters: In Step 2, when you're pasting that public key, resist the urge to eyeball it. A single missing character—especially in that base64-encoded section—will make SSH reject your key. My trick? Copy the key from your terminal first, then paste it into the editor. This avoids hidden formatting issues from web interfaces or email clients. And for heaven's sake, include the full header like ssh-rsa—don't strip it out.
File Permissions Are Your Last Line of Defense: The 600 permissions on authorizedkeys in Step 3 exist for a reason: they prevent even other users on your system from reading your private keys. I've had students accidentally set 644 and wonder why their SSH connections kept failing. Run ls -l ~/.ssh/authorizedkeys after setting permissions—you should see -rw-------. If not, SSH will reject the key without explanation, leaving you scratching your head.
Test Connection Before Assuming Success: That final test in Step 4 isn't just a formality. I've seen users connect once successfully, then later get password prompts because they forgot to verify the key worked with multiple SSH sessions. Always test with a fresh terminal window—sometimes environment variables from previous sessions can mask permission issues. If you're still getting password prompts, start over: check permissions again, verify the key was pasted correctly, and consider using ssh -v username@hostname for verbose output.
Pro Tips for Linux Add Public Key To Authorized_keys
- Setting up SSH keys should be straightforward, but these small details make all the difference between a seamless experience and hours of troubleshooting.
- Directory Permissions Are Non-Negotiable: When you create the ~/.ssh directory in Step 1, those 700 permissions aren't just recommended—they're essential.
- Key Paste Precision Matters: In Step 2, when you're pasting that public key, resist the urge to eyeball it.
Frequently asked questions
about adding a public key toauthorizedkeys—because even the simplest steps can raise questions! Here are answers to the most common ones.
Why does my SSH key still prompt for a password after adding it to authorizedkeys?
If you’re still seeing password prompts, double-check these steps:
- Permissions: Ensure
~/.ssh/authorizedkeyshas600permissions (chmod 600 ~/.ssh/authorizedkeys). - Correct key: Verify the public key matches the private key you’re using (e.g.,
ssh -i ~/.ssh/yourkey). - Server config: Confirm
PubkeyAuthenticationis set toyesin/etc/ssh/sshdconfig.
sudo systemctl restart sshd afterward.
How long does it take for the key to work after adding it?
It’s instant—but only if you’ve already connected once or the server trusts your client. If it’s a fresh setup, SSH may cache old settings. Try:
- Disconnect and reconnect.
- Clear SSH known hosts:
ssh-keygen -R [hostname]. - Restart the SSH service (
sudo systemctl restart sshd).
Can I add multiple public keys to authorizedkeys?
Just append each public key on a new line in the file. Example:
ssh-rsa AAAAB3NzaC1yc2E... user1@laptop
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5... user2@phone
Each key will work independently. Useful for managing access from different devices!
What if I accidentally delete or overwrite authorizedkeys?
Don’t panic! Here’s how to recover:
- Backup first: Always keep a copy of your keys in a safe location.
- Re-add keys: Copy your public keys from
~/.ssh/id*.pub(or another trusted source) back toauthorizedkeys. - Permissions: Reset to
600(chmod 600 ~/.ssh/authorizedkeys).
Is there a safer alternative to editing authorizedkeys manually?
Yes! Use ssh-copy-id for a foolproof method:
ssh-copy-id -i ~/.ssh/id_rsa.pub user@hostname
This tool handles permissions and file placement automatically. For advanced setups, consider ssh-agent or hardware keys (like YubiKey) for extra security.
Wrapping up and next steps
Adding your public key to authorized_keys is a game-changer for secure SSH access—no more passwords, just seamless, encrypted logins! 🔐 By following these steps, you’ve not only boosted security but also simplified your workflow.
Now that you’re set up, why not explore advanced SSH configurations, like setting up key-based authentication for root or automating backups with rsync?
Your Linux journey just got easier—keep experimenting and mastering these tools! 🚀
