Purpose of this document is to enable two-factor authentication (2FA) on Ubuntu Desktop Login using Time-based One-Time Password (TOTP) verification with Google Authenticator.
Why do we need 2FA?
To enhance security by requiring an additional layer of authentication beyond just a password. This helps prevent unauthorized access even if a password is compromised.
How to enable 2FA?
1. Install PAM Google Authenticator.
1
2
| sudo apt update
sudo apt-get install libpam-google-authenticator
|
2. Generate a secret key using Google Authenticator.
- Make sure you have any TOTP authenticator app installed on your mobile device.
- Generate TOTP secret key
- For your reference I am giving this steps-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| Do you want authentication tokens to be time-based (y/n) y
<IT WILL SHOW QR CODE>
<SCAN TO AUTHENTICATOR APP>
<TYPE VERIFICATION CODE>
Do you want me to update your "/home/<USER_NAME>/.google_authenticator" file? (y/n) y
Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y
By default, a new token is generated every 30 seconds by the mobile app.
In order to compensate for possible time-skew between the client and the server,
we allow an extra token before and after the current time. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
Do you want to do so? (y/n) y
If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting? (y/n) n
|
- Edit
/etc/pam.d/gdm-password and add the following line after @include common-auth:
1
2
| @include common-auth
auth required pam_google_authenticator.so
|
- Don’t forget to backup the secret key generated by Google Authenticator.
Bonus Tips
If you have different windows manager or login manager, you may need to edit different PAM configuration files.
For example:
- For GDM, edit
/etc/pam.d/gdm-password
- For LightDM, edit
/etc/pam.d/lightdm
- For SDDM, edit
/etc/pam.d/sddm
- For LXDM, edit
/etc/pam.d/lxdm
- For KDM, edit
/etc/pam.d/kdm
- For Console login, edit
/etc/pam.d/login