配置SSH免密
生成公钥、私钥
ssh-keygen
会让你输出公钥和私钥的存储地址
默认公钥在/root/.ssh/id_rsa.pub,私钥在/root/.ssh/id_rsa复制公钥到远程
ssh-copy-id -i /root/.ssh/id_rsa.pub root@xxx.xxx.xxx.xxx
这一行命令是把本端公钥放在追加到远程~/.ssh/authorized_keys
文件内直接登录
ssh -p 22 root@xxx.xxx.xxx.xxx
配置远程服务器别名
vi ~/.ssh/config
配置文件配置Host别名
#Use only the configured key
IdentitiesOnly=yes
# Add keys to the agent as required on first connection
AddKeysToAgent=yes
# Forward the agent credentials, which allows ssh'ing to nodes your job is running on
ForwardAgent=yes
Host unity
Hostname=unity.uri.edu
User=username_uri_edu
# 这里配置的路径是私钥
IdentityFile=~/.ssh/id_unity
Host andromeda
Hostname=ssh3.hac.uri.edu
User=username
IdentityFile=~/.ssh/id_rsa
配置免密失效的原因及解决方法
原因
- 客户端私钥文件路径未配置或配置错误
- 服务端配置错误
- 权限错误
/etc/ssh/sshd_config
文件配置错误
解决方法
检查客户端 ~/.ssh/config 文件,配置方法如前所述
在服务端修改权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
配置 /etc/ssh/sshd_config
文件,修改以下内容,启用秘钥验证
PubkeyAuthentication yes
修改后重启 sshd 服务
service sshd restart
重启后须重新执行配置拷贝公钥操作
Ref:
https://its.uri.edu/research-computing/advanced-ssh-configuration/
https://blog.csdn.net/sculpta/article/details/108130992