Integrating Google Authenticator with SSH logins
Time Synchronization
TOTP requires that the system time be accurate, so synching the server with a Network Time Protocol (NTP) service is recommended. While not a requirement for TOTP to work, NTP will ensure system time synchronization with an external service. By default, Ubuntu Server 22.04 uses systemd-timesyncd
for system time synchronization, but it does not have an NTP server configured by default. To determine if the NTP service is active, use the following command:
$ timedatectl status
I recommend adding NIST's NTP service as the primary NTP server, and Ubuntu's own NTP service as a fallback. To do this, uncomment the #NTP=
and #FallBackNTP=ntp.ubuntu.com
lines in /etc/systemd/timesyncd.conf
, and change NTP=
to NTP=time.nist.gov
. After these configuration changes have been made, restart the timesyncd
service with:
# restart systemd-timesyncd.service
The system should now synchronize its time with the NIST NTP service.
Configuration
The OpenSSH server must be configured to use PAM. In /etc/ssh/sshd_config
, look for the configuration item UsePAM yes
. On Ubuntu Server, sshd
is configured to use PAM by default. Next, switch the KbdInteractiveAuthentication
option from No
to Yes
.
Once these configuration changes have been added to sshd_config
, you will need to add the Google Authentication module to PAM.
PAM looks for service-specific configuration files within /etc/pam.d/
by default. OpenSSH server has a file located at /etc/pam.d/sshd
. Add this configuration item to the bottom of line:
auth required pam_google_authenticator.so nullok
The temporary nullok
option, used for testing, allows users that have not generated a secret key to still authenticate with SSH using only their password. Once keys have been added to all SSH users, the nullok
option will be removed to enforce MFA.
Finally, reload the SSH server configuration with:
# systemctl reload sshd
Generating a Key
Next, you will need to generate a key for users. If you are logging in with the user account via SSH, you will see Verification code:
after entering the password or authenticating with your SSH key. Just click Enter. Because you added nullok
during PAM configuration, you will still be authenticated. Once logged in, run google-authenticator
to generate a secret key. When you run the command, you will be prompted to choose whether the token is time based; enter y
for yes.
A QR code will be displayed, which you can scan with the Google Authenticator app on your phone. A plaintext key will also be displayed, which should be treated with the security of a password. You should write down and securely store the plaintext key or save it to a password manager. This plaintext key can be used to re-add the TOTP key to Google Authenticator if the existing phone is lost, replaced, or broken. After adding the QR code or key to your Google Authenticator app, you can enter the current code displayed in the app for the next prompt in the shell console to verify it is working (you can use -1
to skip this, but it is not recommended). You will also receive several "scratch codes," which can be used in emergencies if the Google Authenticator device is not available. Next, you'll be given several prompts for security options; choose y
for yes for all of them (see Figure 1) The rate limiting options are especially important because these options will prevent an attacker from attempting a brute-force attack on a time-based password.
![](/var/linux_magazin/storage/images/issues/2023/269/multifactor-authentication-with-ssh/figure-1/819643-1-eng-US/Figure-1_large.png)
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.