在Linux环境下,两台主机之间传输文件一般使用scp命令,通常用scp命令通过ssh获取对方linux主机文件的时候都需要输入密码确认。
不过通过建立信任关系,可以实现不输入密码。
这里假设A的IP:192.168.10.1
B的IP:192.168.10.2
需要从A免密码输入复制文件至B。
在主机A上执行如下命令来生成配对密钥:
ssh-keygen -t rsa
按照提示操作,注意,不要输入passphrase。提示信息如下
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
ff:8e:85:68:85:94:7c:2c:46:b1:e5:2d:41:5c:e8:9b root@localhost.domain将 .ssh 目录中的 id_rsa.pub 文件复制到 主机B 的 ~/.ssh/ 目录中,并改名为 authorized_keys。
scp .ssh/id_rsa.pub 192.168.10.2:/root/.ssh/authorized_keys
注意:chmod 600 authorized_keys,其他权限不敢保证
以后从A主机scp到B主机就不需要密码了。
在第2步中如果机器中已经存在authorized_keys文件,则需注意,这个文件可以包含多个SSH验证信息,这时可用 cat >>命令将验证文件内容附加上去。
比如复制到scp .ssh/id_rsa.pub 192.168.10.2:/root/.ssh/a.pub
然后执行cat ~/.ssh/a.pub >> ~/.ssh/authorized_keys
3.编辑sshd_config文件
目录:/etc/ssh/sshd_config
RSAAuthentication yes #RSA验证打开
PubkeyAuthentication yes #Public key验证打开
AuthorizedKeysFile %h/.ssh/authorized_keys # 在设定若要使用不需要密码登入的账号时,那个账号的存放档案所在档名!