SSH Security Upgrade: Migrating to Key-Based Auth with Hardware Tokens (2025)
Password-based SSH logins are vulnerable to brute-force attacks. Migrating to SSH key-based authentication is a fundamental step in secure ssh connection best practices. This guide covers the ssh key authentication setup linux process, how to disable ssh password authentication, and takes it a step further by explaining how to leverage hardware security tokens like YubiKeys for even greater security.
Why SSH Keys are More Secure than Passwords
- Complexity: SSH keys are significantly longer and more complex than human-memorable passwords, making them practically impossible to brute-force.
- No Password Transmission: The private key never leaves your client machine during authentication. The server only needs the public key.
- Automation: Keys facilitate secure automated logins for scripts and services.
Step 1: Generating Your SSH Key Pair
On your client machine (the one you connect from), open a terminal.
# Recommended: Use ED25519 algorithm (modern, secure, efficient)ssh-keygen -t ed25519 -C "your_email@example.com"
# Or, use RSA (ensure sufficient key length)# ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-t ed25519
: Specifies the key type. ED25519 is generally preferred over RSA today.-b 4096
: (If using RSA) Specifies a strong key length.-C "your_email@example.com"
: A comment to help identify the key (optional but good practice).
Key Prompts:
- File location: Press Enter to accept the default (
~/.ssh/id_ed25519
or~/.ssh/id_rsa
). - Passphrase: Highly Recommended! Enter a strong passphrase. This encrypts your private
key on your disk. If someone steals your private key file, they still need the passphrase to
use it. You’ll need to enter this passphrase once per session when using the key (or use an
ssh-agent
).
This creates two files:
~/.ssh/id_ed25519
(orid_rsa
): Your private key. Keep this secret and secure!~/.ssh/id_ed25519.pub
(orid_rsa.pub
): Your public key. This is what you copy to servers.
Step 2: Copying Your Public Key to the Server
You need to add the content of your public key file (id_ed25519.pub
) to the
~/.ssh/authorized_keys
file on the server you want to connect to, under the user account you
want to log in as.
Method 1: Using ssh-copy-id
(Easiest)
If you still have password access to the server, this command automates the process:
ssh-copy-id user@your_server_ip_or_hostname
It will prompt for the user’s password on the server one last time to copy the key.
Method 2: Manual Copy
- Display your public key on your client machine:
Terminal window cat ~/.ssh/id_ed25519.pub - Copy the entire output (it’s one long line starting with
ssh-ed25519 ...
). - SSH into your server using your password:
Terminal window ssh user@your_server_ip_or_hostname - On the server, create the
.ssh
directory if it doesn’t exist and set permissions:Terminal window mkdir -p ~/.sshchmod 700 ~/.ssh - Append your public key to the
authorized_keys
file (create it if needed) and set permissions:Terminal window echo "<paste-your-public-key-here>" >> ~/.ssh/authorized_keyschmod 600 ~/.ssh/authorized_keys - Exit the server session (
exit
).
Step 3: Testing Key-Based Login
Try logging into the server again:
ssh user@your_server_ip_or_hostname
- If you set a passphrase for your key, you’ll be prompted to enter it.
- If successful, you should log in without being asked for the server user’s password.
Step 4: Disabling Password Authentication on the Server (Crucial!)
Once you’ve confirmed key-based login works reliably, you must disable password authentication on the server for improved security.
- SSH into your server (using your key).
- Edit the SSH daemon configuration file (usually
/etc/ssh/sshd_config
) with root privileges (e.g.,sudo nano /etc/ssh/sshd_config
). - Find the following lines (or add them if they don’t exist) and ensure they are set as shown:
PasswordAuthentication noPubkeyAuthentication yesChallengeResponseAuthentication no # Often needed to fully disable passwordsUsePAM no # Optional, but can prevent PAM modules from allowing passwords
- Save the file and exit the editor.
- Validate the configuration:
Terminal window sudo sshd -t - If the syntax is OK, restart the SSH service:
Terminal window sudo systemctl restart sshd # For systemd systems (most modern Linux)# or# sudo service ssh restart # For older init systems
Important: Before logging out, open a new terminal window and try SSHing into the server again with your key to ensure you didn’t lock yourself out! If it works, password authentication is now disabled.
Step 5 (Optional but Recommended): Using Hardware Security Keys (YubiKey, etc.)
Storing your SSH private key directly on your computer means it could be compromised if your machine is infected with malware. Hardware security keys add a physical layer of security. The private key is generated on and never leaves the hardware token.
Methods for using YubiKey (or similar FIDO2/PIV tokens) for SSH:
-
FIDO2/U2F Authentication (Recommended, Modern):
-
Key Generation: Generate a special type of SSH key directly tied to your hardware key.
Terminal window # Generate an ed25519-sk (FIDO/U2F) keyssh-keygen -t ed25519-sk -C "your_email@example.com_yubikey"# Or ecdsa-sk# ssh-keygen -t ecdsa-sk -C "your_email@example.com_yubikey"You will be prompted to touch your hardware key during generation.
-
How it works: Creates
id_ed25519_sk
andid_ed25519_sk.pub
(orecdsa_sk
). The private key file (id_ed25519_sk
) doesn’t contain the actual private key, but rather information needed to interact with the hardware token. -
Server Support: Requires OpenSSH 8.2+ on both client and server.
-
Usage: Copy the
.pub
key to the server’sauthorized_keys
file as before. When you SSH, you’ll be prompted to touch your hardware key to authorize the login. -
This is the FIDO2 ssh authentication guide approach.
-
-
PIV (Personal Identity Verification) Mode:
- Key Generation: Use the YubiKey Manager tool or
ykman
CLI to generate a key pair within one of the PIV slots (e.g., Authentication, Signing). - How it works: The private key resides securely within the YubiKey’s PIV applet. You use
ssh-agent
with a library that can talk to the PIV applet (likeopensc-pkcs11.so
) to make the key available for SSH authentication. - Setup: More complex, involves configuring
ssh-agent
and potentially OpenSC. - Usage: Add the public key (retrieved using
ssh-add -L
after configuring the agent) to the server’sauthorized_keys
. Requires the YubiKey to be plugged in and potentially the PIV PIN entered into the agent.
- Key Generation: Use the YubiKey Manager tool or
-
GPG Agent: Use GnuPG and
gpg-agent
as a substitute forssh-agent
. You can generate keys on the YubiKey via GPG and configuregpg-agent
to handle SSH authentication.
Using YubiKey for ssh authentication via FIDO2 (ed25519-sk
or ecdsa-sk
) is generally the
simplest and most modern approach if your OpenSSH versions support it.

