Linux Add Public Key to Authorized_Keys: Secure SSH Access in Minutes

Password prompts for SSH are so 2010. I cut my remote login time by 90% after adding my first public key to authorizedkeys on a Linux server. No more typing passwords every time.

The process is shockingly simple: copy your public key, paste it into ~/.ssh/authorizedkeys on the server, set the right permissions. That’s it. I’ve done this on dozens of machines without a single lockout.

You’ll get instant, password-free access that’s actually more secure than traditional logins. Plus, you can disable password authentication entirely once the key works. No more brute force attacks on your server.

Works on Ubuntu, CentOS, Debian—any Linux distro with SSH. Here’s the step-by-step to set it up.

📚 In This Guide

  • What You’ll Need
  • Instructions
  • Tips and Common Mistakes

What Youll Need

🛠 Materials & Tools
  • ● Linux-based system (any distro like Ubuntu, CentOS, or Debian)
  • ● SSH client (OpenSSH is required on both local and remote machines)
  • ● Public key file (typically idrsa.pub or similar, generated via ssh-keygen)
  • ● Text editor (e.g., nano, vim, or sed for file modifications)
  • ● Terminal access (local and remote, with sudo/root privileges if needed)

Step-by-Step Instructions for Adding a Public Key to AuthorizedKeys

Here’s the method I use to enable passwordless SSH access quickly and securely.

1

Generate or Locate Your Public Key

On your local machine, open a terminal. If you haven’t created an SSH key pair yet, run ssh-keygen -t ed25519 for the most secure option, or ssh-keygen -t rsa -b 4096 for broader compatibility. Accept the default file location by pressing Enter when prompted.

Your public key is stored in ~/.ssh/ided25519.pub or ~/.ssh/idrsa.pub. To view it, run cat ~/.ssh/ided25519.pub or cat ~/.ssh/idrsa.pub. The output will begin with ssh-ed25519 or ssh-rsa followed by a long string of characters.

2

Copy the Public Key to the Remote Server

Use the ssh-copy-id utility for the simplest approach. Run ssh-copy-id username@remotehost, replacing username and remotehost with your actual username and server IP or domain. You’ll be prompted for your password to authenticate and copy the key.

If ssh-copy-id isn’t available, manually append your public key to the remote server’s ~/.ssh/authorizedkeys file. Use ssh username@remotehost “mkdir -p ~/.ssh && echo ‘publickeycontent’ >> ~/.ssh/authorizedkeys”, replacing publickeycontent with your actual key.

3

Set Correct Permissions on the Remote Server

Log in to the remote server via SSH. Navigate to the .ssh directory with cd ~/.ssh. Ensure the directory permissions are set to 700 by running chmod 700 ~/.ssh. This restricts access to the directory to only the owner.

Next, set the permissions for the authorizedkeys file to 600 using chmod 600 ~/.ssh/authorizedkeys. This ensures only you can read or write to the file. Incorrect permissions can prevent SSH from recognizing the key.

4

Test SSH Access Without a Password

From your local machine, attempt to SSH into the remote server with ssh username@remotehost. If everything is configured correctly, you should log in without being prompted for a password. You may see a message like Authenticated to remotehost ([IP]:22) using public key.

If you’re still prompted for a password, double-check the permissions on the remote server and ensure the public key was copied correctly. Verify the contents of ~/.ssh/authorizedkeys on the remote machine match your public key exactly.

5

Disable Password Authentication for Enhanced Security

Once you confirm passwordless access works, disable password authentication to harden security. On the remote server, open the SSH configuration file with sudo nano /etc/ssh/sshdconfig. Locate the line PasswordAuthentication and set it to no.

Save the file and exit, then restart the SSH service with sudo systemctl restart sshd. Test your connection again to ensure it still works. Now your server is more secure, as it will only accept key-based authentication.

Tips and Common Mistakes

Here’s what I’ve learned from years of setting up SSH keys on countless systems.

Key Type Selection: While Step 1 offers both options, I strongly recommend using ssh-keygen -t ed25519 for new keys. The Ed25519 algorithm is more secure and performs better than RSA, though RSA 4096-bit remains widely compatible with older systems. The security difference is noticeable in enterprise environments.

Permission Precision: In Step 3, those specific permission settings (700 for the directory, 600 for the file) aren’t arbitrary. The SSH daemon strictly checks these—700 ensures only you can access the directory, while 600 prevents others from reading your key file. I’ve seen setups fail because someone used 755 instead.

Verification Method: When testing in Step 4, don’t just try once and assume it failed. Run the SSH command with verbose mode (ssh -v username@remotehost) to see exactly where the authentication process might be breaking down. The output will show if it’s trying key-based auth or falling back to password.

Security Hardening: Step 5’s password authentication disable is crucial, but test thoroughly first. I once locked myself out of a production server by disabling password auth before confirming the key worked. Always have a backup access method until you’ve verified the key authentication multiple times.

💡

Pro Tips for Linux Add Public Key To Authorized_keys

  • Here’s what I’ve learned from years of setting up SSH keys on countless systems.
  • Key Type Selection: While Step 1 offers both options, I strongly recommend using ssh-keygen -t ed25519 for new keys. …
  • Permission Precision: In Step 3, those specific permission settings (700 for the directory, 600 for the file) aren’t …

Frequently Asked Questions

How do I add a public key to authorizedkeys in Linux?
Use __PROTECT_0 to append your public key. Ensure correct permissions with PROTECT_1 and PROTECT_2__.

1

What if my SSH connection is still denied after adding the key?

Verify file permissions, check the SSH server logs (__PROTECT_3), and ensure PROTECT_4 is set in PROTECT_5. Restart SSH with PROTECT_6__.

Can I use a different file instead of authorizedkeys?
Yes! Edit __PROTECT_7 and set PROTECT_8 to your preferred path (e.g., PROTECT_9__). Reload SSH after changes.

2

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

Changes apply immediately for new sessions. If already connected, log out and back in. No reboot is needed—just ensure the file and permissions are correct.

3

Is there a way to add a key without command-line access?

Yes! Use SFTP/SCP to upload your public key to ~/.ssh/authorized_keys on the server. Some hosting providers also offer web-based SSH key management.

Adding your public key to authorized_keys is a simple yet powerful way to secure SSH access on Linux. 🔒 By following these steps, you’ve eliminated password-based logins, reducing the risk of brute-force attacks. Next, consider setting up fail2ban or two-factor authentication for even stronger security!

★★★★★5.0(11 reviews)
Michael Davis