Mac Tips guides, explanations and reader checklists on whilenetworking.com

Configuring SSH Key Authentication for Secure Remote Server Access from Your Mac

In the modern IT landscape, relying on password-based authentication for remote server access is increasingly risky due to the prevalence of brute-force…

In the modern IT landscape, relying on password-based authentication for remote server access is increasingly risky due to the prevalence of brute-force attacks and credential stuffing. The industry standard has shifted toward SSH key authentication, which offers a robust layer of security by leveraging public-key cryptography. For macOS users, configuring this process involves interacting with the Terminal, managing identity files, and ensuring compatibility across different Linux distributions and Windows environments. This guide provides a step-by-step walkthrough to set up SSH keys securely, complete with verification steps, troubleshooting for common errors, and practical applications for both students and professionals.

Understanding the Security Advantage of SSH Keys

SSH (Secure Shell) key authentication replaces passwords with a cryptographic handshake between your client (your Mac) and the server. When you generate an SSH key pair, you create a private key that stays securely on your Mac and a corresponding public key that is uploaded to the remote server’s authorized_keys file. During a connection attempt, the server presents a challenge; your Mac signs this challenge using your private key. If the signature matches the stored public key, access is granted without ever transmitting a password.

This method mitigates several risks:
* No Password Transmission: Credentials are never sent over the network in plain text or even encrypted as passwords.
* Resistance to Brute Force: Attackers cannot guess your access because there is no password to crack.
* Multi-Factor Integration: SSH keys can be combined with a passphrase (a password protecting the private key file itself), effectively creating a two-factor authentication mechanism where the device holding the key and the user’s knowledge of the passphrase are both required.

Prerequisites and Environment Setup

Before generating keys, ensure your Mac is running macOS Ventura or later (or Catalina for older setups) with the built-in OpenSSH client installed. While Apple includes an SSH server by default in newer versions, this guide focuses on the client side—initiating connections from your Mac to remote servers.

You will need:
1. A Terminal application (pre-installed on macOS).
2. An existing remote server or a local test machine (like a Raspberry Pi or another Linux VM) to upload your public key to.
3. Basic familiarity with the command line interface.

Note: If you are using a specific corporate environment, verify that your organization’s security policies do not require additional certificates or managed identity agents before proceeding.

Generating Your SSH Key Pair in Terminal

The most common key type is Ed25519, which offers strong security with smaller file sizes compared to RSA. You can generate a new key pair using the ssh-keygen command located in your user’s home directory.

Run the following command in your Terminal:

ssh-keygen -t ed25519 -C "your_email@example.com"

Press Enter to accept the default file location (~/.ssh/id_ed25519). When prompted for a passphrase, type a strong password and press Enter. Do not skip this step. A passphrase ensures that if your Mac is lost or stolen, an attacker cannot use the private key to access other servers without also guessing your passphrase.

Expected Output:
The terminal will display confirmation messages indicating the file has been created:

Your identification has been saved in ~/.ssh/id_ed25519
Your public key has been saved in ~/.ssh/id_ed25519.pub
Your fingerprint is SHA256:<random_fingerprint>

The private key (id_ed25519) remains hidden and secure. The public key (id_ed25519.pub) contains the data needed to verify your identity on the server.

Uploading the Public Key to Your Remote Server

To authenticate, you must place the contents of your public key file into the authorized_keys file on the remote server. Navigate to where your public key is stored:

cat ~/.ssh/id_ed25519.pub

Copy the entire line that starts with ssh-ed25519. You can then paste this into a text editor on your remote server, or use the convenience script ssh-copy-id if available.

To paste directly via Terminal (assuming you have SSH access as a user):

pbcopy < ~/.ssh/id_ed25519.pub

(This copies the key to your Mac’s clipboard for easy pasting elsewhere, or simply copy the output manually).

On the remote server, log in and ensure the .ssh directory exists:

mkdir -p ~/<username>/.ssh
chmod 700 ~/<username>/.ssh

Create or edit the authorized_keys file:

echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... <paste_key_here> your_email@example.com" >> ~/.ssh/authorized_keys
chmod 600 ~/<username>/.ssh/authorized_keys

Critical Security Note: The permissions on the .ssh directory must be 700, and the authorized_keys file must be 600. If these permissions are too open (e.g., 755), the SSH daemon will refuse to use them, resulting in a “Permission denied” error even if the key is correct.

Verifying the Configuration and Testing Access

Once configured, test the connection without using a password. This verifies that the key pair matches and permissions are correct.

Open Terminal on your Mac and run:

ssh -i ~/.ssh/id_ed25519 username@remote_server_ip

If you have set a passphrase, you will be prompted to enter it. Upon successful entry, you should see the server’s welcome message without being asked for a password again. To disable password prompts entirely and force key-only authentication, add this line to your ~/.ssh/config file on your Mac:

Host remote_server_ip
    IdentityFile ~/.ssh/id_ed25519
    PasswordAuthentication no

Common Error: “Permission denied (publickey,password)”
This usually means the public key was not uploaded correctly, or the authorized_keys file permissions are wrong. Re-check that the file on the server is owned by your user and has 600 permissions. Also, ensure you didn’t accidentally upload a different key type; the prefix in the authorized_keys file must match the key generated (e.g., ssh-ed25519).

Common Error: “Host key verification failed”
This occurs if your Mac doesn’t trust the server’s identity. This is a security feature to prevent man-in-the-middle attacks. To fix this in a trusted lab environment, you can add the server’s host key fingerprint to your known_hosts file by running ssh -o StrictHostKeyChecking=no username@server, but in production, investigate why the trust is failing before disabling strict checking.

Student Practice Task and Industry Application

For IT students, a practical exercise involves setting up a local virtual machine (using VirtualBox or Parallels) running Ubuntu Server. Generate keys on your Mac host, upload them to the VM, and practice managing multiple environments by creating separate key pairs for different projects stored in ~/.ssh/project_a and ~/.ssh/project_b. This teaches key management hygiene.

For industry practitioners, this configuration is essential for DevOps workflows. Automated deployment scripts (like Ansible or Jenkins) often run as non-interactive users. These bots cannot enter passphrases. Therefore, industry best practice dictates generating keys without a passphrase specifically for automation tools (e.g., ssh-keygen -t ed25519 -f /etc/ansible/ssh_key) and storing the private key securely in a secrets manager or encrypted volume, while human-administered keys always retain passphrases.

Always verify policy terms with your organization’s security team, as some jurisdictions require specific logging of SSH access attempts regardless of authentication method used.

Educational-use notice: This tutorial is for learning and controlled lab use only. Test carefully, use backups, and obtain authorization before making changes. Do not run unfamiliar commands on production systems. We accept no responsibility for misuse, damage, data loss, or production issues.