DEV Community

Khalif AL Mahmud
Khalif AL Mahmud

Posted on

Resolving SSH Authentication Errors on Mac: Using a .pem File

When attempting to connect to a remote server via SSH, you might encounter an error indicating that your client isn’t configured to use the correct private key. This typically happens when the server expects a private key for authentication, and your SSH setup isn’t pointing to the correct .pem file. Here's how to resolve this issue on a Mac.

Steps to Use the .pem File for SSH

1. Ensure You Have the .pem File

The private key file (e.g., my-key.pem) is essential for establishing a secure connection. This file should have been provided or downloaded from your cloud provider (like AWS) when setting up the server.

2. Set Proper Permissions for the .pem File

To protect your private key, it’s critical to restrict its permissions. Use the following command to set the appropriate permissions:

chmod 400 path/to/your-key.pem
Enter fullscreen mode Exit fullscreen mode

This ensures that only your user account can read the file.

3. Connect to the Server Using the .pem File

When connecting via SSH, specify the .pem file using the -i flag:

ssh -i path/to/your-key.pem username@3.81.139.63
Enter fullscreen mode Exit fullscreen mode
  • Replace path/to/your-key.pem with the actual path to your .pem file.

  • Replace username with the correct username for the server (e.g., ec2-user, ubuntu, or sakib).

4. Check SSH Configuration (Optional)

For convenience, you can create an SSH configuration file to simplify future connections:

  1. Open or create the SSH config file:
nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode
  1. Add the following configuration:
Host my-server
    HostName 3.81.139.63
    User sakib
    IdentityFile path/to/your-key.pem
Enter fullscreen mode Exit fullscreen mode
  1. Save and exit the file. Now, you can connect simply by typing:
ssh my-server
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Common Issues

Key Pair Mismatch

Ensure that the .pem file you are using matches the public key configured on the remote server. If the keys don’t match, you’ll encounter authentication errors.

Wrong Username

Some servers have specific default usernames based on their operating systems:

  • Ubuntu: ubuntu

  • Amazon Linux: ec2-user

  • CentOS: centos

  • Debian: admin or debian

  • RHEL: ec2-user

If you’re unsure, check the documentation of your cloud provider or contact the server administrator.

Check Server Logs (Advanced)

If you have access to server logs, they can provide insight into authentication failures. Check /var/log/auth.log (on Ubuntu/Debian) or /var/log/secure (on CentOS/RHEL).

Top comments (0)