OTP (One-time password) with soft token is one of the most widely-used
2FA (Two-factor authentication) or
MFA (Multi-factor authentication).
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
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
SSHdconfiguration file using your preferred text editor.
$ sudo vi /etc/ssh/sshd_config
# 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
2FA. Possible authentication methods:
$ sudo systemctl restart ssh
PAMusing your preferred text editor.
$ 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.
google-authenticatorcommand to start configuring and generating the
QRcode for the user.
This step need to be repeated for all other users.
[ENTER]to use time-based authentication token.
Do you want authentication tokens to be time-based (y/n) y
Authyor any other similar app.
Enter code from app (-1 to skip): 466822 Code confirmed Your emergency scratch codes are: 21277551 15985104 82336172 67376642 85166830
[ENTER]to confirm generation of configuration file for the google authenticator module.
Do you want me to update your "/home/user/.google_authenticator" file? (y/n) y
[ENTER]to force only a single use of generated time-based token.
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.
[ENTER]to agree to code generation and usage policies.
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
[ENTER]to enable rate-limiting option.
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
Support us on Patreon if this guide has helped you. Thanks!!!
Comment anonymously. Login not required.