|
| 1 | +# Two-Factor Authentication (2FA) for Cockpit |
| 2 | + |
| 3 | +Adding Two-Factor Authentication (2FA) to Cockpit increases the security of your server by requiring a time-based one-time password (TOTP) in addition to your regular credentials. |
| 4 | + |
| 5 | +## Step 1: Install Required Packages |
| 6 | + |
| 7 | +Open a terminal and run: |
| 8 | + |
| 9 | +```sh |
| 10 | +sudo apt-get install libpam-google-authenticator -y |
| 11 | +``` |
| 12 | + |
| 13 | +This installs the PAM module for Google Authenticator. |
| 14 | + |
| 15 | +## Step 2: Configure Google Authenticator for Your User |
| 16 | + |
| 17 | +Run the following command to set up Google Authenticator with recommended options: |
| 18 | + |
| 19 | +```sh |
| 20 | +google-authenticator -t -d -f -r 3 -R 30 -W -Q UTF8 |
| 21 | +``` |
| 22 | + |
| 23 | +!!! note |
| 24 | + |
| 25 | + - `-t` use TOTP instead of HOTP (recommended). |
| 26 | + - `-d` disable reuse of previously used TOTP tokens. |
| 27 | + - `-f` disable confirmation before writing the `~/.google_authenticator` file. |
| 28 | + - `-r 3 -R 30` limits the number of login attempts to 3 every 30 seconds. |
| 29 | + - `-W` by default google-authenticator allows the use of codes that were generated a little before or a little after the current time. This option disables that feature (recommended for security). |
| 30 | + - `-Q UTF8` specifies the encoding for the QR code. Change to `-Q ANSI` if you're having issues with viewing the QR code. |
| 31 | + |
| 32 | +- This will generate a secret key, QR code, and emergency scratch codes. |
| 33 | +- Scan the QR code with your preferred authenticator app (e.g., Google Authenticator, Authy). |
| 34 | +- Enter the verification code from your authenticator app to complete the setup. |
| 35 | +- Save the emergency scratch codes in a safe place. |
| 36 | + |
| 37 | +!!! note |
| 38 | + |
| 39 | + Scratch codes are one-time use only. If you lose access to your authenticator app, enter one of these codes to log in and recreate your 2FA setup. |
| 40 | + |
| 41 | +## Step 3: Enable 2FA for Cockpit |
| 42 | + |
| 43 | +Use the following command to add the Google Authenticator PAM module to the Cockpit PAM configuration: |
| 44 | + |
| 45 | +```sh |
| 46 | +sudo bash -c 'echo "auth required pam_google_authenticator.so nullok" >> /etc/pam.d/cockpit' |
| 47 | +``` |
| 48 | + |
| 49 | +This tells Cockpit to require a TOTP code during login. |
| 50 | + |
| 51 | +!!! note |
| 52 | + |
| 53 | + - The `nullok` option disables 2FA for users that do not have a `~/.google_authenticator` file. |
| 54 | + |
| 55 | +## Step 4: Restart Cockpit |
| 56 | + |
| 57 | +Restart the Cockpit service to apply the changes: |
| 58 | + |
| 59 | +```sh |
| 60 | +sudo systemctl restart cockpit |
| 61 | +``` |
| 62 | + |
| 63 | +## Step 5: Test Your Setup |
| 64 | + |
| 65 | +1. Log out of Cockpit. |
| 66 | +2. Log back in. You should be prompted for a verification code from your authenticator app. |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +## Uninstalling 2FA |
| 72 | + |
| 73 | +To remove 2FA from Cockpit, simply delete the line you added to the PAM configuration: |
| 74 | + |
| 75 | +```sh |
| 76 | +sudo bash -c 'sed -i "/pam_google_authenticator.so nullok/d" /etc/pam.d/cockpit' |
| 77 | +``` |
| 78 | + |
| 79 | +Then restart the Cockpit service: |
| 80 | + |
| 81 | +```sh |
| 82 | +sudo systemctl restart cockpit |
| 83 | +``` |
| 84 | + |
| 85 | +You can also remove the generated `~/.google_authenticator` file and the installed packages if you no longer need 2FA: |
| 86 | + |
| 87 | +```sh |
| 88 | +sudo apt remove libpam-google-authenticator -y |
| 89 | +rm ~/.google_authenticator |
| 90 | +``` |
0 commit comments