Enable Two-Factor Authentication For SSH in Linux
Enable Two-Factor Authentication For SSH in Linux
Enable Two-Factor Authentication For SSH in Linux
In this article, you will learn how to enable two-factor authentication for SSH in Linux.
Secure Shell (or SSH) is primarily used by admins to securely access and manage remote
computers, particularly those running Linux. It supports two very common forms of
authentication: password authentication and public key authentication. It is always
recommended to use public key authentication for SSH access since it is more secure.
However, if you still depend on password authentication, it is highly recommended to
enable two-factor authentication.
Surender Kumar
Surender Kumar has more than twelve years of experience in server and network administration. His
fields of interest are Windows Servers, Active Directory, PowerShell, web servers, networking, Linux,
virtualization, and penetration testing. He loves writing for his blog.
Contents
I will use Ubuntu Linux and the Google Authenticator app on a smartphone to generate a
time-based one-time password (TOTP). You can use the same idea to enable 2FA on
any other Linux distro and use any other authenticator app instead of Google.
Once you successfully confirm that the 2FA setup is working as expected,
delete this user.
Time synchronization ^
Before enabling 2FA, it is really important that the clocks on your Linux system
and mobile device are synchronized, which means that both should have the
same time in their clocks. However, a nominal time difference of a few seconds
(e.g., 30 seconds or less) is usually acceptable.
The best way to keep the time synchronized is using the network time protocol
(NTP). By default, Ubuntu uses timedatectl/timesyncd for time synchronization,
Enable two-factor authentication for SSH in Linux
If you see System clock synchronized: no and NTP service: inactive, as shown
in the screenshot, run the following command:
This command enables the NTP service and synchronizes the system's clock.
Now repeat the timedatectl command once again and confirm that the output is
as shown in the screenshot.
You will notice that the system clock is now properly synchronized. Similarly,
you can also set your mobile device to use a network-provided time instead of
manually setting it.
• Scan a QR code
• Enter a setup key
google-authenticator -s ~/.ssh/google_authenticator
Add an account for the Linux system in the Google Authenticator app using a QR code
This command will run the Google Authenticator PAM module. The -s option
allowed us to save the secret key in a nonstandard location, and we specified
the ~/.ssh directory where SSH keys are kept. When you run this command,
you will see a QR code on screen if QR code libraries are supported. Now open
the Google Authenticator app in your mobile, tap the Scan a QR code option,
and scan the QR code displayed on the Linux server console.
If your Linux server doesn't support QR codes or your mobile device can't scan
the QR code, you could use the secret key option. To do so, tap the Enter a
setup key option in the Google Authenticator app, and use the secret key
displayed on the Linux console to add the account in the Authenticator app.
Add an account for the Linux system in the Google Authenticator app using a setup key
Once your account is added to the Authenticator app, type the TOTP from the
Authenticator app in the Linux console and press Enter. When the code is
confirmed, you will see the emergency scratch codes (or backup codes)
displayed on the Linux console. Store these backup codes in a safe place, as
you might need them to get your SSH access back if something goes wrong
with your phone in the future. Furthermore, you will be prompted by a series of
questions during the setup. Make sure you answer these questions, as shown
in the screenshot:
KbdInteractiveAuthentication yes
• If you're running Ubuntu 21.10 or earlier, add the following line instead:
ChallengeResponseAuthentication yes
In either case, if the suggested line already exists in the sshdconfig file, make
sure you uncomment it and change it to "yes". By doing so, you're essentially
enabling keyboard-interactive (or challenge response) authentication for the
SSH daemon. See the following screenshots for reference:
Make sure you don't close the current Linux console session until you confirm
the 2FA setup is properly working. If you close this session and 2FA doesn't
work, you might get yourself locked out. If this happens, you need to log in with
the temporary admin user we created earlier.
You need to use the verification code generated in the Google Authenticator
app on your mobile device for successful authentication. If you enter the wrong
verification code, it will prompt you for a password again. Now, if you take a
look at the SSH access logs (shown in the screenshot below), you will see a
log indicating invalid verification code. This is because I intentionally typed the
wrong verification code the first time. When the correct verification code is
entered, authentication is successful, and the SSH session is opened.
This is it. The SSH access on your Ubuntu Linux system is now protected with
2FA.
PasswordAuthentication no
PubkeyAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
Enabling 2FA for SSH access using public key authentication in Ubuntu Linux
Troubleshooting ^
If you can't access the Linux system using SSH, make sure you followed all the
steps correctly, and the clocks on both the Linux server and the mobile device
are properly synced. If the clock on your Linux system is properly synced using
NTP, but you still see an invalid verification code in the SSH logs, the problem
is most likely with the time synchronization on your mobile device. Follow these
steps to fix it:
Conclusion ^
That was it for this guide. You just learned how to enable two-factor authentication for SSH login in Linux.
When 2FA starts working properly, don't forget to delete the temporary admin user that we created at
the beginning, and make sure you disable root login via SSH to further improve the security.