SSH(Secure Shell)无密码登录基于非对称加密技术,通过公钥/私钥对实现安全认证:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# -t 指定密钥类型(推荐rsa/ed25519)
# -b 指定密钥长度
# -C 添加注释标识
ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_host
# 或手动复制:
cat ~/.ssh/id_rsa.pub | ssh user@host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_rsa # 私钥文件权限
编辑服务器/etc/ssh/sshd_config:
PasswordAuthentication no
PubkeyAuthentication yes
然后重启服务:systemctl restart sshd
创建config文件管理不同主机:
Host myserver
HostName server_ip
User username
IdentityFile ~/.ssh/custom_key
Port 2222
ssh-keygen -p -f ~/.ssh/id_rsa # 为现有密钥添加密码
结合Google Authenticator实现二次验证
getenforcessh-keygen -t ed25519ClientAliveInterval 300AllowUsers user@192.168.1.*