如何使用公钥在 bash 脚本中执行 SCP



我谦虚地道歉,但我在网上到处寻找,我仍然无法做到这一点。这是我迄今为止找到的最好的指南。我也用这个作为指南。仍然没有任何效果。

我需要执行一个自动将本地文件发送到远程计算机的脚本。本地和远程机器都是 Linux。 编辑:脚本不应该提示用户输入密码 - 因此我应该使用公钥。

到目前为止我做了什么:

  • 编辑:执行评估"ssh-agent">,然后执行ssh-add,然后执行ssh-copy-id

  • 在本地机器上执行ssh-keygen,以在 ~/.ssh 文件夹中生成id_rsaid_rsa.pub

  • ssh-keygen中使用了 NO 密码短语
  • id_rsa.pub发送到远程计算机的 ~/.ssh 文件夹中
  • 远程机器中的id_rsa.pub重命名为authorized_keys(因为它最初不存在)

脚本文件(在本地计算机中)

#!/bin/bash    
scp -i ~/.ssh/id_rsa -o BatchMode=yes -v file.txt meuser@remotemachine:/home/meuser

SCP详细模式的输出:

./scp_example.sh
Executing: program /usr/bin/ssh host webui01, user meuser2, command scp -v -t /home/meuser
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to remotemachine [###.###.###.###] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 504/505
debug1: identity file /home/meuser/.ssh/id_rsa type 1
debug1: loaded 1 keys
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-cbc hmac-md5 none
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'remotemachine' is known and matches the RSA host key.
debug1: Found key in /home/meuser/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
Unknown code krb5 195
debug1: Unspecified GSS failure.  Minor code may provide more information
Unknown code krb5 195
debug1: Unspecified GSS failure.  Minor code may provide more information
Unknown code krb5 195
debug1: Next authentication method: publickey
debug1: Offering public key: /home/meuser/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: No more authentication methods to try.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
lost connection

希望有人能阐明这一点。

谢谢和最好的问候。

您提供的密钥被拒绝。查看服务器日志以了解原因,确保主目录、.ssh.ssh/authorized_keyus由正确的用户拥有,并且不能由其他任何人写入(这是最常见的错误)。

相关内容

  • 没有找到相关文章

最新更新