SSH 相关
1. SSH 秘钥访问
1.1 生成本地秘钥公钥
1.2 将本地 公钥发送到 目标主机
cat ~/.ssh/id_rsa.pub
目标主机
ehco 'id_rsa.pub value' >> authorized_keys
2. ubuntu 关闭 密码ssh 登陆
3. SSH 二次认证
3.1 安装 PAM 模块
tab: Ubuntu
``` shell
# 时间与客户端进行校验
ntpdate pool.ntp.org
sudo apt install -y libpam-google-authenticator
```
tab: CentOS7
``` shell
# 时间与客户端进行校验
ntpdate pool.ntp.org
yum install -y epel-release
yum install -y google-authenticator
```
3.2 生成二次验证代码
# 生成验证码
# 哪个账号需要动态验证码,请切换到该账号下操作
# -t: 使用 TOTP 验证
# -f: 将配置保存到 ~/.google_authenticator 文件里面
# -d: 不允许重复使用以前使用的令牌
# -w 3: 使用令牌进行身份验证以进行时钟偏移
# -e 5: 生成 5 个紧急备用代码
# -r 3 -R 30: 限速 - 每 30 秒允许 3 次登录
google-authenticator -t -f -d -w 3 -e 5 -r 3 -R 30
Warning: pasting the following URL into your browser exposes the OTP secret to Google:
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/vagrant@vagrant%3Fsecret%3DKZ7QPA11115XTQJQGBFWAIUJBY%26issuer%3Dvagrant
Your new secret key is: KZ7xxx7EI5123xxx123
Your verification code is 90xx71
Your emergency scratch codes are:
1571xx03
9968xx56
2319xx89
8321xx97
9730xx15
配置 SSH 服务启动两步验证
# 启用两步验证
$ sudo nano /etc/pam.d/sshd
# @include common-auth # 将禁用密码身份验证
auth required pam_google_authenticator.so # 禁用密码验证
# 修改SSH配置文件
$ sudo nano /etc/ssh/sshd_config
Port 22
ChallengeResponseAuthentication yes
PubkeyAuthentication yes
PasswordAuthentication no # 禁用密码登陆
AuthenticationMethods publickey,keyboard-interactive
# 重启SSH服务
$ sudo systemctl restart ssh.service
按条件区分登陆认证类型
在sshd_config中,Match指令可以根据特定的条件匹配,应用不同的配置。Match指令支持以下类型的条件:
- User:匹配指定的用户名。
- Group:匹配指定的用户组。
- Host:匹配指定的远程主机名。
- Address:匹配指定的客户端IP地址。
- LocalAddress:匹配指定的本地服务器IP地址。
- LocalPort:匹配指定的本地服务器端口号.
这些条件可以单独使用,也可以组合使用。以下是每种类型的详细解释和示例:
Match Address 192.168.0.0/24
AuthenticationMethods publickey
Match all
AuthenticationMethods publickey,keyboard-interactive
验证ssh 配置是否正常
sudo /usr/sbin/sshd -T