Linux配置无需密码的SCP
有的时候脚本里需要一些远处拷贝,自动化执行需要没有密码输入提示的SCP过程,本人参考了很多文档,几乎都是不够详细,昨天实验后试出肯定有效地方法。
1.首先 我们的环境是 master.test.com slave.test.com两台主机,需要从master拷贝文件到slave而无需密码
2.登录master段 执行创建密钥对的命令:
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user//.ssh/id_rsa): /home/user//.ssh/id_rsa_test
Enter passphrase (empty for no passphrase): #这里不输入任何密码
Enter same passphrase again: #这里不输入任何密码
Your identification has been saved in /home/user//.ssh/id_rsa_test.
Your public key has been saved in /home/user//.ssh/id_rsa_test.pub.
The key fingerprint is:
e3:71:f3:23:bb:1a:a1:c7:da:0b:f5:43:59:e6:5a:f3 user@master
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user//.ssh/id_rsa): /home/user//.ssh/id_rsa_test
Enter passphrase (empty for no passphrase): #这里不输入任何密码
Enter same passphrase again: #这里不输入任何密码
Your identification has been saved in /home/user//.ssh/id_rsa_test.
Your public key has been saved in /home/user//.ssh/id_rsa_test.pub.
The key fingerprint is:
e3:71:f3:23:bb:1a:a1:c7:da:0b:f5:43:59:e6:5a:f3 user@master
3.把公钥拷贝到slave端:
scp /home/user/.ssh/id_rsa_test.pub testuser@slave.test.com:/home/testuser
4.登录salve端:
把文件拷贝到家目录下 .ssh目录
家目录所在位置 可以用 env | grep ‘HOME’ 查询
cat /home/testuser/id_rsa_test.pub >> /home/testuser/.ssh/authorized_keys
chown -R testuser:testuser /home/testuser/.ssh
chown -R testuser:testuser /home/testuser/.ssh
5.从master端用相同用户,登录测试
[user@master~ ]$ ssh testuser@slave.test.com -i /home/user/.ssh/id_rsa_test #使用刚刚创建的相同的私钥去比对