Linux设置SSH远程无密码登录

检查远程主机文件权限

请使用ssh登录查询你需要免密用户的家目录中:

  • ~/.ssh权限:700
  • ~/.ssh/authorized_keys:权限 600

如果不是请设置chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

设置主机无密码登录

  1. 本机生成SSH密钥键值对:ssh-keygen -t rsa
    image.png

  2. 添加SSH配置到远程服务器:ssh-copy-id -i ./xxxx.pub -p 端口 用户名@主机

    1
    2
    3
    4
    5
    Usage: /usr/bin/ssh-copy-id [-h|-?|-f|-n|-s] [-i [identity_file]] [-p port] [-F alternative ssh_config file] [[-o <ssh -o options>] ...] [user@]hostname
    -f: force mode -- copy keys without trying to check if they are already installed
    -n: dry run    -- no keys are actually copied
    -s: use sftp   -- use sftp instead of executing remote-commands. Can be useful if the remote only allows sftp
    -h|-?: print this help

我这里是已经执行之后的结果:
image.png

  1. 测试:ssh -p 端口 用户名@主机
    成功:
    image.png

如果出现需要输入密码,并且上一步已经设置成功的话,可以使用一下命令:ssh-add ~/.ssh/xxxx添加你的私钥到ssh-agent中
image.png


出现其他问题可以通过ssh -p 端口 用户名@主机 -vvv输出全部过程检查问题