场景:机器A想要无密码访问机器B
方法一:手工操作
- 在机器A执行,ssh-keygen 按几次回车
- 把生成的id_rsa.pub传输到机器B /tmp/ 目录下
- 把id_rsa.pub加入到authorized_keys里
- 如果没有authorized_keys,mv /tmp/id_rsa.pub ~/.ssh/authorized_keys
- 如果有authorized_keys, cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys
- 更改权限 chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys 一定要注意权限对不对,如果不对还是需要输入密码
方法二:使用ssh-copy-id
#步骤1: 用 ssh-key-gen 在本地主机上创建公钥和密钥
ssh-keygen -t rsa
#步骤2: 用 ssh-copy-id 把公钥复制到远程主机上
ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 1122 user@server"
# 拷贝authorized_keys
key='ssh-rsa xxx'
mkdir -p ~/.ssh && chmod 700 ~/.ssh
content=`cat ~/.ssh/authorized_keys |grep "$key"`
if (($? != 0)); then
echo $key >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
fi
参考:http://baiqiuyi.com/linux/ssh-sshkeygen-linux.html