harbor run -t LiteCoder/ssh-mfa-google-authenticatorharbor download LiteCoder/ssh-mfa-google-authenticatorSet up multi-factor authentication (MFA) for SSH access on an Ubuntu server using Google Authenticator, requiring both public key and TOTP verification for all users except a designated emergency account.
libpam-google-authenticatorInstall Google Authenticator PAM Module
libpam-google-authenticator package.Configure PAM for SSH
/etc/pam.d/sshd to include the Google Authenticator PAM module.pam_google_authenticator.so with the nullok option (to allow users who haven't yet set up TOTP to still log in).pam_google_authenticator.so line must NOT be commented out.Configure SSHD
/etc/ssh/sshd_config (or create a drop-in file at /etc/ssh/sshd_config.d/mfa.conf) so that the following directives are active (not commented out):
ChallengeResponseAuthentication yesUsePAM yesPubkeyAuthentication yesAuthenticationMethods publickey,keyboard-interactiveMatch User block for the emergency user emergency_admin that sets:
AuthenticationMethods publickey
This allows emergency_admin to authenticate with only a public key, bypassing MFA.Create Emergency User
emergency_admin./home/emergency_admin/.ssh/ with permissions 700, owned by emergency_admin./home/emergency_admin/.ssh/authorized_keys with permissions 600, owned by emergency_admin.Create Test User and Generate TOTP Secret
mfa_testuser.google-authenticator (non-interactively) for mfa_testuser to generate a TOTP secret. The generated config must be stored at /home/mfa_testuser/.google_authenticator.mfa_testuser.400 or 600.Configuration Summary Output
/app/output.json with the following structure:{
"pam_module_installed": true,
"pam_sshd_config": "/etc/pam.d/sshd",
"sshd_config_file": "<path to the sshd config file you edited, e.g. /etc/ssh/sshd_config or /etc/ssh/sshd_config.d/mfa.conf>",
"challenge_response_auth": "yes",
"use_pam": "yes",
"pubkey_auth": "yes",
"authentication_methods": "publickey,keyboard-interactive",
"emergency_user": "emergency_admin",
"emergency_user_auth_method": "publickey",
"test_user": "mfa_testuser",
"totp_secret_file": "/home/mfa_testuser/.google_authenticator"
}
All JSON keys must be present. Boolean values must be JSON booleans. String values must be JSON strings.