
Two-factor authentication, or 2FA, confirms a user's identity via two different factors: something they know and something they have. Also known as multi-factor authentication, two-step verification, and two-step authentication, 2FA enhances security. This guide explains how to use two-factor authentication with sudo and SSH on Linux with Google Authenticator by configuring the Google Authenticator PAM module.
Before you proceed with this guide, you need a deployed Linux cloud server. We recommend the following best practices:
You will also need a two-factor authenticator app. Popular choices are Google Authenticator, Microsoft Authenticator, Authy, and FreeOTP.
Log in to your Linux server as a non-root user with sudo access. Follow the instructions for your Linux distribution to install the Google Authenticator PAM module and configure SSH Google Authenticator for enhanced two-factor authentication security.
Install with apt.
Install with yum.
Install the EPEL Repository.
Install with yum.
Note: Each user connecting to the server will perform these steps.
Run the Google Authenticator setup program. You can run the program without command-line options for an interactive setup, or use the following options:
These options explained:
~/.google_authenticatorUse
--helpfor more options.
The program will update your configuration files and display several values:
Important: Follow the instructions in your 2FA application to create a new entry with the QR code or secret key. Store your emergency codes in a secure location. If you need to reset your code, rerun the program.
These steps disable password authentication. Public/private SSH keys are required for login, and 2FA will be enabled.
Edit your SSH PAM configuration file.
Add the following line to the bottom of the file. The nullok option allows users that have not yet generated a 2FA code to log in, while codes are required if the user has followed Step 2 above. This option is useful during rollout. After all users have generated codes, you can remove the nullok option to require 2FA for everyone.
Disable user password authentication. Comment out the following line by adding # to the beginning.
Save and close the file.
Edit the SSH daemon configuration file.
Find the line for ChallengeResponseAuthentication and set its value to yes.
Verify the following options are set as shown, or add them if they don't exist.
Save and close the file.
Restart the SSH service daemon.
Log out of the server.
Log in to the server with your SSH key. The server will request a verification code. Enter the code generated by your authenticator app.
Recovery tip: If there is an error in the SSH configuration and you are unable to log in, use the Vultr web console.
Configure sudo to require 2FA codes by following these steps.
Edit /etc/pam.d/common-auth.
Add these lines to the bottom of the file. The nullok option allows users that have not yet generated a 2FA code to use sudo, while codes are required if the user has followed Step 2 above. This option is useful during rollout. After all users have generated codes, you can remove the nullok option to require 2FA for everyone. See this note for more information.
Save and exit the file.
The 2FA option takes effect immediately. If a user has configured 2FA in Step 2 above, sudo will require 2FA codes in addition to the user password. Once all users have configured 2FA, you can remove the nullok option.
Note: Users that have NOPASSWD set in the sudoers file will not receive the 2FA challenge.
If you lose access to your authenticator app, use one of your emergency backup codes. The codes are for one-time use only.
If you are locked out from SSH, you can use the Vultr web console. The configuration in this guide does not require 2FA for console access.
To disable 2FA for a specific user:
Log in as root.
Delete the .google_authenticator file in the user's home directory.
To disable 2FA for all users:
Edit /etc/ssh/sshd_config.
Locate the following line:
Remove the keyboard-interactive method:
Save and close the file, then restart the SSH daemon:
Links to download popular client apps:
Learn more about the Google Authenticator PAM module on GitHub.
0 Comments
Be the first to comment and share your perspective with the community.