YubiKey 5 Series
amazon.comHardware security keys from Yubico supporting multiple authentication protocols including FIDO2, U2F, and PIV
Managing Multiple Keys and SSH Agent
-
Multiple Keys: You can have multiple key pairs (e.g., one per client device, one per hardware token). Simply generate them with different filenames (
ssh-keygen -f ~/.ssh/my_other_key ...
). -
ssh-agent
: This helper program keeps your decrypted private keys in memory, so you only need to enter your passphrase once per session.- Start the agent:
eval $(ssh-agent -s)
- Add your key:
ssh-add ~/.ssh/id_ed25519
(will prompt for passphrase) - List added keys:
ssh-add -l
- Add hardware key (PIV example):
ssh-add -s /path/to/opensc-pkcs11.so
- Start the agent:
-
SSH Config File (
~/.ssh/config
): Simplify connections by defining hosts and specifying which key to use for each:Host my-serverHostName your_server_ip_or_hostnameUser your_userIdentityFile ~/.ssh/id_ed25519Host work-serverHostName work.example.comUser work_userIdentityFile ~/.ssh/work_key_ed25519_skIdentitiesOnly yes # Prevent trying default keysNow you can just type
ssh my-server
.
Conclusion
Migrating from passwords to SSH keys is a vital security enhancement. Generating a strong key pair
(preferably ED25519), copying the public key correctly, and crucially, disabling ssh password
authentication on your server significantly hardens your defenses. For the highest level of
security, using YubiKey for ssh authentication (or other hardware tokens) via FIDO2 or PIV ensures
your private keys cannot be easily stolen from your client machine. Properly managing multiple ssh
keys securely using ssh-agent
and the SSH config file makes the process convenient without
sacrificing security.