SSH Keys vs 2FA: Securing Your VPS
To secure your VPS, use SSH keys as your primary authentication method and add two-factor authentication (2FA) as an extra layer—together they protect against both stolen credentials and lost key files. This article explains how each works, their strengths and weaknesses, and how to combine them effectively.
Background: Why VPS Security Matters
A Virtual Private Server (VPS) is a Linux-based virtual machine that runs your websites, applications, or services. Because it is directly exposed to the internet, it is constantly probed by automated bots that try to guess passwords or exploit weak authentication. The default SSH password login is a common entry point for attackers. Using only a password—even a strong one—leaves your server vulnerable to brute-force attacks, credential stuffing, and phishing. SSH keys and 2FA address different weaknesses, and understanding their roles helps you build a robust defense.
How SSH Keys Work
SSH keys are a cryptographic pair: a private key stored on your local machine and a public key placed on the server. When you connect, the server verifies that you possess the private key corresponding to the public key. This is done through a challenge-response handshake—no password is sent over the network. The private key is typically protected by a passphrase, so even if someone steals the key file, they still need the passphrase to use it.
- Key generation: Use
ssh-keygen -t ed25519to create a modern, secure key pair. - Key installation: Copy the public key to the server’s
~/.ssh/authorized_keysfile usingssh-copy-id. - Key types: Ed25519 is recommended for its security and speed; RSA with 3072 or 4096 bits is also acceptable.
SSH keys are resistant to brute-force attacks because guessing a private key is computationally infeasible. However, they are only as secure as the device storing the private key. If your laptop is compromised, the attacker can use your key.
How 2FA Works for SSH
Two-factor authentication adds a second verification step after you present your first credential. For SSH, this usually means after entering your password or using your SSH key, you must also provide a one-time code from an authenticator app (TOTP) or a hardware token like a YubiKey. The server runs a PAM module (e.g., pam_google_authenticator) that checks the code. Even if an attacker steals your password or key, they cannot log in without the second factor.
- TOTP: Time-based one-time passwords, generated by apps like Google Authenticator or Authy, change every 30 seconds.
- Hardware tokens: U2F/FIDO2 devices provide phishing-resistant authentication and are more secure than TOTP.
2FA protects against credential theft, but it can be bypassed by sophisticated phishing attacks that intercept the code in real time. It also adds friction to every login, which is why some administrators use it only for password logins or for sudo commands.
Comparing SSH Keys and 2FA
The two methods protect against different threats. SSH keys are strong against remote brute-force attacks because there is no password to guess. However, they are vulnerable to local device compromise—if your machine is infected with malware, the attacker can use your key. 2FA protects against stolen credentials but not against a compromised device that can intercept the code. In practice, using both is the most secure approach: the SSH key proves you have the private key, and 2FA proves you are the legitimate user, not just someone who stole the key.
For most VPS administrators, SSH keys alone are sufficient if the private key is stored securely and protected by a strong passphrase. Adding 2FA is recommended for high-security environments, such as servers holding sensitive customer data or financial information. The trade-off is convenience: every login requires the extra step of entering a code.
How to Choose What to Use
Your choice depends on your threat model and how you access the server. If you are the only user and your devices are secure, SSH keys with a passphrase are a solid baseline. If you have multiple administrators or users, consider enforcing 2FA for all SSH logins to prevent unauthorized access from stolen keys. For servers managed by a team, you might also use a jump host or a bastion that requires both key and 2FA before reaching internal servers.
- Single user, personal VPS: SSH keys with passphrase, disable password login.
- Team access: SSH keys plus 2FA (TOTP or hardware token) for every user.
- High-risk data: Hardware tokens (U2F) and disable password login entirely.
Remember that 2FA is not a replacement for SSH keys; it is an additional layer. Disabling password login is a critical step regardless—it eliminates the most common attack vector.
Common Problems and Misconceptions
One common misconception is that 2FA makes passwords safe to use. While 2FA protects against credential theft, it does not prevent brute-force attacks on the password itself—the server still processes the password attempt. For this reason, you should always disable password authentication once SSH keys are in place. Another issue is losing your private key or 2FA device. Without a recovery plan, you can lock yourself out of your server. Always keep a backup of your private key in a secure location and have a recovery code for 2FA (most TOTP apps provide backup codes).
Another problem is key management. If you have many servers, copying the same public key everywhere is convenient but risky—if one server is compromised, the attacker can use that key on all others. Use separate keys for different security levels or use an SSH agent with a master key. Also, ensure your server’s time is synchronized (NTP) because TOTP codes rely on time—a clock drift can cause authentication failures.
Setting Up SSH Keys and 2FA
Here is a practical guide to securing a typical Linux VPS (Ubuntu/Debian).
- Generate an SSH key pair on your local machine:
ssh-keygen -t ed25519 -f ~/.ssh/vps_key. - Copy the public key to the server:
ssh-copy-id -i ~/.ssh/vps_key.pub user@server_ip. - Test key login:
ssh -i ~/.ssh/vps_key user@server_ip. - Install 2FA on the server:
sudo apt install libpam-google-authenticatorand rungoogle-authenticatorto set up TOTP for your user. - Configure SSH to use 2FA: Edit
/etc/ssh/sshd_configto setChallengeResponseAuthentication yesandAuthenticationMethods publickey,keyboard-interactive. - Restart SSH:
sudo systemctl restart sshd(do not close your current session until you test the new login). - Disable password login: Set
PasswordAuthentication noinsshd_configand restart SSH again.
Always keep a second terminal session open while testing to avoid locking yourself out. If something fails, you can revert the changes.
Frequently Asked Questions
Can I use 2FA without SSH keys? Yes, you can configure SSH to require a password plus a TOTP code. However, this is less secure than using keys because passwords are still susceptible to brute-force attacks. If you use 2FA with passwords, enable fail2ban to block repeated attempts.
What if I lose my SSH private key? If you lose your private key, you cannot log in with that key. You can still log in if you have another authentication method (like a password or another key). If you have no other method, you must use the VPS provider’s web console to reset access. Always keep a backup of the key in a safe place, such as an encrypted USB drive.
Is TOTP 2FA secure enough for a VPS? TOTP is widely used and secure for most threat models. It is vulnerable to phishing attacks that intercept the code in real time, but such attacks require targeting you specifically. For most users, TOTP is a strong second factor. If you face advanced threats, consider hardware tokens like YubiKey.
Should I use a passphrase on my SSH key? Yes, always use a passphrase. It encrypts the private key on disk, so if someone copies the file, they cannot use it without the passphrase. The passphrase is not sent over the network; it only decrypts the key locally.
Can I use the same SSH key for multiple servers? You can, but it increases the risk: if one server is compromised, the attacker has the key to all of them. Use separate keys for different security domains or use an SSH agent with a master key and per-server keys.
What is the difference between 2FA and MFA? 2FA is a subset of MFA—it uses exactly two factors. MFA can use more than two. In practice, for SSH, you typically use two factors: something you have (key or phone) and something you know (passphrase or code).
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.