Resolving "Permission Denied" Errors in SSH Print

  • 0

Managing a Virtual Private Server (VPS) is a critical task for developers, sysadmins, and businesses, and SSH (Secure Shell) is the go-to method for secure remote access. Providers like VPS.DO offer user-friendly VPS solutions with SSH access enabled out of the box, but encountering a "Permission Denied" error can halt your workflow. This error typically occurs when the SSH client fails to authenticate with the server, often due to incorrect credentials, misconfigured SSH settings, or file permission issues. This article provides a practical, step-by-step guide to diagnose and resolve "Permission Denied" errors when connecting to a Linux-based VPS via SSH.

Understanding "Permission Denied" Errors

The "Permission Denied" error in SSH usually appears as Permission denied (publickey) or Permission denied, please try again in the terminal or SSH client (e.g., PuTTY). Common causes include:

  • Incorrect username or password.
  • Issues with SSH key authentication (e.g., wrong key, incorrect permissions).
  • Misconfigured SSH server settings (sshd_config).
  • Firewall or security group restrictions.
  • Disabled root login or password authentication.

Below, we outline actionable steps to troubleshoot and fix these issues.

Step-by-Step Troubleshooting

1. Verify Username and IP Address

The simplest cause of a "Permission Denied" error is an incorrect username or IP address.

  • Check Credentials: Ensure you’re using the correct username (e.g., root or a custom user) and VPS IP address provided by your VPS provider (e.g., in your VPS.DO dashboard or welcome email).
  • Test Connection: Run the SSH command with the correct details:
    ssh username@your.vps.ip.address
    
    If your VPS uses a non-standard port (e.g., 2222), include it:
    ssh -p 2222 username@your.vps.ip.address
    
  • Verbose Mode: Use verbose output to debug:
    ssh -v username@your.vps.ip.address
    
    Look for clues in the output, such as which authentication method is being attempted.

2. Confirm Password Authentication

If you’re using password-based authentication, the error may stem from an incorrect password or disabled password login.

  • Reset Password: If you suspect the password is wrong, use your VPS provider’s console or control panel to reset it. Check VPS.DO’s documentation for password reset instructions.
  • Check SSH Configuration: Access the VPS via an alternative method (e.g., provider console). Verify that password authentication is enabled in /etc/ssh/sshd_config:
    PasswordAuthentication yes
    
    If it’s set to no, change it to yes, save the file, and restart the SSH service:
    sudo systemctl restart sshd
    
  • Try Again: Test the connection with the correct password.

3. Troubleshoot SSH Key Authentication

If you’re using SSH keys and see Permission denied (publickey), the issue likely lies with the key setup.

  • Verify Key Pair: Ensure you’re using the correct private key. On your local machine, check that the private key (e.g., ~/.ssh/id_rsa) matches the public key on the VPS.
  • Check Key Permissions: On your local machine, private key permissions must be restrictive:
    chmod 600 ~/.ssh/id_rsa
    
    On the VPS, the authorized_keys file and its directory must have correct permissions:
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
    
  • Copy Public Key Correctly: Ensure the public key (e.g., id_rsa.pub) is in ~/.ssh/authorized_keys on the VPS. If not, copy it using:
    ssh-copy-id username@your.vps.ip.address
    
    Or manually append the public key to ~/.ssh/authorized_keys.
  • Check SSH Config: In /etc/ssh/sshd_config, ensure key authentication is enabled:
    PubkeyAuthentication yes
    AuthorizedKeysFile .ssh/authorized_keys
    
    Restart the SSH service after changes:
    sudo systemctl restart sshd
    

4. Check for Root Login Restrictions

Some VPS configurations disable root login for security.

  • Verify Root Access: In /etc/ssh/sshd_config, check:
    PermitRootLogin yes
    
    If set to no or prohibit-password, change to yes or create a non-root user:
    sudo adduser newuser
    sudo usermod -aG sudo newuser
    
    Copy your SSH key to the new user’s ~/.ssh/authorized_keys and test:
    ssh newuser@your.vps.ip.address
    
  • Restart SSH: Apply changes with:
    sudo systemctl restart sshd
    

5. Inspect Firewall and Security Groups

A firewall or security group might block your SSH connection, indirectly causing authentication failures.

  • Check VPS Firewall: Using console access, verify the SSH port (default: 22) is open. For Ubuntu/Debian:
    sudo ufw status
    sudo ufw allow 22
    
    For CentOS/RHEL:
    sudo firewall-cmd --list-all
    sudo firewall-cmd --add-port=22/tcp --permanent
    sudo firewall-cmd --reload
    
  • Provider Security Groups: Log in to your VPS provider’s dashboard (e.g., VPS.DO) and ensure the SSH port is allowed in network or security group settings.

6. Test with Alternative Clients

If the default SSH client fails, try another tool to rule out client-specific issues:

  • Linux/macOS: Use ssh with verbose mode (as above) or try openssh-client.
  • Windows: Use PuTTY or MobaXterm. For PuTTY, ensure the private key (converted to .ppk format using PuTTYgen) is correctly loaded in the “Connection > SSH > Auth” section.

7. Check Server Logs

If you have console access, check the SSH server logs for detailed errors:

sudo tail -f /var/log/auth.log  # Debian/Ubuntu
sudo tail -f /var/log/secure    # CentOS/RHEL

Look for authentication failure messages or incorrect key attempts.

Best Practices to Prevent "Permission Denied" Errors

  • Use SSH Keys: They’re more secure and less prone to errors than passwords.
  • Backup Credentials: Store your username, password, and SSH keys securely.
  • Regularly Update SSH Config: Ensure /etc/ssh/sshd_config aligns with your authentication method.
  • Monitor File Permissions: Use ls -l to verify ~/.ssh and authorized_keys permissions.
  • Test Changes: After modifying SSH settings, test with a new session before closing your console.

Conclusion

 

"Permission Denied" errors in SSH can stem from simple credential mistakes or complex configuration issues, but methodical troubleshooting can resolve them. By verifying credentials, checking SSH configurations, and ensuring proper permissions, you can restore access to your VPS. If issues persist, providers like VPS.DO often provide console access and support resources to help you recover. With these steps, you’ll be back to managing your server seamlessly.


Was this answer helpful?

« Back