Debian系统设置SSH密钥登陆

Linux · 2023-09-10

先生成属于你的密钥~

ssh-keygen -t rsa  # 生成配对密钥,后续一路enter即可

会在用户目录(即~这个)下生成.ssh文件夹,里面的id_rsa是私钥,id_rsa.pub是公钥。
输入cd ~/.ssh/进入.ssh文件夹中,将公钥写入到authorized_keys中,将id_rsa下载并保存好。

touch authorized_keys
cat id_rsa.pub >> authorized_keys # 写入公钥写入

此时,还需要给文件相应的权限,不然可能无法正常工作

chmod 600 authorized_keys
chmod 700 ~/.ssh

现在密钥已经配对好了,还需要修改ssh的配置文件,打开/etc/ssh/sshd_config文件,修改其中的关键文件
默认的配置文件是已经有的不过没打开 只需要把前面的#取消掉即可 可参考如下

PubkeyAuthentication yes # yes表示允许密钥登陆
AuthorizedKeysFile      .ssh/authorized_keys .ssh/authorized_keys2 # 指定密钥的文件位置

不允许使用密码登陆,(如果你想既可以密码登录也可以密钥登录这个可以无视)等测试密钥登陆成功了再修改此条,以防无法登陆

PasswordAuthentication no

到此,SSH密钥登陆就配置完成了,重启ssh服务即可systemctl restart ssh

Linux Debian SSH
Theme Jasmine by Kent Liao