Linux Add Public Key to Authorized_Keys: Secure SSH Access in 3 Steps

Troubleshooting

Linux Add Public Key to Authorized_Keys: Secure SSH Access in 3 Steps

Adding a public key to your Linux authorizedkeys file is the first step toward password-free SSH access—something I still remember debugging at 3 AM during my first server setup in Pittsburgh.

The process is straightforward once you know the exact commands and file permissions to avoid those frustrating "Permission denied" errors that waste hours.

You’ll need the public key (usually ending in .pub) from your client machine, a Linux server with SSH access, and about five minutes of focused terminal time. The key goes in ~/.ssh/authorizedkeys, and permissions must be set to 600—anything looser, and SSH will reject it.

I’ve seen teams spend days chasing permission issues they could’ve fixed with one chmod command.

Once configured, you’ll log in silently, without typing passwords, and your keys will work across Linux, macOS, and even WSL. The setup is shockingly simple after the first time—though I still double-check permissions every time, just like my dad taught me to verify solder joints when rebuilding those old IBMs.

We’ll cover the exact commands, troubleshooting permission errors, and how to verify your key works. The only tools you’ll need are ssh-keygen, cat, and chmod—no fancy software required. Let’s make that SSH connection seamless.

📚 In This Guide

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

What you need

🛠 Materials & Tools
  • ● Linux machine (any distro: Ubuntu, Debian, CentOS, etc.) with ssh installed.
  • ● SSH key pair (public + private key) generated via ssh-keygen (or pre-existing).
  • ● Terminal access (local or remote) with sudo privileges for the target user.
  • ● Text editor (e.g., nano, vim, or gedit) for editing authorizedkeys.
  • ● SSH client (e.g., PuTTY on Windows or Terminal on macOS/Linux).
  • ● Key management tool (e.g., ssh-add for caching keys or GnuPG for encrypted keys).
  • ● File transfer tool (e.g., scp or rsync) to move keys remotely.

Instructions for adding a public key to authorizedKeys on Linux

Here’s how I set up secure SSH access in minutes—no complex commands, just clear steps.

1

Locate the AuthorizedKeys Directory

First, navigate to the directory where SSH stores authorized public keys. On most Linux distributions, this is located in the user’s home folder under ~/.ssh/. If the .ssh directory doesn’t exist, create it with the command: mkdir ~/.ssh.

Next, verify the directory permissions are set correctly. Run chmod 700 ~/.ssh to ensure only the user can access it. This prevents unauthorized users from viewing or modifying the keys. If you skip this, SSH may reject the key during authentication.

2

Create or Edit the AuthorizedKeys File

Inside the .ssh directory, create or edit the authorizedkeys file using a text editor like nano or vim. For example, run nano ~/.ssh/authorizedkeys to open the file. If the file doesn’t exist, it will be created automatically.

Paste the public key you want to authorize into this file. The key should be in the format: ssh-rsa AAAAB3NzaC1yc2E... user@host. Ensure there are no extra spaces or line breaks before or after the key—SSH will reject malformed entries. Save the file and exit the editor.

3

Set Proper Permissions and Test SSH Access

After adding the key, set the correct permissions on the authorizedkeys file using chmod 600 ~/.ssh/authorized_keys. This ensures only the user can read and write to the file, maintaining security. If permissions are too loose, SSH will refuse to use the key.

Now, test the setup by attempting to SSH into the machine from another terminal. Use the command: ssh username@hostname, replacing username and hostname with your actual credentials. If configured correctly, you should log in without a password prompt.

4

Verify Key Functionality (Optional but Recommended)

To confirm the key is working, check the SSH logs for successful authentication. Run grep "Accepted publickey" /var/log/auth.log on Debian-based systems or grep "Accepted publickey" /var/log/secure on RHEL-based systems. You should see an entry confirming the key was used.

If SSH still prompts for a password, double-check the key format, permissions, and that the key was copied correctly. A common mistake is missing the ~/.ssh/ directory or incorrect file permissions. Rebooting the SSH service with sudo systemctl restart sshd can also resolve lingering issues.

Tips & tricks for secure SSH key setup

Setting up SSH keys correctly prevents countless headaches later—here's how to avoid the most common pitfalls while making the process smoother.

Directory Structure Matters: Always verify the ~/.ssh/ directory exists before adding keys. I've seen cases where users created the directory in the wrong location (like /ssh/ instead of ~/.ssh/) which breaks SSH authentication entirely. Double-check with ls -la ~/.ssh after creation to confirm the directory is in your home folder.

