Linux使用的是SSH密钥对认证实现免密登录

原创评论64字数 460

在Linux中,设置免密登录通常使用的是SSH密钥对认证。这种方法不需要在每次登录时输入密码。以下是设置SSH免密登录的步骤:

步骤1:生成SSH密钥对

在客户端机器上(即你要登录的那台机器),使用以下命令生成SSH密钥对:文章源自合一学院-https://unvmax.com/linux%e4%bd%bf%e7%94%a8%e7%9a%84%e6%98%afssh%e5%af%86%e9%92%a5%e5%af%b9%e8%ae%a4%e8%af%81%e5%ae%9e%e7%8e%b0%e5%85%8d%e5%af%86%e7%99%bb%e5%bd%95.html

ssh-keygen -t rsa

执行后会提示你选择保存密钥对的位置,默认是 ~/.ssh/id_rsa。你可以直接按 Enter 使用默认路径。然后,会提示你输入密码短语(可以留空,直接按 Enter)。文章源自合一学院-https://unvmax.com/linux%e4%bd%bf%e7%94%a8%e7%9a%84%e6%98%afssh%e5%af%86%e9%92%a5%e5%af%b9%e8%ae%a4%e8%af%81%e5%ae%9e%e7%8e%b0%e5%85%8d%e5%af%86%e7%99%bb%e5%bd%95.html

步骤2:复制公钥到远程服务器

接下来,需要将生成的公钥文件 id_rsa.pub 复制到你要登录的远程服务器上。使用以下命令:文章源自合一学院-https://unvmax.com/linux%e4%bd%bf%e7%94%a8%e7%9a%84%e6%98%afssh%e5%af%86%e9%92%a5%e5%af%b9%e8%ae%a4%e8%af%81%e5%ae%9e%e7%8e%b0%e5%85%8d%e5%af%86%e7%99%bb%e5%bd%95.html

ssh-copy-id user@remote_server_ip

user 替换为你在远程服务器上的用户名,将 remote_server_ip 替换为远程服务器的IP地址。系统会要求你输入远程服务器的密码,输入正确后公钥就会自动添加到远程服务器的 ~/.ssh/authorized_keys 文件中。文章源自合一学院-https://unvmax.com/linux%e4%bd%bf%e7%94%a8%e7%9a%84%e6%98%afssh%e5%af%86%e9%92%a5%e5%af%b9%e8%ae%a4%e8%af%81%e5%ae%9e%e7%8e%b0%e5%85%8d%e5%af%86%e7%99%bb%e5%bd%95.html

步骤3:验证免密登录

现在你可以尝试登录远程服务器,而不需要输入密码:文章源自合一学院-https://unvmax.com/linux%e4%bd%bf%e7%94%a8%e7%9a%84%e6%98%afssh%e5%af%86%e9%92%a5%e5%af%b9%e8%ae%a4%e8%af%81%e5%ae%9e%e7%8e%b0%e5%85%8d%e5%af%86%e7%99%bb%e5%bd%95.html

ssh user@remote_server_ip

如果一切设置正确,你应该能够直接登录,而无需输入密码。文章源自合一学院-https://unvmax.com/linux%e4%bd%bf%e7%94%a8%e7%9a%84%e6%98%afssh%e5%af%86%e9%92%a5%e5%af%b9%e8%ae%a4%e8%af%81%e5%ae%9e%e7%8e%b0%e5%85%8d%e5%af%86%e7%99%bb%e5%bd%95.html

注意事项:

  1. 远程服务器的SSH配置文件(/etc/ssh/sshd_config)中应该允许公钥认证(默认是开启的)。可以检查以下选项是否开启:
    PubkeyAuthentication yes

    如果进行了修改,请使用以下命令重启SSH服务:文章源自合一学院-https://unvmax.com/linux%e4%bd%bf%e7%94%a8%e7%9a%84%e6%98%afssh%e5%af%86%e9%92%a5%e5%af%b9%e8%ae%a4%e8%af%81%e5%ae%9e%e7%8e%b0%e5%85%8d%e5%af%86%e7%99%bb%e5%bd%95.html

    sudo systemctl restart sshd
  2. 本地客户端和远程服务器的 ~/.ssh 目录权限应为700,authorized_keys 文件权限应为600,否则可能会导致认证失败。

这样就能成功配置Linux系统的免密登录了!文章源自合一学院-https://unvmax.com/linux%e4%bd%bf%e7%94%a8%e7%9a%84%e6%98%afssh%e5%af%86%e9%92%a5%e5%af%b9%e8%ae%a4%e8%af%81%e5%ae%9e%e7%8e%b0%e5%85%8d%e5%af%86%e7%99%bb%e5%bd%95.html 文章源自合一学院-https://unvmax.com/linux%e4%bd%bf%e7%94%a8%e7%9a%84%e6%98%afssh%e5%af%86%e9%92%a5%e5%af%b9%e8%ae%a4%e8%af%81%e5%ae%9e%e7%8e%b0%e5%85%8d%e5%af%86%e7%99%bb%e5%bd%95.html

匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证