Why is a Sudo Password Not Enough?
I once had to stay up all night dealing with an SSH brute-force attack because I was complacent. Many believe that long passwords and changing the SSH port are enough. However, if a server has a Web Shell vulnerability, hackers can execute commands as a regular user. Their next goal is always Privilege Escalation to gain Root access via the sudo command.
According to security reports, most successful Linux attacks exploit weak or leaked passwords. Even if you’ve enabled 2FA for SSH, hackers can still hijack an active session. Installing Google Authenticator for sudo creates a physical layer of protection. Hackers have the password but not your phone? They’ll be stopped immediately.
How PAM and Google Authenticator Work
On Linux, all authentication activities like login or sudo are managed by PAM (Pluggable Authentication Modules). Think of PAM as a flexible gatekeeper. You can add or remove check rules as you wish without modifying the system’s code.
By integrating the pam_google_authenticator module, we add a condition to this process. PAM will require: “In addition to the correct password, provide a 6-digit token.” If either is missing, the door to Root access remains locked.
Detailed Implementation Steps
Step 1: Install Library Packages
First, install the necessary PAM module. For Ubuntu or Debian, use the command:
sudo apt update
sudo apt install libpam-google-authenticator -y
If you are using CentOS or RHEL, enable the EPEL repository before installing:
sudo yum install epel-release -y
sudo yum install google-authenticator -y
Step 2: Initialize 2FA for the User
You need to run this command as a Regular User; absolutely do not run it directly as root.
google-authenticator
The system will present several options; I recommend the following configuration for the best security:
- Time-based tokens (y/n): Select
yso the OTP changes every 30 seconds. - Scan the QR code that appears using the Google Authenticator or Authy app on your phone.
- Update your “/home/user/.google_authenticator” file? Select
yto save the configuration. - Disallow multiple uses of the same token? Select
yto prevent replay attacks. - Rate-limiting? Select
yto block continuous OTP guessing attempts.
Note: Make sure to copy the 5 emergency scratch codes. If your phone is broken or lost, this is the only way to regain access.
Step 3: Configure PAM for the Sudo Command
This is the crucial step. Be careful, as a small mistake could lock you out of sudo privileges permanently.
sudo nano /etc/pam.d/sudo
Add the following line to the very top of the file content:
auth required pam_google_authenticator.so
The required keyword forces the user to enter the correct OTP to proceed. If you want to apply it gradually, you can use nullok at the end of the line. This option allows users who haven’t set up 2FA yet to use sudo temporarily.
The complete file will look similar to this:
#%PAM-1.0
auth required pam_google_authenticator.so
@include common-auth
@include common-account
@include common-session-noninteractive
Step 4: Testing (Don’t Close the Old Terminal!)
Don’t rush to close the current window. Open a new Terminal and try typing:
sudo su -
At this point, the system will ask for a Verification code. Enter the 6 digits from the app on your phone. If you gain root access, your system is now securely protected.
Pro Tips to Avoid Getting Locked Out
Working with security can sometimes cause issues. Here are a few things to keep in mind:
- Time Synchronization: OTP is based on real-time. If the server clock is off by more than 30 seconds, the code will be wrong. Install
chronyto keep the server time accurate. - Authentication Order: If you want to enter the password first and then the OTP, move the
auth requiredline below the@include common-authline. - Lost Phone: If you lose both your phone and backup codes, you’ll need to use the VPS control panel (Console) to remove the configuration line in the PAM file.
Conclusion
Setting up 2FA for sudo is a practical way to implement a Defense-in-Depth strategy. It only takes 10 minutes to set up, but it helps you sleep much better at night. Don’t wait until you see strange logs to start worrying. Protect your server today.

