Securing remote server access is a fundamental skill for any IT professional, from students learning Linux fundamentals to senior engineers managing cloud infrastructure. Relying solely on passwords exposes systems to brute-force attacks and credential stuffing. Switching to SSH key authentication provides a robust layer of defense based on cryptographic principles rather than memorized strings. This guide walks you through generating keys on macOS, configuring the server, and troubleshooting common issues.
Why Move Away from Passwords?
Password-based authentication is inherently fragile. If a user reuses a password across multiple services, a breach at one site compromises all others. Furthermore, complex passwords are often forgotten or written down insecurely. SSH keys utilize asymmetric cryptography: a private key stays securely on your local machine (the client), while the public key resides on the server. The server verifies that you possess the corresponding private key without ever needing to know it. This eliminates the risk of password interception during transmission and significantly reduces the attack surface for automated bots scanning for weak credentials.
Prerequisites and Environment Setup
Before beginning, ensure your macOS environment is ready. You will need an active internet connection and access to a target server (local or remote). While this guide assumes you are using Apple Silicon (M-series) or Intel Macs running macOS Ventura or later, the concepts apply universally.
You must have the OpenSSH client installed on your Mac, which comes pre-installed with macOS. On the server side, ensure that SSH is enabled (sshd) and that password authentication is either disabled or used only as a fallback initially. For this demonstration, we will assume you are connecting to a Linux-based server (such as Ubuntu or Debian) via the ssh command from your Mac Terminal.
Generating the Key Pair on macOS
The first step involves creating a new key pair using the built-in ssh-keygen utility. Open your Terminal application and execute the following command:
ssh-keygen -t ed25519 -C "your_email@example.com"
This command generates an Ed25519 key, which is currently considered one of the most secure and efficient algorithms for SSH. The -C flag adds a comment to the public key file, helping you identify it later (e.g., your email address or role).
You will be prompted to enter a file location; pressing Enter accepts the default path (~/.ssh/id_ed25519). Next, you may be asked to set a passphrase. A passphrase encrypts your private key file on disk. If you forget this password, access to the server becomes impossible until you reset the key. For production environments, always use a strong passphrase. Press Enter twice if you prefer no passphrase (not recommended for sensitive systems), or type a complex string and confirm it.
Copying the Public Key to the Server
Once generated, you must place your public key on the remote server. The most efficient method is using the ssh-copy-id utility. Run the following command in your Mac Terminal:
ssh-copy-id user@server_address
Replace user with your remote username and server_address with the IP or domain name (e.g., 192.168.1.50). You will be prompted to enter the password for that remote account just once. This tool automatically reads your default key files and appends them to the ~/.ssh/authorized_keys file on the server.
Alternatively, you can manually copy the contents of ~/.ssh/id_ed25519.pub using a text editor or clipboard tools if ssh-copy-id fails, but ensure you append the exact content to the correct authorized_keys file without adding extra newlines that might break parsing.
Configuring Server-Side Security
After copying the key, it is critical to disable password authentication on the server to force key-only access. This prevents attackers from guessing passwords even if they have a valid public key. Edit the SSH daemon configuration file on the remote server using sudo nano /etc/ssh/sshd_config.
Locate the line starting with PasswordAuthentication. Change it to:
PasswordAuthentication no
Ensure that PubkeyAuthentication is set to yes. After making changes, restart the SSH service to apply them. On Ubuntu/Debian systems, run sudo systemctl restart sshd.
Verification and Troubleshooting Common Errors
Connect to your server using the new key:
ssh -i ~/.ssh/id_ed25519 user@server_address
If configured correctly, you will be logged in directly without a password prompt. If you encounter “Permission denied (publickey)”, check the following:
1. File Permissions: Ensure the .ssh directory and authorized_keys file have strict permissions. On the server, run chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys. Loose permissions can cause SSH to ignore the keys for security reasons.
2. Key Format: Verify that the public key in authorized_keys matches the private key exactly. Extra spaces or newlines can invalidate the match.
3. Agent Forwarding: If you are using an SSH agent, ensure it is running: eval "$(ssh-agent -s)" followed by ssh-add ~/.ssh/id_ed25519.
Real-World Scenario: The Junior Engineer’s Checklist
Imagine you are a junior engineer onboarding a new developer. They need secure access to the production staging server but cannot use their current password due to security policy.
Step 1: Generate keys for the new user as shown above.
Step 2: Use ssh-copy-id to deploy the key.
Step 3: Document the private key location in a password manager (store only the passphrase, not the key file itself).
Step 4: Rotate the old password after confirming the new key works for 24 hours.
This workflow ensures continuity of access while tightening security posture immediately.
Student Practice Task: Simulating a Lab Environment
For educational purposes, students should practice this in an isolated lab. Download VirtualBox or use a cloud sandbox like AWS EC2 (free tier) or Google Cloud. Spin up a small Ubuntu instance. From your Mac, generate keys and attempt to connect. Intentionally break the configuration by setting incorrect permissions on the authorized_keys file to observe the “Permission denied” error, then fix it. This hands-on experience reinforces the importance of file system integrity in SSH security.
Conclusion
Configuring SSH key authentication transforms remote access from a vulnerable convenience into a secure standard practice. By following these steps—generating Ed25519 keys on macOS, deploying them securely, disabling password logins, and verifying permissions—you build a foundation for robust network management. Remember that terms, availability, and specific security policies depend on your organization’s infrastructure and local laws. Always verify configuration details against your specific server documentation before applying changes to production systems.
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.

