我有 2 台服务器,Server1
并且Server2
.我Server1
有一个名为 的用户user1
。我Server2
的用户名为user2
.
我需要能够编写一个在 上运行的脚本Server1
,它将scp
一些文件传输到user2@Server2
.有什么方法可以在不提示输入密码的情况下执行此操作吗?如果需要的话,我可以将密码放在配置文件或其他文件中。
user2@Server1
顺便说一句,我无法创建用户。
答案1
您想要的是 ssh 密钥对,它们创建允许无密码身份验证的“可信网络”:
在您的客户端(服务器1)上:
[user@server1]# ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): # Hit Enter
Enter passphrase (empty for no passphrase): # Hit Enter
Enter same passphrase again: # Hit Enter
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
现在将您的公钥复制到远程服务器(server2):
ssh-copy-id user2@server2
[OR]
cat ~/.ssh/id_rsa.pub | ssh user2@server2 "mkdir -p ~/.ssh \
&& cat >> ~/.ssh/authorized_keys"
现在,当您运行scp
(或任何其他 ssh)命令时,不应提示您输入密码:
scp file user2@server2:/drop/location