If you need to log in to multiple cloud servers daily, or frequently switch between GitHub, GitLab, and various remote hosts, manually entering long, complex passwords every single time is not only highly inefficient but also makes automation via scripts impossible.
More dangerously, password-based authentication is a primary target for brute-force attacks. Transitioning your servers to key-based authentication—combined with an SSH agent and multi-identity management—not only eliminates the hassle of typing passwords but also significantly elevates your server security. Today, we will resolve both the usability and security issues surrounding SSH logins.
Why should you abandon password-based logins?
Password-based SSH authentication suffers from two fatal flaws: it is insecure and cumbersome.
Passwords can be compromised through exhaustive search (brute-force), dictionary attacks, or interception via man-in-the-middle attacks. In contrast, SSH keys utilize asymmetric encryption (such as RSA or the more modern Ed25519), making them exponentially harder to crack. Furthermore, once keys are configured, connecting to a server becomes as simple as running a single command—`ssh my-server`—with no further interaction required. This is essential for writing automated backup or deployment scripts.
Generating a robust "digital key"
First, you need to generate a key pair on your local machine: a public key (which can be shared and placed on servers) and a private key (which must be kept strictly confidential on your local device).
Open your local terminal and enter the following command:
ssh-keygen -t ed25519 -C "your_email@example.com"
I strongly recommend using the `ed25519` algorithm; it is shorter, faster, and significantly more secure than the older RSA standard. The system will prompt you for a file path (you can simply press Enter to accept the default) and suggest setting a "passphrase." If you are concerned about the private key file being leaked, setting a passphrase is a good idea; however, if you prioritize a completely seamless, password-free experience, you can simply press Enter to leave it blank. Once generated, two files will appear in your `~/.ssh/` directory: `id_ed25519` (the private key) and `id_ed25519.pub` (the public key).
"Pinning" the public key to the server
After obtaining the public key, you need to add it to the server’s "allowlist." The quickest way to do this is by using the `ssh-copy-id` tool (assuming you are still able to log in to the server using a password):
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-IP
Once you enter your password, the public key will be appended to the `~/.ssh/authorized_keys` file on the server.
Now, try running `ssh user@server-IP`. If you didn't set a passphrase when generating your keys, you should be able to connect to the server instantly.
Introducing SSH Agent: Let your terminal remember your private key
If you set a passphrase when generating your keys, you would normally have to enter it every time you connect to a server—which defeats the purpose of "passwordless" access. SSH Agent is designed to solve this: it remembers your decrypted private key in the background.
On Linux or macOS, start the agent and add your private key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Once this is done, you only need to enter your passphrase once after starting your system; the agent will automatically handle all subsequent SSH connections, saving you the trouble of manual entry. (macOS users can also add `UseKeychain yes` to their `~/.ssh/config` file to store the passphrase permanently in the system Keychain.)
Managing multiple identities: Using the `config` file as a "dispatcher"
As the number of servers you manage grows—or if you use multiple cloud providers or several GitHub accounts—you may run into a dilemma: using the same key for everything is insecure, but generating a separate key for each one forces you to manually specify the `-i` parameter every time you connect.
This is where the `~/.ssh/config` file comes to the rescue. Create a `config` file in the `~/.ssh` directory on your local computer (if it does not already exist):
```ini
# Personal Blog Server
Host my-blog
HostName 192.0.2.1
User root
IdentityFile ~/.ssh/id_ed25519_blog
IdentitiesOnly yes
# GitHub Work Account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_work
IdentitiesOnly yes
# Test Server
Host test-server
HostName 198.51.100.2
User ubuntu
IdentityFile ~/.ssh/id_ed25519_test
Once configured, typing `ssh my-blog` will prompt the SSH client to automatically use the specified private key and connect as the `root` user—no additional parameters required. The `IdentitiesOnly yes` directive is crucial; it prevents the SSH client from attempting to use other, irrelevant keys, which helps avoid triggering "too many authentication attempts" limits on the server side.
The Ultimate Security Barrier: Disabling Password Authentication
Once you have verified that all your keys are configured correctly and you can successfully log in without a password, the final step is to completely disable password-based authentication on the server—effectively rendering any hacker's dictionary-attack wordlists useless.
Log in to your server and edit the `/etc/ssh/sshd_config` file:
```ini
PasswordAuthentication no
PubkeyAuthentication yes
After saving the changes, restart the SSH service: `sudo systemctl restart sshd`.
Important: Before restarting, keep your current session active. Open a new terminal window and verify that you can successfully log in using your key-based credentials before closing the original session.
Seamless Passwordless Login Requires a Stable Network Foundation
Once you have everything configured, your local keystrokes will reach the server's command prompt in a fraction of a second—a massive boost to efficiency. However, if your server's network connection becomes highly unstable during evening peak hours, even with key-based authentication, your terminal might hang for tens of seconds during the connection phase, or you may experience frequent disconnections while transferring files. Frequently Asked Questions (FAQ)
Q: What should I do if I accidentally delete my local private key and can no longer access the server?
A: You will need to log in to the server via your cloud platform’s VNC/Web console (out-of-band management) and append a new public key to the `authorized_keys` file. Therefore, it is crucial to keep your local private key secure.
Q: What should I do if `ssh-agent` stops working after I restart my computer?
A: On Linux/macOS, you can configure `~/.bashrc` or `~/.zshrc` to have it start automatically and load your keys. For Windows users utilizing the built-in OpenSSH client, you must enable the "OpenSSH Authentication Agent" service in your system services and set it to start automatically.
Q: How many public keys can be configured on a single server?
A: There is no strict theoretical limit. You can append public keys from different computers or team members to the `authorized_keys` file, with each key placed on a separate line. If a computer is lost, you can simply remove the corresponding line from the file on the server to revoke access.
EN
CN