HOWTO OpenSSH 2FA with password and Google Authenticator
The following will allow you to setup the OpenSSH ssh server to use two factor authentication consisting of the user's password and a Time-based One Time Password (TOTP). In order to facilitate this, you will need to add the required APKs, configure the OpenSSH server, configure the google-authenticator PAM module, restart the OpenSSH server, and setup google-authenticator per user.
Required APKs
Main repository:
Community repository:
Documentation (optional):
- openssh-doc
- google-authenticator-doc (community repo)
To pull in all apks in one shot:
# apk add openssh openssh-server-pam google-authenticator openssh-doc google-authenticator-doc
Configure the OpenSSH server
Use sudo, su, or doas to elevate your preferred text editor to root privileges, and then edit the /etc/ssh/sshd_config file. There are three directives which need to be altered or added if not present:
PasswordAuthentication no AuthenticationMethods keyboard-interactive UsePAM yes
Please consult the sshd_config manpage, particularly the AuthenticationMethods directive, if you would like to use public key authentication.
Configure the google-authenticator PAM module
As root create the /etc/pam.d/sshd file and create a link to it called /etc/pam.d/sshd.pam:
# touch /etc/pam.d/sshd
# ln /etc/pam.d/sshd /etc/pam.d/sshd.pam
Elevate your preferred text editor to root and edit the /etc/pam.d/sshd file, it will contain the following lines at a minimum:
account include base-account auth required pam_env.so auth required pam_nologin.so successok auth required /lib/security/pam_google_authenticator.so echo_verification_code grace_period=57600 nullok auth required pam_unix.so md5 sha512
In our example above, we are passing a few useful options to the google authenticator pam module:
echo_verification_code | Allows the user to see their verification code as they type it, reducing the chance that it is typed into the prompt incorrectly. |
grace_period=57600 | Provides a grace period in seconds during which if a user attempts to login again from the same IP address, they will not be asked to retype the verification code. |
nullok | This option is recommended during the initial roll-out process of google authenticator on your server, it allows users who have not created a secret key to bypass the verification code prompt... It is recommended that you remove this option after all of your users have created secret keys. |
More options can be found in the google-authenticator manpage or on their github.
Restart the openSSH server
In order for our changes above to take effect, elevate to root and run:
# service sshd restart
This is very useful because you can restart sshd and open an additional connection to test config changes, if you typo something, you can use your pre-existing connection to fix typos or revert changes to get sshd running again.
Before proceeding further, take the time to ensure that your sshd is allowing you to login with your username/password without TOTP as usual and fix any issues.Setup google-authenticator per user
Instruct all users whom you wish to use the one time passes to login to their account and run the google-authenticator command to setup their secrets...
They will type the OTP secret generated into their authenticator app and hand write their 5 scratch codes to store in a secure location.
The command output will look something like this:
$ google-authenticator
Do you want authentication tokens to be time-based (y/n) y Warning: pasting the following URL into your browser exposes the OTP secret to Google: A URL APPEARS HERE A URL APPEARS HERE Failed to use libqrencode to show QR code visually for scanning. Consider typing the OTP secret into your app manually. Your new secret key is: Ender code from app (-1 to skip): Your emergency scratch codes are: CODE1 CODE2 CODE3 CODE4 CODE5 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, bit 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) n 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) n ~$
You will want to answer y to the first two questions.
The remaining questions are personal preference, I typically answer y n n, my rationale for doing so:
I disallow multiple uses of authentication tokens on boxes in which the grace_period option above is set above 60 seconds.
I cannot think of any machines I deal with which are not NTP (clock sync) enabled, so I do not enable time-skew compensation.
I do not enable rate-limiting as the sshd and firewall should take care of that.