2016-12-14 268 views
1

我在兩個站點之間通過生成公鑰並將它們複製到其他站點來配置SSH連接。現在我可以不用密碼就可以從一個站點連接到另一個站點,但是如果我想寫一個bash腳本來與ssh連接,它仍然會要求輸入密碼。我應該怎麼做,在運行腳本之後,我不會要求輸入密碼?在bash腳本中的SSH仍然提示輸入密碼,而命令行上的ssh使用密鑰驗證

編輯:添加腳本。我在station1上,我想連接到station2(主機)。

#!/bin/bash 
ssh host 

EDIT2:ssh -v輸出

1)從腳本

OpenSSH_5.9p1 Debian-5ubuntu1.1, OpenSSL 1.0.1 14 Mar 2012 
debug1: Reading configuration data /root/.ssh/config 
debug1: Reading configuration data /etc/ssh/ssh_config 
debug1: /etc/ssh/ssh_config line 19: Applying options for * 
debug1: Connecting to host [IP] port 22. 
debug1: Connection established. 
debug1: permanently_set_uid: 0/0 
debug1: identity file /root/.ssh/id_rsa type -1 
debug1: identity file /root/.ssh/id_rsa-cert type -1 
debug1: identity file /root/.ssh/id_dsa type -1 
debug1: identity file /root/.ssh/id_dsa-cert type -1 
debug1: identity file /root/.ssh/id_ecdsa type -1 
debug1: identity file /root/.ssh/id_ecdsa-cert type -1 
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1 
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1 pat OpenSSH* 
debug1: Enabling compatibility mode for protocol 2.0 
debug1: Local version string SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1 
debug1: SSH2_MSG_KEXINIT sent 
debug1: SSH2_MSG_KEXINIT received 
debug1: kex: server->client aes128-ctr hmac-md5 none 
debug1: kex: client->server aes128-ctr hmac-md5 none 
debug1: sending SSH2_MSG_KEX_ECDH_INIT 
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY 
debug1: Server host key: ECDSA f5:6c:00:ed:78:ac:1d:79:dc:71:57:6f:f5:09:fe:3a 
debug1: Host 'host' is known and matches the ECDSA host key. 
debug1: Found key in /root/.ssh/known_hosts:3 
debug1: ssh_ecdsa_verify: signature correct 
debug1: SSH2_MSG_NEWKEYS sent 
debug1: expecting SSH2_MSG_NEWKEYS 
debug1: SSH2_MSG_NEWKEYS received 
debug1: Roaming not allowed by server 
debug1: SSH2_MSG_SERVICE_REQUEST sent 
debug1: SSH2_MSG_SERVICE_ACCEPT received 
debug1: Authentications that can continue: publickey 
debug1: Next authentication method: publickey 
debug1: Trying private key: /root/.ssh/id_rsa 
debug1: Trying private key: /root/.ssh/id_dsa 
debug1: Trying private key: /root/.ssh/id_ecdsa 
debug1: No more authentication methods to try. 
Permission denied (publickey). 

2)從命令行

OpenSSH_5.9p1 Debian-5ubuntu1.1, OpenSSL 1.0.1 14 Mar 2012 
debug1: Reading configuration data /home/user/.ssh/config 
debug1: Reading configuration data /etc/ssh/ssh_config 
debug1: /etc/ssh/ssh_config line 19: Applying options for * 
debug1: Connecting to host [IP] port 22. 
debug1: Connection established. 
debug1: identity file /home/user/.ssh/id_rsa type 1 
debug1: Checking blacklist file /usr/share/ssh/blacklist.RSA-2048 
debug1: Checking blacklist file /etc/ssh/blacklist.RSA-2048 
debug1: identity file /home/user/.ssh/id_rsa-cert type -1 
debug1: identity file /home/user/.ssh/id_dsa type -1 
debug1: identity file /home/user/.ssh/id_dsa-cert type -1 
debug1: identity file /home/user/.ssh/id_ecdsa type -1 
debug1: identity file /home/user/.ssh/id_ecdsa-cert type -1 
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1 
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1 pat OpenSSH* 
debug1: Enabling compatibility mode for protocol 2.0 
debug1: Local version string SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1 
debug1: SSH2_MSG_KEXINIT sent 
debug1: SSH2_MSG_KEXINIT received 
debug1: kex: server->client aes128-ctr hmac-md5 none 
debug1: kex: client->server aes128-ctr hmac-md5 none 
debug1: sending SSH2_MSG_KEX_ECDH_INIT 
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY 
debug1: Server host key: ECDSA f5:6c:00:ed:78:ac:1d:79:dc:71:57:6f:f5:09:fe:3a 
debug1: Host 'host' is known and matches the ECDSA host key. 
debug1: Found key in /home/user/.ssh/known_hosts:2 
debug1: ssh_ecdsa_verify: signature correct 
debug1: SSH2_MSG_NEWKEYS sent 
debug1: expecting SSH2_MSG_NEWKEYS 
debug1: SSH2_MSG_NEWKEYS received 
debug1: Roaming not allowed by server 
debug1: SSH2_MSG_SERVICE_REQUEST sent 
debug1: SSH2_MSG_SERVICE_ACCEPT received 
debug1: Authentications that can continue: publickey 
debug1: Next authentication method: publickey 
debug1: Offering RSA public key: /home/user/.ssh/id_rsa 
debug1: Server accepts key: pkalg ssh-rsa blen 279 
debug1: read PEM private key done: type RSA 
debug1: Authentication succeeded (publickey). 
Authenticated to host ([IP]:Port). 
debug1: channel 0: new [client-session] 
debug1: Requesting [email protected] 
debug1: Entering interactive session. 
debug1: Sending environment. 

EDIT3:站2名是主機

+1

請提供有問題的腳本。 – cmidi

+0

主要通過'ssh -v'開關提供腳本輸出。 *「我可以從一個站點連接到另一個站點而無需密碼」* –

回答

4

你的腳本下root帳戶下運行:

debug1: Reading configuration data /root/.ssh/config 

所以不會使用user賬戶的鑰匙。


我們不知道,您如何運行腳本。

但是,也許你可以使用susudo運行:

su - user -c /home/user/script.sh 
+1

「ole」,「腳本正在作爲另一個用戶好友運行...」問題。 –

2

你可以使用這個

ssh -i /path/to/key/file [email protected] 

或在你的〜/ .ssh/config中,添加文本這樣

Host foobar 
    HostName 1.2.3.4 
    Port 22 
    User root 
    IdentityFile ~/.ssh/id_rsa 

現在,你可以登錄這個

ssh foobar