Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SSH Key Not Working? Fix Linux Permission Issues

· 5 min read · Home Tech

In this article
  1. How SSH Key Authentication Works
  2. Why Permissions Matter So Much
  3. Step-by-Step Troubleshooting
  4. Common Misconceptions and Mistakes
  5. When the Key Is Offered but Rejected
  6. Key Types and Compatibility
  7. Alternative Authentication Methods
  8. Frequently Asked Questions

If your SSH key file isn't working on Linux, the cause is almost always incorrect file permissions on your ~/.ssh directory or the key files themselves, or the key simply isn't being offered to the server. This article walks through the exact checks and fixes to get your key authentication working.

How SSH Key Authentication Works

SSH key authentication uses a pair of files: a private key (kept on your client machine) and a public key (installed on the server). When you connect, the server sends a challenge that only the holder of the private key can answer. The server checks your public key against the ~/.ssh/authorized_keys file on the server. For this to work, three things must be true:

  • The private key exists on your client and is readable by you.
  • The public key is correctly placed in the server's authorized_keys file.
  • Both the client and server are configured to allow key authentication.

Why Permissions Matter So Much

OpenSSH is deliberately strict about permissions. If your key file is readable by other users, or if your .ssh directory is writable by others, sshd will refuse to use the key. This is a security measure to prevent other users on the same machine from stealing or modifying your keys. The standard permissions are:

  • ~/.ssh directory: 700 (drwx------)
  • Private key (id_rsa, id_ed25519): 600 (-rw-------)
  • Public key (id_rsa.pub, id_ed25519.pub): 644 (-rw-r--r--)
  • ~/.ssh/authorized_keys: 600 or 644 (but not writable by group/others)

Step-by-Step Troubleshooting

Follow these steps in order. Stop when the problem is resolved.

  1. Check the verbose output. Run ssh -vvv user@host. Look for lines like "Offering public key" and "Authentications that can continue". If you see "Permission denied (publickey)" without an offer, the key isn't being offered.
  2. Verify key file permissions. Run ls -la ~/.ssh. If any file has group or world read/write, fix it: chmod 700 ~/.ssh and chmod 600 ~/.ssh/id_ed25519 (adjust filename).
  3. Check the server's authorized_keys. On the server, run cat ~/.ssh/authorized_keys. Ensure the public key is on one line, no line breaks, and matches your id_ed25519.pub content exactly.
  4. Confirm the server's permissions. On the server, chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys.
  5. Test with explicit key path. Use ssh -i /path/to/key user@host. If that works, the issue is that ssh isn't finding the key automatically (maybe it's not in ~/.ssh or has a non-standard name).
  6. Check ssh-agent. If you use ssh-agent, run ssh-add -l to list loaded keys. If your key isn't listed, add it with ssh-add ~/.ssh/id_ed25519.

Common Misconceptions and Mistakes

One frequent mistake is copying the public key with a line break or extra whitespace. The authorized_keys file is extremely picky. Use ssh-copy-id instead of manually copying: ssh-copy-id user@host. This command sets the correct permissions automatically.

Another misconception is that the private key must be 600. While 600 is standard, 400 also works. The key point is that no other user can read it. Also, the .ssh directory itself must not be writable by group or others. If you've ever run chmod -R 777 ~, that breaks everything.

Also, if you're using a passphrase on your key, ssh will prompt for it unless you use ssh-agent. If you see a passphrase prompt, that's normal.

When the Key Is Offered but Rejected

If verbose output shows "Offering public key" and then "Server accepts key" but still fails, the problem is on the server side. Check the server's /var/log/auth.log (or journalctl -u ssh) for lines like "Authentication refused" or "bad permissions". Common server-side causes:

  • authorized_keys has wrong permissions.
  • The user's home directory is writable by others (fix with chmod 755 ~).
  • The key is not in the correct authorized_keys file (e.g., you're logging in as a different user).
  • sshd config has PubkeyAuthentication no or AuthorizedKeysFile points elsewhere.

Key Types and Compatibility

Modern systems default to Ed25519 keys (ssh-keygen -t ed25519), which are shorter and faster. RSA keys (2048/4096 bit) are still widely supported. If you're using an older key type like DSA, it may be disabled by default. To generate a new key, use ssh-keygen -t ed25519 -C "your_email@example.com". The -C is just a comment; it doesn't affect functionality.

Alternative Authentication Methods

If key auth still fails, you can temporarily use password authentication (if enabled) to get in and fix the server. To enable password auth on the server, edit /etc/ssh/sshd_config and set PasswordAuthentication yes, then restart sshd. But this is a fallback, not a long-term solution. Another alternative is using an SSH config file (~/.ssh/config) to specify the key per host:

Host myserver
    HostName example.com
    User myuser
    IdentityFile ~/.ssh/id_ed25519

Frequently Asked Questions

Why does my key work on one machine but not another?

Usually because the public key isn't installed on the server, or the private key on the second machine has different permissions. Check that the authorized_keys on the server contains the correct public key, and that the private key on the second machine is 600.

What does "Permission denied (publickey)" mean exactly?

It means the server rejected all offered public keys. Either no key was offered (client-side issue) or the offered key was not in the server's authorized_keys (server-side issue). Use ssh -vvv to see which.

Can I use a key with a passphrase in automated scripts?

Yes, but you need to load the key into ssh-agent first. Run ssh-add ~/.ssh/id_ed25519 and enter the passphrase once. The agent will then provide the key without prompting. For fully unattended scripts, you can use ssh-keygen -p to remove the passphrase, but that reduces security.

Is it safe to set my .ssh directory to 777 to make it work?

No. That is a severe security risk. Any user on the system could read your private keys or modify your authorized_keys. Always use 700 for the directory and 600 for private keys.

Why does ssh-copy-id fail with "Permission denied"?

This usually means password authentication is disabled on the server, or you're using the wrong password. Since ssh-copy-id needs to log in once to install the key, you need either password auth enabled or you can manually append the public key to the server's authorized_keys using another method (like a console).