OTP (One-time password) with soft token is one of the most widely-used 2FA (Two-factor authentication) or MFA (Multi-factor authentication).
OTP for SSH server is provided by libpam-google-authenticator and both the SSH server and PAM (Pluggable Authentication Modules) need to be configured on the server to OTP 2FA for SSH.
Google Authenticator, Authy or any other soft token application that generates time-dependent codes could be used to authenticate on the client side.
$ sudo apt update && sudo apt install --assume-yes libpam-google-authenticator #Ubuntu and Debian variance
$ sudo vi /etc/ssh/sshd_config
Remove leading # if exist to uncomment the line.
Match User username AuthenticationMethods keyboard-interactive
This is optional as keyboard-interactive is by default enabled for all users.
Set other authentication method for the specific user to exclude them from using OTP 2FA. Possible authentication methods:
$ sudo systemctl restart ssh
$ sudo vi /etc/pam.d/sshd
auth required pam_unix.so no_warn try_first_pass auth required pam_google_authenticator.so
This will require the use of password first to log in, followed by the Google Authenticator module.
This step need to be repeated for all other users.
Do you want authentication tokens to be time-based (y/n) y
Enter code from app (-1 to skip): 466822 Code confirmed Your emergency scratch codes are: 21277551 15985104 82336172 67376642 85166830
Do you want me to update your "/home/user/.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
Set this to n to allow multiple use of the same code.
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
Comment anonymously. Login not required.