How to Enable 2FA/TOTP on Your VPS for Stronger Security

How to Enable 2FA/TOTP on Your VPS for Stronger Security

warning-9.png

Securing your VPS is more important than ever as cyberattacks continue to grow in complexity. While passwords and SSH keys offer protection, they still leave room for unauthorized access—especially if credentials are stolen or leaked. That’s where Two-Factor Authentication (2FA) and Time-based One-Time Passwords (TOTP) come into play. Enabling TOTP adds a powerful extra layer of security by requiring a temporary, time-limited code each time you log in. This guide explains how TOTP works and how you can enable it on your VPS for maximum protection.


1. What Is TOTP and Why It Matters

TOTP (Time-based One-Time Password) is a secure authentication system that generates a new code every 30 seconds. These codes are produced by an authenticator app such as:

  • Google Authenticator
  • Authy
  • Microsoft Authenticator
  • Bitwarden
  • 1Password

When you enable TOTP 2FA on your VPS, logging in requires both:

  1. Your SSH key or password
  2. A unique, temporary code from your authenticator app

This means that even if someone obtains your password or key, they still cannot access your server without the TOTP code. This drastically reduces the risk of brute-force attacks, credential leaks, and unauthorized logins.


2. Requirements for Enabling TOTP on Your VPS

Before setting up TOTP on your server, ensure the following:

  • You have SSH access to your VPS
  • You have sudo privileges
  • You can install new packages
  • You have an authenticator app ready

It’s also wise to create an AVIOX Cloud snapshot before enabling 2FA. If you accidentally misconfigure something and lock yourself out, you can restore your VPS instantly.


3. Installing Google Authenticator Module on Linux

Most VPS servers running Ubuntu or Debian use the PAM (Pluggable Authentication Modules) system for authentication. This makes TOTP setup straightforward.

Install the Google Authenticator Package

 
sudo apt update sudo apt install libpam - google - authenticator - y 

Generate Your Unique Secret Key

Run:

 
google-authenticator 

You will see:

  • A unique QR code
  • Your secret key
  • Emergency backup codes
  • Security settings prompts

Scan the QR code with your authenticator app. Save the backup codes safely.

For each prompt, recommended answers:

  • “Time-based tokens?” → Yes
  • “Disallow multiple uses?” → Yes
  • “Increase time window?” → No
  • “Enable rate limiting?” → Yes

4. Configuring PAM for TOTP

To enable the system to require TOTP, edit the PAM configuration:

 
sudo nano /etc/pam.d/sshd 

Add this line at the top:

 
auth required pam_google_authenticator.so 

Save and exit.


5. Updating SSH Configuration

Now tell SSH to also ask for a verification code.

Open SSH config:

 
sudo nano /etc/ssh/sshd_config 

Find and edit:

 
ChallengeResponseAuthentication yes  

Ensure the following line is enabled:

 
KbdInteractiveAuthentication yes  

If you want SSH keys + TOTP (recommended), set:

 
PasswordAuthentication no  

This enforces the strongest combination:
SSH Key + TOTP Code

Restart SSH:

 
sudo systemctl restart ssh 

6. Testing Your New Two-Factor Login

IMPORTANT: Keep your current SSH session open before testing.

In a new terminal window, connect again:

 
ssh username @your_vps_ip  

You should now see:

  1. SSH key check (or password if enabled)
  2. TOTP code prompt:
 
Verification code : 

Enter the code from your authenticator app.
If it works, congratulations — 2FA is now active on your VPS!

If it fails, use the original session to revert your changes.


7. Additional Hardening Tips with TOTP

1. Disable Root Login Completely

 
sudo nano /etc/ssh/sshd_config PermitRootLogin no  

2. Use Fail2ban for Added Protection

 
sudo apt install fail2ban -y 

3. Limit SSH to Specific IPs (optional)

Using UFW:

 
sudo ufw allow from YOUR_IP to any port 22  

4. Enable Snapshots on AVIOX Cloud

Before any security configuration, take a snapshot for quick rollback.


8. When Should You Use TOTP 2FA on a VPS?

TOTP is strongly recommended for:

  • Developers managing production servers
  • VPS hosting websites or APIs
  • E-commerce and business apps
  • High-traffic apps
  • Servers storing user data
  • Any VPS with password login enabled

Even if you rely solely on SSH keys, adding TOTP gives unbeatable security.


9. What If You Lose Your Authenticator Device?

That’s why backup codes exist.
Store them:

  • Offline
  • In a password manager
  • Printed and locked away

If you use AVIOX Cloud, you can also reset configurations using the console panel.


Conclusion

Enabling TOTP on your VPS is one of the most powerful ways to strengthen security with minimal effort. A VPS protected by SSH keys plus TOTP becomes nearly impossible for attackers to compromise. It’s simple to set up, easy to use, and significantly reduces the risk of unauthorized access. Combine TOTP with AVIOX Cloud’s snapshots, firewalls, and hardened infrastructure for maximum security and peace of mind.


Share:


Your experience on this site will be improved by allowing cookies Cookie Policy