Linux Add Public Key to Authorized_Keys: SSH Access Made Simple

Troubleshooting

Linux Add Public Key to Authorized_Keys: SSH Access Made Simple

Learning how to linux add a public key to the authorized_keys file saved me from typing passwords every time I SSH into my servers—until I hit a permissions wall. ✨ The first time I set this up, I spent an hour fighting permissions—until I realized I... needed to set those file modes just right.

That single fix unlocked seamless, passwordless logins that changed how I manage remote machines.

You’ll need three things: a public key (generate one with ssh-keygen if you don’t have it), a Linux server to connect to, and basic terminal comfort. The process itself is straightforward—copy the key to the server, append it to ~/.ssh/authorizedkeys, and set permissions with chmod 600.

I’ve done this on dozens of servers, and the same steps work every time, whether you’re using Ubuntu, CentOS, or even a Raspberry Pi.

Once set up, you’ll log in without passwords, eliminating the risk of brute-force attacks on your SSH port. The key file itself is just a text file—no fancy encryption needed—so you can even edit it with nano if you prefer.

Just remember: back up that file before you make changes, because a missing or corrupted key means locked-out access until you fix it.

Common mistakes? Forgetting to set the right permissions (which causes "Permission denied") or using the wrong key format (like a private key instead of public). I’ve seen both errors more times than I’d like to admit.

The fix is always the same: double-check the file with cat ~/.ssh/authorized_keys and verify those permissions with ls -la ~/.ssh/. Trust me—this step saves headaches later.

📚 In This Guide

  • What you need
  • Instructions
  • Tips and common mistakes
  • Wrapping up and next steps

What you need

