ssh remote funktioniert über die Bash-Befehlszeile, jedoch nicht über ein Bash-Skript

0

(Hinweis: Auch wenn ich einige meiner eigenen Fragen beantwortet habe, habe ich am Ende dieses Beitrags immer noch eine unbeantwortete Frage.)

Wenn ich den folgenden Befehl über eine Bash-Befehlszeile ausführe, funktioniert er.

ssh -t -v [email protected] sudo /home/my-rails-project/x/pg_rsync_ralph07_to_ralph12_finish

Und die ausführliche Ausgabe von ssh ist:

OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 192.168.29.205 [192.168.29.205] port 22.
debug1: Connection established.
debug1: identity file /home/my-rails-project/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/my-rails-project/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/my-rails-project/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/my-rails-project/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/my-rails-project/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/my-rails-project/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/my-rails-project/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/my-rails-project/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.29.205:22 as 'my-rails-project'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: [email protected]
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:EnDsn2kFQgiA56e4B2UuvDr3ajTLlN7/15lxLItnjUQ
debug1: Host '192.168.29.205' is known and matches the ECDSA host key.
debug1: Found key in /home/my-rails-project/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/my-rails-project/.ssh/id_rsa
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug1: Authentication succeeded (publickey).
Authenticated to 192.168.29.205 ([192.168.29.205]:22).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype [email protected] want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: sudo /home/my-rails-project/x/pg_rsync_ralph07_to_ralph12_finish

Die exakt gleiche Zeile steht in einem Bash-Skript und ich bekomme

OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 192.168.29.205 [192.168.29.205] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.29.205:22 as 'my-rails-project'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: [email protected]
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:EnDsn2kFQgiA56e4B2UuvDr3ajTLlN7/15lxLItnjUQ
debug1: Host '192.168.29.205' is known and matches the ECDSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
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: Trying private key: /root/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).

Die beiden Ausgänge weichen an der Leitung voneinander ab (Skript)

debug1: Found key in /root/.ssh/known_hosts:1

(Befehlszeile)

debug1: Found key in /home/my-rails-project/.ssh/known_hosts:1

Ich weiß nicht, ob diese Abweichung auf das Problem hindeutet.

Meine Frage lautet also:

Warum sucht ssh in einem Skript nach /root/.ssh/known_hosts und in der Befehlszeile nach /home/my-rails-project/.ssh/known_hosts?

Meine eigene Frage gleich oben beantworten:

Ok, als ich dies schrieb, wurde die Antwort auf die Divergenz klar: Ich habe das Skript als ausgeführt

sudo myScript

Meine weitere Frage lautet also: Wie kann ich ssh anweisen, /home/my-rails-project/.ssh/known_hosts für known_hosts zu verwenden?

Beantwortung meiner eigenen Frage gleich oben

Ein bisschen Recherche führte mich zur Antwort in https://stackoverflow.com/questions/10765946/ssh-use-known-hosts-other-than-home-ssh-known-hosts

So sieht nun meine Kommandozeile aus

ssh -o UserKnownHostsFile=/home/my-rails-project/.ssh/known_hosts -t -v [email protected] sudo /home/my-rails-project/x/pg_rsync_ralph07_to_ralph12_finish

Die Befehlszeile direkt darüber funktioniert wieder, wenn ich kein Sudo davor habe. Es schlägt fehl, wenn ich ein Sudo davor benutze. Die ausführliche Ausgabe ist:

my-rails-project@my-rails-project:~$ sudo ssh -o UserKnownHostsFile=/home/my-rails-project/.ssh/known_hosts -t -v [email protected] sudo /home/my-rails-project/x/pg_rsync_ralph07_to_ralph12_finish
[sudo] password for my-rails-project: 
OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 192.168.29.205 [192.168.29.205] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.29.205:22 as 'my-rails-project'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: [email protected]
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:EnDsn2kFQgiA56e4B2UuvDr3ajTLlN7/15lxLItnjUQ
debug1: Host '192.168.29.205' is known and matches the ECDSA host key.
debug1: Found key in /home/my-rails-project/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
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: Trying private key: /root/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).
my-rails-project@my-rails-project:~$ 

