The SSH client is refusing to use your private key because its permissions are too permissive, allowing unauthorized access.
Here’s what’s actually broken: When you generate an SSH key pair, the private key file (id_rsa by default) needs to be inaccessible to anyone other than its owner. The SSH protocol enforces this strictly for security reasons. If the permissions are too broad (like 0644, which means owner can read/write, and group/others can read), the SSH client will reject the key, assuming it might have been compromised. It’s the client’s security guard dog barking, not a problem with the server itself.
Common Causes and Fixes:
-
Incorrect
chmodcommand used during key generation: This is the most frequent culprit. You might have accidentally set the permissions too wide.- Diagnosis: Run
ls -l ~/.ssh/id_rsa. Look at the permission string at the beginning (e.g.,-rw-r--r--). If it’s not-rw-------, this is likely the issue. - Fix: Execute
chmod 600 ~/.ssh/id_rsa. This command changes the permissions so only the owner can read and write the file. The SSH client specifically requires this for private keys. - Why it works: Setting permissions to 600 ensures that only the user who owns the file can read or modify it, fulfilling the SSH client’s security requirement that the private key remains confidential.
- Diagnosis: Run
-
Permissions on the
.sshdirectory itself are too open: Even ifid_rsahas correct permissions, if the.sshdirectory is writable by others, SSH will also reject the key.- Diagnosis: Run
ls -ld ~/.ssh. Check the permission string. It should ideally bedrwx------(700) ordrwxr-xr-x(755), but crucially, it should not be writable by group or others. - Fix: Execute
chmod 700 ~/.ssh. This sets the permissions for the.sshdirectory so that only the owner can read, write, and execute (enter) the directory. - Why it works: This prevents other users on the system from listing the contents of your
.sshdirectory or creating/modifying files within it, which is another layer of security SSH mandates.
- Diagnosis: Run
-
Ownership issues on
.sshorid_rsa: If the files are owned by a different user than the one trying to use them, SSH will reject them. This can happen after copying files or changing user accounts.- Diagnosis: Run
ls -ld ~/.sshandls -l ~/.ssh/id_rsa. Check the user and group columns. Ensure they match your current logged-in user. - Fix: Execute
sudo chown $(whoami):$(whoami) ~/.sshandsudo chown $(whoami):$(whoami) ~/.ssh/id_rsa. Replace$(whoami)with your actual username ifsudodoesn’t interpret it correctly. - Why it works: SSH verifies that the user attempting to authenticate is the owner of the private key. Correcting ownership ensures this association is valid.
- Diagnosis: Run
-
SELinux or AppArmor restrictions: On systems with enhanced security modules like SELinux (common on RHEL/CentOS/Fedora) or AppArmor (common on Ubuntu/Debian), policies might prevent SSH from accessing files with even correct permissions if they are in an unexpected location or have incorrect security contexts.
- Diagnosis: Check system logs for SELinux/AppArmor denials. For SELinux, use
sudo ausearch -m avc -ts recent. For AppArmor, check/var/log/syslogor/var/log/audit/audit.logfor relevant messages. - Fix (SELinux): Run
sudo restorecon -Rv ~/.ssh. This command recursively resets the SELinux security context of the.sshdirectory and its contents to the default, which usually allows SSH access. - Fix (AppArmor): You might need to adjust AppArmor profiles, which is more complex. A temporary workaround is
sudo aa-complain /usr/bin/sshd(orssh), but a permanent fix involves modifying the profile in/etc/apparmor.d/. - Why it works: SELinux and AppArmor enforce security policies beyond standard file permissions. Restoring the correct security context or adjusting profiles ensures that the SSH daemon has the necessary permissions to read the key file as dictated by the security module.
- Diagnosis: Check system logs for SELinux/AppArmor denials. For SELinux, use
-
SSH Agent issues (less common for this specific error): While not directly causing the "Bad Permissions" error on the key file itself, if you’re using
ssh-agentand the agent has been started with incorrect permissions or is somehow compromised, it could indirectly lead to confusion or rejections. However, the0644error is almost always about the file system permissions.- Diagnosis: Check
ssh-add -lto see if keys are loaded. Examine the permissions of the agent socket file (often in/tmp/ssh-XXXXXX/agent.XXXXXX). - Fix: Restart the SSH agent with
eval $(ssh-agent -s)and re-add your key withssh-add ~/.ssh/id_rsa. Ensure the SSH agent socket directory is also protected. - Why it works: Reinitializing the agent and re-adding the key ensures a clean state, and proper protection of the agent socket prevents unauthorized access to the keys managed by the agent.
- Diagnosis: Check
-
File system corruption or network mounts: In rare cases, issues with the underlying file system or how network mounts (like NFS) handle permissions can cause this.
- Diagnosis: Check
dmesgfor file system errors. If.sshis on an NFS mount, verify NFS export options and client mount options related to permissions. - Fix: Run
fsckon the relevant partition (requires unmounting). For NFS, adjustno_root_squashorall_squashoptions on the server and correspondinguid/gidoptions on the client as needed. - Why it works: Ensures the file system correctly reports and enforces permissions, or that the network file sharing protocol correctly maps user identities and permissions.
- Diagnosis: Check
After fixing the permissions on your ~/.ssh/id_rsa file to 600 and ensuring your ~/.ssh directory has 700 permissions, you should be able to connect. The next error you might encounter if other issues persist is "Permissions 0777 for 'authorized_keys' are too open."