Key Format is Non-Negotiable: The public key must be pasted exactly as shown—no extra spaces, line breaks, or formatting changes. SSH will silently reject malformed keys, leaving you scratching your head why it's not working. If you're copying from a website or email, use cat idrsa.pub to verify the clean format before pasting. This simple check saves hours of troubleshooting.

Permissions are Security's First Line: The chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorizedkeys commands are critical. Too-permissive settings (like 755) create security vulnerabilities. Remember: 700 means only the owner can access the directory, while 600 means only the owner can read/write the file. These settings prevent unauthorized users from viewing or modifying your keys.

Test Before Trusting: After adding your key, immediately test with ssh -v username@hostname. The verbose output (-v flag) reveals exactly where authentication fails—whether it's key format, permissions, or even network issues. This debugging step catches problems before they become major headaches, and it's something I wish I'd known years ago when SSH authentication failed silently.

💡

Pro Tips for Linux Add Public Key To Authorized_keys

  • Setting up SSH keys correctly prevents countless headaches later—here's how to avoid the most common pitfalls while making the process smoother.
  • Directory Structure Matters: Always verify the ~/.ssh/ directory exists before adding keys.
  • Key Format is Non-Negotiable: The public key must be pasted exactly as shown—no extra spaces, line breaks, or formatting changes.

Frequently asked questions

Got questions? You’re not alone! Here are answers to the most common concerns about adding public keys to authorizedkeys—so you can troubleshoot like a pro.

1

Why does my SSH connection still ask for a password after adding the key?

This usually happens if the key isn’t properly formatted, permissions are wrong, or the sshdconfig isn’t set to allow public key authentication. Double-check these steps:

  • Verify the key is in ~/.ssh/authorizedkeys with cat ~/.ssh/authorizedkeys.
  • Ensure permissions are chmod 600 ~/.ssh/authorizedkeys and chmod 700 ~/.ssh/.
  • Confirm PubkeyAuthentication yes and PasswordAuthentication no (or without-password) in /etc/ssh/sshdconfig.
  • Restart SSH with sudo systemctl restart sshd.
If it still fails, check /var/log/auth.log for clues!
2

Can I add multiple public keys to one authorizedkeys file?

The authorizedkeys file supports multiple keys—just append them one per line. Each key can even have custom options (like from="192.168.1.0/24" to restrict access). Example:

ssh-rsa AAAAB3NzaC1yc2E... user@laptop
ssh-rsa BBBBC3NzaC1yc2E... user@phone from="trusted-network"
Test each key separately to avoid lockouts.
3

How long does it take for the key to work after adding it?

It should work immediately—no waiting required! Just ensure:

  • The key is added to the remote server’s authorizedkeys (not your local machine).
  • You’re using the correct username and SSH command (ssh user@host).
  • No typos in the key or file path.
If it doesn’t, restart the SSH service (sudo systemctl restart sshd) to clear any cached settings.
4

What if I accidentally lock myself out of the server?

Don’t panic! Most servers have a recovery method:

  1. Use a physical console (if available) or IPMI/iDRAC to access the server directly.
  2. Edit authorizedkeys manually to remove the problematic key.
  3. If no console access, contact your hosting provider—they can often reset SSH access.
Pro tip: Always keep a backup password or a secondary admin account as a safety net!
5

Is there a way to revoke a key without logging in?

Yes! If you manage the server’s SSH config, you can:

  • Edit authorizedkeys via a second SSH session (using a different key or password).
  • Use ssh-keygen -R hostname on your local machine to remove the key from your knownhosts (won’t revoke server-side, but prevents future connections).
  • For cloud servers, some providers (like AWS) let you revoke keys via their dashboard.
No direct method exists to revoke a key remotely, but these workarounds help!

Wrapping up and next steps

Adding a public key to authorized_keys is a simple yet powerful way to secure your SSH access—no more password prompts, just seamless, encrypted connections! 🔐 By following these steps, you’ve not only enhanced security but also streamlined your workflow.

Now that you’re set up, why not explore advanced SSH configurations, like setting up key-based authentication for multiple servers or automating deployments with scripts? The possibilities are endless—keep learning and securing your digital life! 🚀

★★★★★4.9(6 reviews)
Categories Troubleshooting