Configure SSH Two Factor Authentication on Ubuntu 22.04 with 3 extremely simple steps..
Table of Contents
Introduce
SSH Two Factor Authentication aka 2-Step Authentication for SSH service, with this feature, users can increase the security of their server from the risk of attacks via SSH protocol. And in this post AZDIGI will guide you how to install Google Authenticator and configuration Two Factor Authentication when SSHing into a system using Ubuntu 22.04.
Installation Instructions
Step 1: Install and Configure Google Authenticator
- To install Google Authenticator, go to your Ubuntu 22.04 server, and run the command:
sudo apt install libpam-google-authenticator -y

To generate a security key, type the command
After typing the above command, the result will appear a link and QR code as shown below.

If the image is too big, you can copy the link and paste it in your browser to display the QR code easier to see as below.

Now, take your phone or device with the App . pre-installed Google Authenticator and scan the above QR code. After scanning, you will receive a 6-digit code similar to the picture, and these 6 numbers will be continuously changed every 30 seconds. And the number is only valid when it shows green as shown, otherwise if it is red, it will have no effect.

After you have the above 6 numbers, you proceed with those 6 numbers to the server setup to complete the installation.

After successfully entering the code, you will receive an urgent code and need to verify some of the information below.
Code confirmed
Your emergency scratch codes are:
32788307
11480031
78992160
60171886
58985147
Do you want me to update your "/root/.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) y
Step 2: Configure OpenSSH to use 2FA
- Edit the configuration file at /etc/pam.d/sshd
First, backup the file with the command
sudo cp /etc/pam.d/sshd /etc/pam.d/sshd.bak
Then you edit the file with the command
sudo nano /etc/pam.d/sshd
Add the content below at the end of the file and save it.
auth required pam_google_authenticator.so nullok
auth required pam_permit.so

- Edit the configuration file at /etc/ssh/sshd_config
First, backup the file with the command
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
Then you edit the file with the command
sudo nano /etc/ssh/sshd_config
Then you find the value ChallengeResponseAuthentication and set it to yesif you don’t have it you can add it manually.

Restart the service
sudo systemctl restart sshd.service
Step 3: SSH into the test server
After the configuration completes the steps, now you proceed to SSH into your Ubuntu 22.04 server to see the results. And as shown below, you can see that after I enter the Passwd root of the VPS, the system will ask to enter the 2-factor authentication code. Now you just need to check the App Google Authenticator to get the Code and enter it to be able to SSH successfully.

You can refer to other user guides at the link below:
If you need assistance, you can contact support in the ways below: