2FA for Linux Desktop Logins: Step-by-Step Setup
In this article
Setting up two-factor authentication (2FA) for your Linux desktop login adds a second layer of security beyond your password. With 2FA enabled, logging in requires both your password and a one-time code from an authenticator app on your phone. This guide shows you how to configure this using the standard PAM (Pluggable Authentication Modules) system with the google-authenticator package, which works with most Linux distributions.
What You'll Need
- A Linux desktop with sudo/root access
- An authenticator app on your phone (Google Authenticator, Authy, or FreeOTP)
- Internet access to install the package (or the package already installed)
Step-by-Step
- Install the PAM module. Open a terminal and run the package manager command for your distribution:
- Debian/Ubuntu:
sudo apt install libpam-google-authenticator - Fedora:
sudo dnf install google-authenticator - Arch:
sudo pacman -S libpam-google-authenticator
- Debian/Ubuntu:
- Run the setup tool. In the terminal, run
google-authenticator. It will ask a series of questions. Answer them as follows:- "Do you want authentication tokens to be time-based" – type
yand press Enter. - It will display a QR code and a secret key. Open your authenticator app, choose "Add account" or "Scan QR code," and scan the code. If scanning fails, manually enter the secret key.
- "Do you want me to update your "/home/username/.google_authenticator" file" – type
y. - "Do you want to disallow multiple uses of the same authentication token" – type
y. - "Do you want to enable rate-limiting" – type
y. This locks the account after too many failed attempts. - "Do you want to enable time-skew correction" – type
n(default) unless you have time sync issues.
- "Do you want authentication tokens to be time-based" – type
- Test the code. Before configuring PAM, verify the 6-digit code from your authenticator app works by running
google-authenticatoragain and entering the code when prompted. If it fails, check your phone's time settings and resync. - Edit the PAM configuration. Open the PAM login configuration file with sudo. The exact file depends on your display manager:
- For GDM (GNOME):
sudo nano /etc/pam.d/gdm-password - For SDDM (KDE Plasma):
sudo nano /etc/pam.d/sddm - For LightDM:
sudo nano /etc/pam.d/lightdm - If you use a different display manager, find its PAM file in
/etc/pam.d/(e.g.,lxdm,xdm).
authlines):auth required pam_google_authenticator.so
Save and exit (in nano: Ctrl+O, Enter, Ctrl+X). - For GDM (GNOME):
- Configure PAM to work with passwords. By default, the above line will require the 2FA code and then the password. To make it more secure, you can require both in sequence. Add this line instead of the previous one:
auth required pam_google_authenticator.so nullok
Thenullokoption allows users without a.google_authenticatorfile to still log in (useful for the first login after setup). For strict enforcement, removenullokafter all users have set up 2FA. - Test the login. Log out of your session and try logging back in. You should be prompted for your password first, then the 6-digit code. If you see only one prompt, check that the PAM line is in the correct file and that the module is loaded.
- Set up recovery codes. When you ran
google-authenticator, it displayed 5 emergency scratch codes. Write these down and store them in a safe place. They can be used once each if you lose your phone.
Common Mistakes
- Editing the wrong PAM file. Each display manager uses its own file. If you edit
loginorsystem-auth, it may not affect the GUI login. Always edit the file specific to your display manager. - Forgetting the
nullokoption. Without it, if the.google_authenticatorfile is missing or corrupted, you'll be locked out completely. - Not testing before logging out. Always test the 2FA code with
google-authenticatorbefore applying PAM changes. A typo in the secret key will lock you out. - Using a different time zone on your phone. The code is time-based; if your phone's clock is off by more than a minute, the codes will fail. Ensure automatic time sync is enabled.
Tips
- Keep a backup of the secret key or scratch codes in a password manager or a printed paper stored securely.
- If you have multiple users on the system, each user must run
google-authenticatorindividually to create their own.google_authenticatorfile. - For SSH logins, you can also enable 2FA by adding the same PAM line to
/etc/pam.d/sshdand settingChallengeResponseAuthentication yesinsshd_config, but this guide focuses on desktop login only. - If you use a display manager that supports it, you can set up fingerprint or smartcard login as an alternative, but 2FA is still recommended.
Troubleshooting
If you're locked out of your desktop:
- Switch to a virtual terminal by pressing
Ctrl+Alt+F2(or F3-F6). Log in with your username and password. Note: this terminal login may not have 2FA if you only edited the display manager's PAM file. - Once in the terminal, edit the PAM file you changed and remove the line you added. Then reboot or restart the display manager.
- If you forgot the scratch codes and can't get in, boot into recovery mode (from GRUB, select "Advanced options" then "Recovery mode"), and remove the PAM line from a root shell.
If the code is rejected but the app shows a valid code:
- Check your phone's time settings. Enable automatic time sync and wait a minute.
- Run
google-authenticatoragain and choose "yes" to time-skew correction when prompted. - Ensure you're entering the code within the 30-second window.
If the PAM module isn't found:
- Verify the package installed correctly with
dpkg -l | grep google-authenticator(Debian) orrpm -qa | grep google-authenticator(Fedora). - Reinstall the package if needed.
Frequently Asked Questions
Will 2FA work with fingerprint or face unlock?
No, this setup only applies to password-based logins. If your display manager supports biometrics, those are separate and won't be affected by the PAM 2FA.
Can I use the same secret key on multiple devices?
Yes, you can scan the same QR code or enter the secret key into multiple authenticator apps. This is useful if you have a backup phone.
What happens if I lose my phone?
Use one of the 5 scratch codes you saved. Each is single-use. After that, you'll need to recover via the terminal or recovery mode and remove the PAM line, then set up 2FA again.
Does this work for all Linux distributions?
Yes, as long as the libpam-google-authenticator package is available. It's in most major distro repositories.
Can I disable 2FA later?
Yes, remove the PAM line you added and optionally delete the .google_authenticator file in your home directory.
Is 2FA required for every login, including screen unlock?
No, this only affects the login screen. If you lock your screen with a keyboard shortcut, the unlock prompt usually only asks for your password, not the 2FA code.