🛠 Materials & Tools
  • ● A Linux system (Ubuntu, Debian, CentOS, Fedora, or any other distro with SSH support).
  • ● SSH access to the target server (via terminal or SSH client like PuTTY, Terminal, or iTerm).
  • ● A public key generated from your local machine (e.g., using ssh-keygen or tools like blank">PuTTYgen).
  • ● Root or sudo privileges on the target server to modify ~/.ssh/authorizedkeys.
  • ● A text editor (like nano, vim, or gedit) to edit files.
  • ● A backup of the original authorizedkeys file (just in case!).
  • ● SSH config file (~/.ssh/config) for streamlining future connections.
  • ● Key permissions checker (e.g., chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys) to ensure security.
  • ○ Key fingerprint verification (optional but recommended for security).

Step-by-Step instructions for securing SSH access with authorizedKeys

Here's the straightforward method I use to add a public key to your Linux system's SSH configuration.

1

🔧 Step 1: Locate or Generate Your SSH Key Pair

First, confirm you have a public-private key pair. If you haven't created one yet, open a terminal and run ssh-keygen -t ed25519 (or ssh-keygen -t rsa -b 4096 for RSA). This creates your key pair in ~/.ssh/ with default filenames ided25519 and ided25519.pub.

If you already have keys, verify they exist by running ls -l ~/.ssh/. You'll see your private key (id) and public key (id.pub). The public key is what you'll add to the server's authorizedkeys file.

2

💻 Step 2: Copy Your Public Key to the Remote Server

Use the ssh-copy-id command to automatically append your public key to the server's authorizedkeys file. Run ssh-copy-id -i ~/.ssh/ided25519.pub username@serverip, replacing username and serverip with your actual credentials. This command handles the entire transfer process securely.

If ssh-copy-id isn't available, manually copy the key by running cat ~/.ssh/ided25519.pub and selecting the output, then paste it into the remote server's ~/.ssh/authorizedkeys file using a text editor like nano or vim.

3

⌨️ Step 3: Set Proper Permissions on the Remote Server

After transferring the key, ensure the authorizedkeys file has the correct permissions. On the remote server, run chmod 600 ~/.ssh/authorizedkeys to restrict access. Then verify the .ssh directory permissions with chmod 700 ~/.ssh—this prevents other users from viewing your SSH configuration.

Here's the thing—if permissions are wrong, SSH will reject your key even if it's correctly added. Double-check with ls -la ~/.ssh/ to confirm the directory and file permissions match these exact values: drwx------ for the directory and -rw------- for the file.

4

💡 Step 4: Test Your SSH Connection with Key Authentication

Disconnect from the server and attempt to log in again using ssh username@serverip. The system should now authenticate you using your public key without prompting for a password. If you're still asked for a password, verify the key was added correctly and permissions are set properly.

For troubleshooting, check the SSH daemon logs on the server with sudo tail -f /var/log/auth.log. Look for errors related to authorizedkeys or publickey authentication. If you see Permission denied (publickey), revisit the permissions step—this is the most common issue.

Tips & tricks for secure SSH key setup

Setting up SSH keys properly is the foundation of secure remote access—here's how to avoid the most common pitfalls while keeping things simple.

Key Generation Best Practices: When running ssh-keygen in Step 1, I always use the ed25519 algorithm if available—it offers better security than RSA with the same key size. If you must use RSA, stick with 4096-bit keys for maximum protection. Never use the default settings without a passphrase unless this is a truly disposable key for testing environments. The passphrase adds an extra layer of protection if someone gains access to your private key file.

Manual Key Transfer Safety: In Step 2, if you're manually copying your public key, I recommend using scp to securely transfer it first: scp ~/.ssh/ided25519.pub username@serverip:/tmp/. Then use cat /tmp/ided25519.pub >> ~/.ssh/authorizedkeys on the server. This prevents accidental exposure of your key during transfer. Always verify the key's fingerprint matches your local key before adding it to the server.

Permissions Troubleshooting: Step 3's permissions are critical—600 for the authorizedkeys file and 700 for the .ssh directory. Here's my fail-safe check: After setting permissions, run find ~/.ssh -type f -exec chmod 600 {} \; and find ~/.ssh -type d -exec chmod 700 {} \;. This ensures all files and subdirectories maintain proper security. If you see any files with 644 permissions, SSH will silently ignore your key.

Testing Connection Strategy: Before testing in Step 4, I always run ssh -v username@serverip with the verbose flag. This shows exactly where the authentication process fails if you're still prompted for a password. Look for lines containing "Offering public key" and "Server refused our key" to identify issues. If you see "debug1: Authentications that can continue: publickey,password", your key wasn't properly loaded.

💡

Pro Tips for Linux Add Public Key To Authorized_keys

  • Setting up SSH keys properly is the foundation of secure remote access—here's how to avoid the most common pitfalls while keeping things simple.
  • Key Generation Best Practices: When running ssh-keygen in Step 1, I always use the ed25519 algorithm if available—it offers better security than RSA with the same key size.
  • Manual Key Transfer Safety: In Step 2, if you're manually copying your public key, I recommend using scp to securely transfer it first: scp ~/.ssh/ided25519.pub username@serverip:/tmp/.

Frequently asked questions

Got questions about adding public keys to authorizedkeys? You’re not alone! Here are some common concerns and quick solutions to keep your SSH setup smooth as butter.

1

Why can’t I log in after adding my public key?

If SSH still asks for a password, double-check these steps:

  • Permissions: Ensure ~/.ssh/authorizedkeys has 600 permissions (chmod 600 ~/.ssh/authorizedkeys).
  • File ownership: The file must belong to your user (chown $USER:$USER ~/.ssh/authorizedkeys).
  • Key format: Verify the public key isn’t truncated or corrupted. Try copying it fresh from ~/.ssh/idrsa.pub.
Restart the SSH service (sudo systemctl restart sshd) if needed.
2

How long does it take for the key to work?

It’s instant! Once you’ve added the key and set permissions correctly, you should log in passwordlessly on your next attempt. If it doesn’t work immediately, check for typos in the key or network issues (e.g., firewall blocking port 22).

3

Can I add multiple public keys to authorizedkeys?

Just append each public key on a new line in the authorizedkeys file. For example:

ssh-rsa AAAAB3NzaC1yc2E... user1@laptop
ssh-rsa BBBBC3NzaC1yc2E... user2@phone
This lets you switch between devices seamlessly. Useful for work/life setups!
4

What if I accidentally delete or overwrite authorizedkeys?

Don’t panic! If you lose the file, recreate it with:

  1. Generate a new key pair if needed (ssh-keygen -t ed25519).
  2. Add the public key back to authorizedkeys.
  3. Set permissions to 600.
If you’re locked out, use a recovery console or sudo -i to fix it.
5

Is there a safer alternative to authorizedkeys?

For stricter security, consider:

  • SSH Certificate Authority (CA): Uses signed certificates instead of static keys (advanced setup).
  • Hardware Keys (YubiKey): Physical devices add a layer of protection beyond software keys.
  • Fail2Ban: Pair with this tool to block brute-force attacks on your SSH port.
For most users, authorized_keys is perfectly secure if managed carefully!

Wrapping up and next steps

Adding your public key to the authorizedkeys file is a game-changer for secure SSH access on Linux! This simple step eliminates password prompts, enhances security, and streamlines remote management—whether you're a sysadmin or a curious learner. 🚀

Now that you’ve mastered the process, why not level up further? Try automating key-based authentication for multiple servers or explore SSH config tweaks to customize your workflow. Your future self will thank you!

★★★★★5.0(14 reviews)
Categories Troubleshooting