SSH本地 ~/.ssh下
在本地客户端执行
ssh-keygen
一路回车
~/.ssh下会新增两个文件
id_rsa 私钥
id_rsa.pub 公钥
上传公钥,可直接在客户端执行
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.235.22
服务端会新增authorized_keys文件
如添加公钥在服务端后,连接仍需要密码
修改 /etc/ssh/sshd_config
#禁用密码验证
#PasswordAuthentication no
#启用密钥验证
RSAAuthentication yes
PubkeyAuthentication yes
还有一种可能可在~/.ssh下输入 ll 查看
需要执行命令
chmod 600 authorized_keys
在 ll,可发现
也可以通过
service sshd status (ubuntu)
systemctl status sshd (centos)
查看日志
如果含有Authentication refused: bad ownership or modes for directory /root
或者Authentication refused: bad ownership or modes for directory /root/.ssh
因为之前有过经验id_ras的权限非600,导致免密失败,检查发现id_ras权限正常600,然后检查发现root目录(或者*.ssh*)的权限配置成了777,改成700后,免密正常登陆。
更多文章可以访问我的blog网站