目前,二次认证(这里不做过多解释)是一种比较常见的安全方式。通过设置二次认证(谷歌或其他工具),可以有效避免账号密码泄露带来的安全问题。因为,每次登录前都需要获取一次性验证码,没有验证码是无法登录成功的。1安装PAM模块#用客户端验证时间$ntpdatepool.ntp.org#Ubuntu$sudoaptinstall-ylibpam-google-authenticator#CentOS7$yuminstall-yepel-release$yuminstall-ygoogle-authenticator2生成二次验证码#生成验证码#Which账号需要动态验证码,请切换到账号进行操作#-t:使用TOTP验证#-f:将配置保存到~/.google_authenticator文件#-d:不允许重复使用之前使用过的token#-w3:使用时钟偏移令牌进行身份验证#-e10:生成10个紧急备份代码#-r3-R30:速率限制-每30秒允许3次登录https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/vagrant@vagrant%3Fsecret%3DKZ7QPA11115XTQJQGBFWAIUJBY%26issuer%3DvagrantYournewsecretkeyis:KZ7xxx7EI5123xxx123Yourverificationcodeis90xx71Youremergencyscratchcodesare:1571xx039968xx562319xx898321xx979730xx153424xx235667xx039408xx867502xx414677xx143配置SSH服务启用两步验证#启用两步验证$sudovim/etc/pam.d/sshd#@includecommon-auth#将禁用密码身份验证authrequiredpam_google_authenticator.so#禁用密码验证#修改SSH配置文件$sudovim/etc/ssh/sshd_configPort1090ChallengeResponseAauthenticationyesPubkeyAuthenticationyesPasswordAuthenticationnoAuthenticationMethodspublickey,keyboard-interactive#重启SSH服务$sudosystemctlrestartssh.service4配置sudo二次验证#保存退出$sudovim/etc/pam.d/common-authauthrequiredpam_google_authenticator.so#重启SSH服务$sudosystemctlrestartsGooglerestarts5手机安装扫描1.使用本工具上一步生成的二维码,获取动态验证码。2、之后就可以使用手机进行二次认证了,就可以登录服务器了。软件$sudoaptinstall-yfail2ban#配置文件$vim/etc/fail2ban/jail.local[DEFAULT]ignoreip=127.0.0.1/8bantime=86400findtime=600maxretry=5banaction=firewallcmd-ipsetaction=%(action_mwl)s[sshd]enabled=truefilter=sshdport=1090action=%(action_mwl)slogpath=/var/log/secure#重启服务$systemctlrestartfail2ban7从2FA锁定中恢复#为特定用户禁用2FA(无法访问身份验证器应用程序)$sudovim/etc/ssh/sshd_configAuthenticationMethodspublickey,keyboard-interactiveAuthenticationMethodspublickey#重启SSH服务$sudosystemctlrestartssh.service