Und die ausführlichen Sudo- und Nicht-Sudo-Ausgaben weichen gegen Ende der ausführlichen Auflistung in der Nähe ab

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: Trying private key: /root/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).
my-rails-project@my-rails-project:~$ 

Frage:

Wie kann ich ssh anweisen, /home/my-rails-project/.ssh anstelle von /root/.ssh für meinen privaten Schlüssel zu verwenden und trotzdem auf dem Zielsystem sudo zu sein?

Meine eigene Frage gleich oben beantworten:

Anschauen http://manpages.ubuntu.com/manpages/trusty/man1/ssh.1.html

Nun ist die ssh Kommandozeile (Hinzufügen eines -ich Möglichkeit)

ssh -o UserKnownHostsFile=/home/my-rails-project/.ssh/known_hosts -i /home/my-rails-project/.ssh/id_rsa -t -v [email protected] sudo /home/my-rails-project/x/pg_rsync_ralph07_to_ralph12_finish

Und dann habe ich Erfolg, außer dass etwas (ssh?) Nach einem Passwort fragt. Die Ausgabe sieht nun so aus:

my-rails-project@my-rails-project:~$ sudo ssh -o UserKnownHostsFile=/home/my-rails-project/.ssh/known_hosts -i /home/my-rails-project/.ssh/id_rsa -t -v [email protected] sudo /home/my-rails-project/x/pg_rsync_ralph07_to_ralph12_finish
OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 192.168.29.205 [192.168.29.205] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /home/my-rails-project/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/my-rails-project/.ssh/id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.29.205:22 as 'my-rails-project'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: [email protected]
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:EnDsn2kFQgiA56e4B2UuvDr3ajTLlN7/15lxLItnjUQ
debug1: Host '192.168.29.205' is known and matches the ECDSA host key.
debug1: Found key in /home/my-rails-project/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/my-rails-project/.ssh/id_rsa
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
Enter passphrase for key '/home/my-rails-project/.ssh/id_rsa': 
debug1: Authentication succeeded (publickey).
Authenticated to 192.168.29.205 ([192.168.29.205]:22).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype [email protected] want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: sudo /home/my-rails-project/x/pg_rsync_ralph07_to_ralph12_finish

Frage:

Ich erhalte die folgende Aufforderung:

Enter passphrase for key '/home/my-rails-project/.ssh/id_rsa': 

Wie unterdrücke ich diese Aufforderung zur Eingabe eines Passworts?

Ich habe es geschafft, die meisten Anfragen nach Passwörtern zu unterdrücken, indem ich /etc/sudoers.d/ralph auf beiden Rechnern massiert habe (Ralph07 und Ralph12)

# This script should only be run on Ralph07
my-rails-project ALL=(ALL:ALL) NOPASSWD: /home/my-rails-project/x/pg_rsync_ralph07_to_ralph12

# This script should only be run on Ralph12
my-rails-project ALL=(ALL:ALL) NOPASSWD: /home/my-rails-project/x/pg_rsync_ralph07_to_ralph12_finish
RalphShnelvar
quelle

Antworten:

1

Die Eingabeaufforderung 'Passwort für Schlüssel eingeben' wird angezeigt, da Ihr SSH-Schlüssel mit einem Passwort erstellt wurde.

Sie können die Passphrase folgendermaßen von Ihrem SSH-Schlüssel entfernen:

ssh-keygen -p -N '' -f ~/my-rails-project/.ssh/id_rsa

Oder Sie können einen neuen Schlüssel ohne Passphrase erstellen:

ssh-keygen -q -t rsa -b 4096 -N '' -C '' -f ~/my-rails-project/.ssh/id_rsa

Sie können sich auch das anschauen sshpass Tool, mit dem Passphrasen für ssh-Befehle bereitgestellt werden können, das jedoch nicht empfohlen wird, da es einige Sicherheitsprobleme aufwirft.

ewatt
quelle
1
Oh wow! Vielen Dank! Ich wünschte, ich hätte mehr als 15 Reputationspunkte, damit ich Ihre Antwort abstimmen könnte.
RalphShnelvar