Debian12启用密钥登录

生成密钥

#ssh-keygen
#或者:
ssh-keygen -t rsa

在服务器上安装公钥

cd .ssh
cat id_rsa.pub >> authorized_keys

如此便完成了公钥的安装。为了确保连接成功,请保证以下文件权限正确:

chmod 600 authorized_keys
chmod 700 ~/.ssh

设置 SSH,打开密钥登录功能

编辑/etc/ssh/sshd_config

nano /etc/ssh/sshd_config

配置

#yes表示允许密钥登陆
PubkeyAuthentication yes
#指定密钥的文件位置
AuthorizedKeysFile      .ssh/authorized_keys .ssh/authorized_keys2
#不允许使用密码登陆,等测试密钥登陆成功了再修改此条,以防无法登陆
PasswordAuthentication no

sshd_config的最终配置

Port 22

# 只用公钥认证(禁止密码认证)
PasswordAuthentication no
ChallengeResponseAuthentication no
KbdInteractiveAuthentication no

# 启用公钥认证并强制只通过公钥认证
PubkeyAuthentication yes
AuthenticationMethods publickey

# 允许 root 通过公钥登录,但禁止密码登录 root
PermitRootLogin prohibit-password   # "without-password" 为旧版写法

# 明确授权密钥文件路径(更稳妥)
AuthorizedKeysFile %h/.ssh/authorized_keys

# 如果你只想允许 root 登录(禁止其它用户)
AllowUsers root

# PAM 可以保留以支持会话管理,但因为密码认证被关闭,不会允许密码登录
UsePAM yes

# 一些强化项(可选但建议)
PermitEmptyPasswords no
MaxAuthTries 3
#AllowTcpForwarding no     # 根据需求调整
#X11Forwarding yes         # 如需 X11 保留

重启ssh服务:

service sshd restart

设置私钥

https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
下载puttygen.exe,把私钥转换为putty可用模式
File----->Load Private key,找到刚才另存为的id_rsa文件,打开,点Save private key,然后保存为一个ppk的文件

无标签
评论区
头像