These tutorials are written by Freedom Lab members in their free time. If you find them helpful, please consider supporting our work.

TutorialsTwo-Factor Authentication for Linux Login

Two-Factor Authentication for Linux Login

Linux Security

Add 2FA to your Linux desktop for enhanced security.

This guide covers setting up Google Authenticator for:

Prerequisites

Keep a root terminal open while configuring. If something goes wrong, you could lock yourself out.

Install Google Authenticator

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

Configure Authenticator for Your User

Run the setup as your normal user (not root):

google-authenticator

Answer the prompts:

Scan the QR code with your authenticator app and save the emergency backup codes.

Configure PAM for Desktop Login

Edit the GDM PAM configuration:

sudo vim /etc/pam.d/gdm-password

Add this line at the very top of the file:

auth required pam_google_authenticator.so

We use required without nullok to enforce 2FA for all users.

Configure PAM for TTY Login

Edit the login PAM configuration:

sudo vim /etc/pam.d/login

Add this line at the very top of the file:

auth required pam_google_authenticator.so

This protects console logins (Ctrl+Alt+F2, etc.).

Apply Changes

Restart GDM to apply (this will log you out):

sudo systemctl restart gdm

Login Process

  1. At the login screen, enter your verification code first
  2. Then enter your password

This applies to:

Troubleshooting

Locked Out

If you're locked out, boot into recovery mode:

  1. Reboot and hold Shift (BIOS) or Esc (UEFI) during boot
  2. Select Advanced optionsRecovery mode
  3. Choose Root shell
  4. Remount filesystem as writable: mount -o remount,rw /
  5. Remove the 2FA line from PAM configs:
    nano /etc/pam.d/gdm-password
    nano /etc/pam.d/login
  6. Reboot: reboot

Clock Sync Issues

TOTP codes are time-sensitive. If codes aren't working:

sudo apt install ntp
sudo systemctl enable ntp
sudo systemctl start ntp

Multiple Users

Each user needs to run google-authenticator individually to set up their own 2FA.

Security Notes