SSH加固配置谷歌双因子认证+更换手机方式
注:为某一用户配置双因子认证,需切换到该用户下进行操作
1.更换yum源为阿里源
下载wget,用以更换yum源使用:
sudo yum -y install wget
创建备份源文件夹
sudo mkdir /etc/yum.repos.d/bak
备份原有yum源
sudo mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
私有云arm架构服务器:
配置centos源:
sudo wget http://mirrors.aliyun.com/repo/Centos-altarch-7.repo -O /etc/yum.repos.d/CentOS-Base.repo
配置epel源:
sudo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
清除缓存:
sudo yum clean all
生成缓存:
sudo yum makecache
公有云x86架构服务器
配置centos源:
sudo wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
配置epel源:
sudo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
清除缓存:
sudo yum clean all
生成缓存:
yum makecache
2.安装依赖包
sudo yum install -y gcc autoconf automake libtool pam-devel libpng-devel mercurial epel-release openssh
3.安装谷歌身份验证器
sudo yum -y install google-authenticator
4.手机应用商店下载Authenticator安装
5.运行google-authenticator
sudo google-authenticator
是否开启基于时间的认证:输入 y 确认
打开手机端Authenticator,选择添加账户–>个人账户–>扫描QR码,扫描该二维码
扫描完成后,回到xshell界面,连续输入四次y:
注意:
1、Your new secret key is: FP3YIORGY4QW5IK5WORKR7DSHQ
当更换手机或安装APP需要使用此密钥做验证!!!
2、Your emergency scratch codes are:
61313680
57780866
22806904
19694937
61548077
紧急验证码,用于紧急使用,每个验证码使用一次后作废!!!
6.修改SSH服务配置
注:因系统不同,部分sshd配置可能会有所不同!!!如有报错,可手动修改!!!
ChallengeResponseAuthentication选项,取消注释并改成yes
sudo sed -i '$a\ChallengeResponseAuthentication yes' /etc/ssh/sshd_config
UsePAM选项,取消注释并改成yes
sudo sed -i '$a\UsePAM yes' /etc/ssh/sshd_config
7.配置OpenSSH PAM
sudo sed -i '1a\auth required pam_google_authenticator.so' /etc/pam.d/sshd
8.重启sshd服务
sudo systemctl restart sshd
9.验证登录
等待一分钟左右,再次打开一个终端,登录该服务器
当输入密码直接登录时,提示登录失败。
打开手机端app获得一次性代码,选择下方的Keyboard Interactive(I),输入验证码
输入密码:
10.配置完成后,换回原来的yum源
sudo rm -rf /etc/yum.repos.d/*.repo && mv /etc/yum.repos.d/bak/* /etc/yum.repos.d/
生成缓存:
sudo yum makecache
11.更换手机
1、打开手机APP主页面----->右上角 + 号-----> 其他(Google、facebook等)
2、选择手动输入代码
3、输入账户名和密钥:
账户名:就是服务器的名称!!!
密钥:即上述第5步生成的new secret key!!!
4、点击完成,成功添加账户
tips:依次点击账户----->右上角小齿轮------>账户名称,即可重命名账户