Relying on passwords for server access is increasingly risky in an era of sophisticated credential stuffing attacks and keyloggers. Generating a public/private key pair and configuring them correctly offers a robust, seamless alternative that enhances security while improving workflow efficiency. This guide walks you through generating SSH keys, installing the public key on your remote server, and verifying passwordless authentication.
Understanding SSH Keys and Security Best Practices
Before diving into the technical steps, it is essential to understand how SSH keys function. Unlike passwords, which are transmitted in a way that can be intercepted or guessed, SSH keys use asymmetric cryptography. You generate a pair of keys: a private key stays securely on your local machine, and the public key is uploaded to the remote server. The server uses the public key to verify that you possess the corresponding private key before granting access.
Security best practices dictate that your private key should never be shared or uploaded to public repositories. You should also consider protecting your private key with a passphrase. While this adds an extra layer of security requiring a password entry upon every new session (or until the agent is refreshed), it ensures that even if your laptop is stolen, the attacker cannot use the private key without knowing your passphrase. If you prioritize maximum convenience over defense-in-depth, you can configure the system to load the key from memory automatically at login, but this requires careful configuration of your shell environment variables.
Generating a New SSH Key Pair
The most common and secure method for generating keys is using the built-in OpenSSH utility, ssh-keygen. This tool works on Linux, macOS, and Windows (via Git Bash or WSL). If you already have an existing key pair, you can skip this step, but it is best practice to generate a fresh pair when changing computers or losing access.
Open your terminal application and run the following command:
ssh-keygen -t ed25519 -C "your_email@example.com"
This command specifically asks for an Ed25519 key, which is currently considered more secure and compact than the older RSA algorithm. The -C flag adds a comment to the public key file, which helps you identify it later.
The system will prompt you to specify a file location (default is ~/.ssh/id_ed25519) and then ask for a passphrase.
* For production servers: Enter a strong passphrase. This prevents unauthorized access if your device is compromised.
* For personal convenience: You may press Enter twice to leave the key unencrypted. Note that this makes the private key less secure if your device is lost.
After generation, you will have two files in your ~/.ssh directory: id_ed25519 (private) and id_ed25519.pub (public). Verify their existence with ls -l ~/.ssh.
Installing the Public Key on the Remote Server
Once the keys are generated, the next step is to place the public key onto the remote server. The most reliable method is using the ssh-copy-id utility, which handles formatting and permissions automatically.
First, ensure your SSH client can connect to the server with a password so that authentication works for this initial setup session:
ssh username@server_ip_address
Once logged in, run the following command from your local machine:
ssh-copy-id username@server_ip_address
This will prompt you to enter the password for the remote account once. The tool will then append your public key to the ~/.ssh/authorized_keys file on the server. If ssh-copy-id is unavailable (common on some minimal Linux distributions), you can manually copy the contents of your public key file (id_ed25519.pub) and paste it into the authorized_keys file on the remote server using a text editor like nano or vim.
Verifying Passwordless Authentication
After installing the key, always test the connection to ensure passwordless login works before relying on it for daily work. Run this command:
ssh username@server_ip_address
If configured correctly, the terminal should log you in immediately without prompting for a password. If you set a passphrase during key generation, you will be prompted to enter that passphrase once per session (or every time if your SSH agent is not running).
If the connection fails or prompts for a password despite the key being installed, check the following common issues:
1. Permissions: Ensure the ~/.ssh directory and files on the remote server have strict permissions. The directory should be 700, and the authorized_keys file should be 600. Incorrect permissions often cause SSH to ignore the key for security reasons.
2. Key Format: Older systems might require keys in OpenSSH format rather than PEM or PuTTYgen formats. Ensure you are using the standard .pub file generated by ssh-keygen.
3. Multiple Keys: If multiple keys exist in authorized_keys, ensure the one you just added is the correct one and not overridden by an earlier entry with different permissions.
Managing SSH Agent for Seamless Usage
To avoid entering your passphrase every time you open a new terminal window, you can use the SSH authentication agent. The agent holds decrypted private keys in memory, allowing multiple sessions to authenticate without re-entering the password.
Start the agent and add your key using these commands:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
The system will prompt you for the passphrase once. Once added, the agent manages the key until it is removed or the session ends. On Linux systems like Ubuntu or Fedora, services like gnome-keyring or kwallet-pam can automatically start the agent when you log in to your desktop environment.
Common Mistakes and Troubleshooting
Even with a straightforward process, several pitfalls can prevent passwordless SSH from working. One frequent error involves file permissions on the remote server. If the ~/.ssh folder is writable by group or others (e.g., permissions set to 755 instead of 700), OpenSSH will discard the public key for security reasons. Always run chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys on the remote machine after installation.
Another issue arises when using Windows Subsystem for Linux (WSL) or certain IDEs like VS Code with the Remote SSH extension. These environments sometimes use a different SSH configuration path or agent socket. Ensure your local environment variables are set correctly and that you are connecting to the actual server IP, not a forwarded port that might have its own authentication rules.
Additionally, be wary of legacy systems still using PasswordAuthentication yes in their /etc/ssh/sshd_config. While this allows password logins, it can sometimes interfere with key-based authentication if the configuration isn’t updated to prioritize keys over passwords (PasswordAuthentication no). Always review the server logs (/var/log/auth.log or /var/log/secure) for messages indicating “publickey” versus “password” authentication successes.
Conclusion
Setting up SSH keys transforms your remote access workflow from a fragile, password-dependent process into a secure, automated routine. By generating an Ed25519 key pair, securely installing the public key on your server, and utilizing the SSH agent, you eliminate the risk of password leaks while speeding up your daily tasks. Remember that terms, availability, eligibility, laws, and prices depend on insurer, policy and jurisdiction; similarly, SSH configurations vary by OS version and distribution. Always verify your specific setup against official documentation for your Linux distribution or cloud provider to ensure compliance with local security policies and best practices.



