Secure SSH with Google Authenticator Two-Factor Authentication on CentOS 7
First of all we will install the open source Google Authenticator PAM module by executing the following command on the shell.
# yum install google-authenticator
# google-authenticator
The next step is to change some files which we will start by first changing /etc/pam.d/sshd. Add the following line to the top:
auth required pam_google_authenticator.so
auth required pam_google_authenticator.so
auth required pam_sepermit.so
auth include password-auth
account required pam_nologin.so
account include password-auth
password include password-auth
# pam_selinux.so close should be the first session rule
session required pam_selinux.so close
session required pam_loginuid.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session required pam_selinux.so open env_params
session required pam_namespace.so
session optional pam_keyinit.so force revoke
session include password-auth
Additional you could also add folowing line. This makes it possible to logon localy without verify code:
auth [success=1 default=ignore] pam_access.so accessfile=/etc/security/access-local.conf
Contect of /etc/security/access-local.conf:
# Google Authenticator can be skipped on local network + : ALL : 192.168.1.0/24 + : ALL : LOCAL - : ALL : ALL
Change the next file which is /etc/ssh/sshd_config. Add the following line in the file and if its already placed then change the parameter to “yes”:
ChallengeResponseAuthentication yes
Now restart the service of ssh by the following command:
# service sshd restart