Skip to content

Linux SSH Permission Denied (publickey) Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Linux SSH Permission Denied (publickey) Fix. We cover key concepts, practical examples, and best practices.

SSH returns "Permission denied (publickey)" when the server rejects your key. This happens when key permissions are too open, the wrong key is used, the authorized_keys file is misconfigured, or SELinux/AppArmor blocks SSH key access.

The Problem

ssh user@server

Error:

user@server: Permission denied (publickey).

With verbose output:

ssh -vvv user@server

Shows:

debug1: Authentications that can continue: publickey
debug1: Offering public key: /home/user/.ssh/id_rsa
debug1: Server accepts key
debug1: Authentication succeeded (publickey).

But then:

debug1: reading /home/user/.ssh/authorized_keys
debug1: Authentication refused: bad permissions

Wrong Approach

# WRONG — setting loose permissions on the entire ~/.ssh
chmod 777 ~/.ssh
chmod 777 ~/.ssh/authorized_keys

Right Approach

# Correct permissions for SSH
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub

Expected output:

$ ssh user@server
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-86-generic x86_64)
user@server:~$

Step-by-Step Fix

Step 1: Fix local SSH directory permissions

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub

Step 2: Fix server authorized_keys permissions

ssh user@server "chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"

Or if you can log in with a password:

ssh user@server
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Step 3: Verify the key was added to authorized_keys

ssh user@server "cat ~/.ssh/authorized_keys"

Step 4: Check the SSH config

grep -i "pubkeyauthentication" /etc/ssh/sshd_config

Expected:

PubkeyAuthentication yes

Step 5: Test with verbose output

ssh -vvv user@server 2>&1 | grep -i "authenticated\|permission\|key"

Step 6: Fix SELinux context for custom SSH directories

sudo restorecon -Rv ~/.ssh

Prevention Tips

  • Always run ssh-copy-id user@server instead of manually copying keys
  • Keep strict permissions on ~/.ssh (700) and its files (600)
  • Use ssh-keygen -t ed25519 for modern, secure keys
  • Never share private keys between servers
  • Add keys to the SSH agent with ssh-add for convenience

Common Mistakes with ssh permission denied

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world LINUX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does SSH reject my key even though it is in authorized_keys?

The most common cause is incorrect permissions. SSH requires ~/.ssh to be 700 and ~/.ssh/authorized_keys to be 600. If the home directory is writable by group, SSH also refuses. Check with ls -la ~/ and ls -la ~/.ssh/.

What key algorithm should I use?

Use Ed25519 keys (ssh-keygen -t ed25519). They are faster, more secure, and produce shorter keys than RSA. If you need compatibility with legacy systems, use RSA with 4096 bits (ssh-keygen -t rsa -b 4096).

How do I copy my public key to a server?

Use ssh-copy-id user@server. It automatically appends your public key to ~/.ssh/authorized_keys and sets the correct permissions. If ssh-copy-id is not available, use: cat ~/.ssh/id_rsa.pub | ssh user@server "cat >> ~/.ssh/authorized_keys".

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro