Two Factor Authentication With OpenSSH: Difference between revisions

From Alpine Linux
(first draft)
 
(tested setup)
Line 1: Line 1:
{{Draft}}
{{Note|Currently the packages required to follow this how-to are available only in edge (future AL3.7). You can [[Alpine_Linux_package_management#Repository_pinning|pin edge repository]] if you're on a stable version.}}
 
{{Note|Currently the packages required to follow this how-to are available only on edge (future AL3.7)}}


== Using Google Authenticator ==
== Using Google Authenticator ==
{{cmd|apk add google-authenticator openssh-server-pam}}
{{cmd|apk add google-authenticator openssh-server-pam}}
{{cmd|cat /etc/pam.d/base-auth}}
<pre>
# basic PAM configuration for Alpine.
auth required pam_env.so
#auth required pam_unix.so nullok_secure
auth required pam_nologin.so successok
auth required /lib/security/pam_google_authenticator.so
</pre>


{{cmd|cat /etc/ssh/sshd_config}}
{{cmd|cat /etc/ssh/sshd_config}}
<pre>
<pre>
UsePAM yes
AuthenticationMethods publickey,keyboard-interactive
AuthenticationMethods publickey,keyboard-interactive
ChallengeResponseAuthentication yes
ChallengeResponseAuthentication yes
PermitRootLogin yes
PermitRootLogin yes
UsePAM yes
</pre>
</pre>
{{Note|This configuration does NOT allow password authentication. To allow password authentication append "password" to "AuthenticatioMethods"}}
{{Note|This configuration does NOT allow password authentication globally}}


{{cmd|cat /etc/pam.d/sshd #create the file if needed}}
<pre>
account include base-account
auth required pam_env.so
auth required pam_nologin.so successok
auth include google-authenticator
</pre>
== Time-based One Time Password authentication (TOTP RFC 6238) ==
As user root:
As user root:
{{cmd|google-authenticator}}
{{cmd|google-authenticator}}
{{Note|Please take note of <secret>}}
<pre>
<pre>
Do you want authentication tokens to be time-based (y/n) y
Do you want authentication tokens to be time-based (y/n) y
Line 52: Line 50:
size of +-1min (window size of 3) to about +-4min (window size of
size of +-1min (window size of 3) to about +-4min (window size of
17 acceptable tokens).
17 acceptable tokens).
Do you want to do so? (y/n) y
Do you want to do so? (y/n) n


If the computer that you are logging into isn't hardened against brute-force
If the computer that you are logging into isn't hardened against brute-force
Line 60: Line 58:
</pre>
</pre>


Download '''Google Authenticator''' app and enter manually your <secret> key
{{Tip|You might want to answer differently at questions 2, 3 and 4 based on your paranoia's level and firewall settings :)}}
 
Re-run <code>google-authenticator</code> for each user that needs to login via SSH. Don't forget to include <code>.google_authenticator</code> files in your [[Alpine_local_backup|LBU]] if you're running from RAM.
 
== Prover ==
Download '''Google Authenticator''' app from your ''App Store''. Startup '''Google Authenticator''' app and enter manually your <secret> key.
 
== Login ==
{{cmd|ssh -v root@yourbox}}
You should see the last lines saying:
<pre>
Authenticated with partial success.
debug1: Authentications that can continue: keyboard-interactive
debug1: Next authentication method: keyboard-interactive
Verification code:
</pre>
<code>Authenticated with partial success</code> means that pubkey authentication was successfull and now the verifier is asking for the verification code generated from the '''Google Authenticator''' app.

Revision as of 14:12, 12 June 2017

Note: Currently the packages required to follow this how-to are available only in edge (future AL3.7). You can pin edge repository if you're on a stable version.

Using Google Authenticator

apk add google-authenticator openssh-server-pam

cat /etc/ssh/sshd_config

AuthenticationMethods publickey,keyboard-interactive
ChallengeResponseAuthentication yes
PermitRootLogin yes
UsePAM yes
Note: This configuration does NOT allow password authentication globally

cat /etc/pam.d/sshd #create the file if needed

account		include				base-account

auth		required			pam_env.so
auth		required			pam_nologin.so	successok
auth		include				google-authenticator

Time-based One Time Password authentication (TOTP RFC 6238)

As user root:

google-authenticator

Note: Please take note of <secret>
Do you want authentication tokens to be time-based (y/n) y
https://www.google.com/<pruned>
Your new secret key is: <secret>
Your verification code is <pruned>
Your emergency scratch codes are:
  <pruned>
  <pruned>
  <pruned>
  <pruned>
  <pruned>

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, tokens are good for 30 seconds. 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. If you experience problems with
poor time synchronization, you can increase the window from its default
size of +-1min (window size of 3) to about +-4min (window size of
17 acceptable tokens).
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
Tip: You might want to answer differently at questions 2, 3 and 4 based on your paranoia's level and firewall settings :)

Re-run google-authenticator for each user that needs to login via SSH. Don't forget to include .google_authenticator files in your LBU if you're running from RAM.

Prover

Download Google Authenticator app from your App Store. Startup Google Authenticator app and enter manually your <secret> key.

Login

ssh -v root@yourbox

You should see the last lines saying:

Authenticated with partial success.
debug1: Authentications that can continue: keyboard-interactive
debug1: Next authentication method: keyboard-interactive
Verification code: 

Authenticated with partial success means that pubkey authentication was successfull and now the verifier is asking for the verification code generated from the Google Authenticator app.