SSH with TOTP RHEL/Centos 7

Let’s see how to enable SSH authentication using an OATH-TOTP application in addition to an SSH key. Logging into your server via SSH will then require two factors across two channels, thereby making it more secure than a password or SSH key alone.

NOTE: This configuration part of the fact that you have already configured some user through key exchange (root in this example). So please configure at least one before begin.

First of all we need to enable the epel repositories

root@lab1234:~ $ yum install

Then we have to install the google’s pam.

root@lab1234:~ $ yum install google-authenticator

Run the initialization application. Here we will have to answer some questions. Read and answer them like you prefer, are questions related with the hardening of the solution.

root@lab1234:~ $ google-authenticator

Do you want authentication tokens to be time-based (y/n) y

--- code in screen and link for the browser ---
--- your secret keys for emergency ---

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) n

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
root@lab1234:~ $ 

The next step is create this new service with the qrcode on your TOTP application: google authenticator or others. My personal preference is Latch ( but you can use the one which you trust in.

Now we have to configure pam and ssh.

/etc/pam.d/sshd – We have to add in this file the next line at the bottom of the file. This allows users without a OATH-TOTP token to still log in using their SSH key.

auth required nullok

When all your users will have working their token, you must remove the “nullok” clause.

/etc/ssh/sshd_config – Configure SSH to support this type of authentication.

ChallengeResponseAuthentication yes

And restart ssh server

root@lab1234:~ $ systemctl restart sshd.service

Now, in our next logon we will be asked about the password and the Verification code

[user@mylaptop ~]$ ssh root@lab1234
Password: **************
Verification code: ******

root@lab1234:~ $ 

This next steps are not required but are recommended for making ssh aware of MFA

/etc/ssh/sshd_config – Add this line at the bottom of the file. This line tells SSH we need a SSH key and either a password or a verification code (or the three).

AuthenticationMethods publickey,password publickey,keyboard-interactive

/etc/pam.d/sshd – Comment the next line for avoid pam to prompt for a password if you use ssh keys

auth substack password-auth

Restart ssh server again

root@lab1234:~ $ systemctl restart sshd.service

And done! I recommend also trace one or two logons in order to understand how all works.