Get in Touch With Us

Submitting the form below will ensure a prompt response from us.

When connecting to a remote server using SSH, you might encounter the error:

Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

This error indicates that the SSH client attempted authentication but the server rejected it. It usually occurs when the server expects public key authentication, but the client either does not provide a valid key or the key is not configured correctly.

Understanding why the permission denied (publickey,gssapi-keyex,gssapi-with-mic) error occurs can help you quickly restore secure server access.

What Does This Error Mean?

The message:

Permission denied (publickey,gssapi-keyex,gssapi-with-mic)

means the SSH server tried several authentication methods but none succeeded.

The authentication methods shown include:

  • publickey → SSH key-based authentication
  • gssapi-keyex → Kerberos-based authentication
  • gssapi-with-mic → Secure Kerberos authentication

Most commonly, the failure occurs with public key authentication.

Common Causes of the Error

Missing SSH Key

If you try to connect without a valid SSH key, authentication fails.

Example command:

ssh user@server-ip

If no key is available or loaded, the server rejects the connection.

Incorrect Key Permissions

SSH requires strict file permissions for security.

Incorrect permissions may cause authentication failure.

Example fix:

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

Public Key Not Added to Server

Even if the private key exists locally, the corresponding public key must be stored on the server.

Location on server:

~/.ssh/authorized_keys

Add your public key:

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

Wrong Username

SSH authentication depends on the correct user account.

Example connection:

ssh ubuntu@server-ip

Using the wrong username may trigger the permission denied (publickey,gssapi-keyex,gssapi-with-mic) error.

SSH Agent Not Running

If your SSH key is not loaded into the SSH agent, authentication may fail.

Start the agent:

eval "$(ssh-agent -s)"

Add your key:

ssh-add ~/.ssh/id_rsa

Step-by-Step Fix of Permission Denied (Publickey,GSSAPI-Keyex,GSSAPI-With-Mic)

Step 1: Generate SSH Key (if missing)

ssh-keygen -t rsa -b 4096

This generates:

  • Private key → id_rsa
  • Public key → id_rsa.pub

Step 2: Copy Public Key to Server

Use:

ssh-copy-id user@server-ip

This automatically adds the key to authorized_keys.

Step 3: Verify Server Permissions

On the server:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Incorrect permissions can cause authentication failures.

Step 4: Test SSH Connection

Try connecting again:

ssh -i ~/.ssh/id_rsa user@server-ip

Debugging SSH Authentication

Use verbose SSH output to identify the problem.

ssh -v user@server-ip

For more detailed logs:

ssh -vvv user@server-ip

This command shows which authentication method fails.

Python Example: Testing SSH Connectivity

Developers sometimes test SSH connections programmatically using Python.

Example using paramiko:

import paramiko

host = "server-ip"
username = "user"
key_file = "/home/user/.ssh/id_rsa"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
   client.connect(hostname=host, username=username, key_filename=key_file)
   print("SSH connection successful")
except Exception as e:
   print("Connection failed:", e)

client.close()

This script helps validate SSH authentication automatically.

Best Practices for SSH Authentication

To avoid the permission denied (publickey,gssapi-keyex,gssapi-with-mic) error:

  1. Always use SSH keys instead of passwords
  2. Secure private keys with correct permissions
  3. Disable root SSH login where possible
  4. Use SSH agents for key management
  5. Rotate keys regularly for security

Proper SSH configuration improves both security and reliability.

When Does This Error Usually Occur?

The permission denied (publickey,gssapi-keyex,gssapi-with-mic) error often appears in these situations:

  • First-time server setup
  • Cloud VM access (AWS, Azure, GCP)
  • Git repository authentication failures
  • CI/CD pipeline deployments
  • Incorrect SSH key configuration

Understanding these scenarios helps troubleshoot quickly.

Need Help Fixing SSH Errors?

Our DevOps experts resolve server authentication and SSH configuration issues.

Consult DevOps Experts

Conclusion

The error permission denied (publickey,gssapi-keyex,gssapi-with-mic) occurs when the SSH server cannot authenticate the client using the available methods.

The most common causes include:

  1. Missing SSH keys
  2. Incorrect key permissions
  3. Public key not added to the server
  4. Wrong username
  5. SSH agent not running

By generating keys, adding them to the server, fixing permissions, and verifying SSH configuration, you can quickly resolve the issue and restore secure remote access.

About Author

Jayanti Katariya is the CEO of BigDataCentric, a leading provider of AI, machine learning, data science, and business intelligence solutions. With 18+ years of industry experience, he has been at the forefront of helping businesses unlock growth through data-driven insights. Passionate about developing creative technology solutions from a young age, he pursued an engineering degree to further this interest. Under his leadership, BigDataCentric delivers tailored AI and analytics solutions to optimize business processes. His expertise drives innovation in data science, enabling organizations to make smarter, data-backed decisions.