我认为我的根本问题是我不完全理解ssh / scp是如何工作的。 但是我不知道怎么去google上试试看我的问题。
问题描述
我在serverA上创build了一个bash脚本。 它试图从服务器A到服务器B scp文件。 该脚本将被安排为cron作业。
为了testing这个脚本是否可以正常工作,我只是试图手动将文件从serverA scp到serverB。 但是我得到了serverA上的“权限被拒绝”错误信息。
我首先login到serverA,像这样:
mycomputer#> ssh [email protected]
问题:
手动scptesting是一个好主意吗? 因为我使用我的密钥login到serverA,但最终,这是要触发scp命令的cron作业。
我如何知道cron作业将使用哪个用户ID /密钥?
我应该怎样谷歌更好地了解这一切是如何工作的?
谢谢。
如果你想自动化这样的事情,你将需要在任何一方设置公钥/私钥,然后在你想复制的方向configurationssh作为授权密钥。 假设您正在使用OpenSSh,请在两个系统上执行以下操作:
ssh-keygen -t dsa
cd〜/ .ssh
cp id_dsa身份
cp id_dsa.pub authorized_keys
cp id_dsa.pub systemname_id_dsa.pub
现在您需要将最后一个文件从源系统复制到目标系统的〜/ .ssh目录,然后运行以下命令:
cat systemname_id_dsa.pub >> authorized_keys
您现在应该能够从源到目标ssh / scp而无需该用户的密码。 我build议不要以root身份执行此操作,而是以另一个用户身份执行此操作,因为如果以root身份执行操作,则可能会造成安全问
为了更好的理解,我build议你从OpenSSH套件中查看这两个组件(你可以通过man ssh-keygen和/或man ssh-copy-id ):
SSH-KEYGEN(1) – authentication密钥的生成,pipe理和转换 SSH-COPY-ID(1) – 使用本地可用的密钥授权远程机器上的login 示例(如何制作密钥对并将其复制到远程主机):
SSH-KEYGEN(1) :
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/X/.ssh/id_rsa): Created directory '/X/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /X/.ssh/id_rsa. Your public key has been saved in /X/.ssh/id_rsa.pub. The key fingerprint is: e0:cd:fd:18:45:66:0d:11:a0:08:75:6a:f3:1a:6c:45 X@Z The key's randomart image is: +--[ RSA 2048]----+ | ... E ..B= | | . = . + . | | * o . | | + B . . | | = S o | | . o + | | . . . | | | | | +-----------------+ $
&
SSH-COPY-ID(1) :
$ ssh-copy-id root@Y The authenticity of host 'Y (ZZZZ)' can't be established. RSA key fingerprint is 5e:8e:ad:71:77:6a:c4:16:e6:0e:34:f8:92:b2:ce:9f. Are you sure you want to continue connecting (yes/no)? yes /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@Y's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@Y'" and check to make sure that only the key(s) you wanted were added. $
您然后将能够使用scp来复制没有密码提示…
手动scptesting是一个好主意吗? 因为我使用我的密钥login到serverA,但最终,这是要触发scp命令的cron作业。
在调度到cron之前,我会这样做手动testing,您不需要等待预定的时间即可知道预期结果。
我如何知道cron作业将使用哪个用户ID /密钥?
crond通常在根目录下运行,但要validation你可以使用以下内容:
# ps aux | grep crond | grep -v grep root 2696 0.0 0.0 126336 1712 ? Ss May13 0:01 /usr/sbin/crond -n #
要使用scp从服务器A复制到服务器B,您需要:
1)在应该“接收”文件的服务器B(用户B)上select一个用户。 该用户将需要在文件将被复制到的目录中的写入权限。
2)为该用户创build一个公钥/私钥对。
3)将公钥放在服务器B的/.ssh/allowedkey上。
4)select将发送文件的服务器A上的用户。 该用户将要求对要发送的文件具有读取权限。
5)将私钥放在服务器A上的/.ssh/userB
6)编写一个脚本来复制所需的文件。 这个副本就像scp -i〜/ .ssh / userB somefiles [email protected]:/ somedirectory /
7)以userA的身份login到服务器A,然后运行crontab -e编辑userA的crontab,并将脚本添加到crontab中并添加所需的设置。
这应该让你在那里。