ssh登录

ssh $user@$server_ip

ssh配置公钥登录

su - $user
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# 把你的本地公钥(id_rsa.pub内容)写入authorized_keys
vim ~/.ssh/authorized_keys

生成公钥

ssh-keygen -t ed25519
vim ~/.ssh/config

ssh服务器配置文件

vim /etc/ssh/sshd_config
# 禁止root账号ssh远程登录
PermitRootLogin no

# 关闭密码认证,只允许密钥
PasswordAuthentication no

# 关闭挑战密码认证(有些版本会绕过PasswordAuthentication)
ChallengeResponseAuthentication no

# 启用公钥认证(确保开启)
PubkeyAuthentication yes

# 禁用PAM密码,保留PAM其他功能
UsePAM yes

⚠️不要直接关闭UsePAM no,会导致 sudo、su 出问题。

sudo systemctl reload sshd

ssh保持会话

vim /etc/ssh/sshd_config

服务器配置

TCPKeepAlive yes
# 服务器往客户端发心跳包。单位秒
ClientAliveInterval 60
ClientAliveCountMax 10
# 校验语法
sudo sshd -t

# 重启sshd服务
sudo systemctl reload sshd

测试ssh连接

ssh -T git@github.com

FAQ

PAM 密码是什么

PAM = Pluggable Authentication Modules, 可插拔认证模块, 是 Linux 的一套认证框架,不是某一个密码,是一套认证机制。

SSH、sudo、su、登录屏幕,全都靠 PAM 来做身份校验。

参考资料