Step 1: Install and Configure Google Authenticator
sudo apt install libpam-google-authenticator -y
Step 2: Generate Security Code
google-authenticator
After running the above command, the system will display a link and QR code as shown below:

Step 3: Generate Security Code
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 4: Configure OpenSSH to use 2FA
First, backup the file with the command:
sudo cp /etc/pam.d/sshd /etc/pam.d/sshd.bak
Next, 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
sudo nano /etc/ssh/sshd_config

Restart Service
sudo systemctl restart sshd.service