本地公钥写入远程服务器 authorized_keys

  1. ssh-copy-id -i ~/.ssh/id_rsa.pub login_user@host_ip -p ssh_port

密钥登录远程服务器

  1. # root 用户执行一下命令,cat /etc/passwd 查看所有用户信息
  2. useradd -d /home/github github # 创建用户 github 制定 HOME 目录,也可以使用 adduser,adduser会自动创建 HOME 目录
  3. passwd github # 设置密码
  4. # 将 github 用户添加到 root 用户组,可将用户加到多个用户组,usermod -a -G groupname username
  5. usermod github -G root

远程服务器允许密钥登录设置(vim /etc/ssh/sshd_config ):

  • StrictModes yes 改成 StrictModes no (去掉注释后改成 no)
  • 找到 #PubkeyAuthentication yes 改成 PubkeyAuthentication yes (去掉注释)
  • 找到 #AuthorizedKeysFile .ssh/authorized_keys 改成 AuthorizedKeysFile .ssh/authorized_keys (去掉注释)

保存后,重启 sshd,systemctl restart sshd

  1. # 在要远程登录的主机,生成密钥
  2. ssh-keygen -t rsa -b 4096 -C "1329441308@qq.com"
  3. # 手动上传公钥到远程服务器
  4. cat ~/.ssh/id_rsa.pub | ssh github@111.230.58.139 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
  5. # ssh 密钥登录测试
  6. ssh -p 965 -i id_rsa github@111.230.58.139

scp

  1. scp -P 965 -r root@111.230.58.139:/www/wwwroot/shan333.cn shan333.cn

async

  1. rsync -e 'ssh -p 965' -rv root@111.230.58.139:/www/wwwroot/shan333.cn temp2

troubleshoot