1. Home
  2. Hosting Management
  3. FTP & SSH Access
  4. How to Fix SSH Permission Denied (publickey) Error on Linux and Mac

How to Fix SSH Permission Denied (publickey) Error on Linux and Mac

The “Permission denied (publickey,gssapi-keyex,gssapi-with-mic)” error you’re encountering suggests that SSH is unable to authenticate using the private key located at:

~/.ssh/id_rsa

Here’s a step-by-step guide to troubleshoot and resolve this:

Step 1) Check If SSH Agent Is Running and Add the Key

Run the following to check if your SSH key is loaded in the agent:

ssh-add -l
  • If you see something like:

The agent has no identities.

Add the key manually:

ssh-add ~/.ssh/id_rsa
  • If your key is encrypted, it will ask for the passphrase.

  • Once added, try connecting again:

ssh username@server-ip-address

Step 2) Check File Permissions

Incorrect file permissions can cause the Permission denied error. Run the following:

# Correct permissions for private key
chmod 600 ~/.ssh/id_rsa

# 
Correct permissions for public key
chmod 644 ~/.ssh/id_rsa.pub

# Correct permissions for the .ssh folder
chmod 700 ~/.ssh

Step 3) Verify Key Usage in SSH Config (Optional)

If you are using a custom SSH key or different config, check the SSH config file:

nano ~/.ssh/config

Ensure the following is in place (replace with the correct IP if needed):

Host server-ip-address User user-to-connect     IdentityFile ~/.ssh/id_rsa

Step 4) Test Verbose Mode for Debugging

If it still doesn’t work, run SSH in verbose mode to get more details:

ssh -v username@server-ip-address-or-hostname

This will help identify any configuration issues.


Step 5) Regenerate SSH Key (if needed)

If the key is corrupted or invalid, regenerate a new key:

ssh-keygen -t rsa -b 4096 -C "[email protected]"
  • Save the new key as id_rsa in the ~/.ssh directory.

  • Add the new public key to the server’s ~/.ssh/authorized_keys file.


Step 6) Check if Public Key is added on the Server

If you suspect the key is missing on the server, ensure that your public key is added to the ~/.ssh/authorized_keys on the server.


If none of these steps work, send the output of verbose mode to your server administrator.

Updated on March 28, 2025
Was this article helpful?

Related Articles

Spring into Savings!
Up to 78% Off Hosting Plans + Free Migration!
👉 View Deals

Leave a Comment