Проксі нового покоління: AI-керовані, блокчейн-мережі та IPv6 — тренди 2025 року.

In this article, we will show you how to enable two-factor authentication (2FA) for SSH login on Linux using Google PAM (Pluggable Authentication Module) and Microsoft Authenticator mobile app. 2FA allows you to add an extra security layer when you authenticate on a Linux host over SSH. After enabling 2FA, you will have to enter a one-time digital password (TOTP) from your smartphone in addition to your username and password (or RSA key) to SSH into Linux.
Install the Microsoft Authenticator mobile app on your smartphone (it is available both in Google Store and App Store).
Now you need to install and configure the Google PAM on your Linux host:
sudo apt-get install libpam-google-authenticator
yum install google-authenticator
google-authenticator
Y -> Enter
Y -> Enter
Y -> Enter
Y -> Enter
$ google-authenticator -t -f -d -w 3 -e 5 -r 3 -R 30
auth required pam_google_authenticator.so nullok
sudo mcedit /etc/ssh/sshd_config
ChallengeResponseAuthentication
value to Yes
:ChallengeResponseAuthentication yes
service ssh restart
Y -> Enter
Then try to connect to your Linux host over SSH. You will be asked to enter a verification code before entering a password.
Open the Authenticator app on your smartphone and find the user of your Linux host. Enter a 6-character one-time password code that the Authenticator generated for you into the console.
Note that the one-time password code is valid for a limited time (30 seconds by default). If you have entered the correct code, you will be prompted to enter your Linux user password.
If the logon is successful, the following line appears in the authentication log:
cat /var/log/auth.log
Jul 20 11:12:22 srvubun01 sshd(pam_google_authenticator)[6242]: Accepted google_authenticator for sysops
If the incorrect code is entered, the log shows errors:
Jul 20 11:14:20 srvubun01 sshd(pam_google_authenticator)[6436]: Invalid verification code for sysops Jul 20 11:14:22 srvubun01 sshd[6436]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.10.15 user=sysops
If you want to use 2FA together with the SSH key authentication, add the following directives to /etc/ssh/sshd_config:
PasswordAuthentication no PubkeyAuthentication yes AuthenticationMethods publickey,keyboard-interactive
Комментарии