重启后autossh无法重新连接

时间:2019-02-14 04:36:32

标签: linux bash ssh cron boot

我试图在每次重新启动时使用Cron来启动autossh连接。

启动它的命令是以下自动连接:

autossh -M 0 -N -q -o ServerAliveInterval=20 -o ServerAliveCountMax=3 3 HMzg8fspYGfW11oJ7Jl6bkDnT2lYrB@fw1.sshreach.me -i prvtkey2.pem -R 13532:localhost:22 -v

我将此命令放入start-autossh.sh内名为/home/debian的bash脚本中。 prvtkey.pem私钥也位于此文件夹中。我使用Cron重新启动时启动此脚本,如下所示:

@reboot 30;cd /home/debian && sh start-autossh.sh

起初效果不错。但是,当我重新启动系统时,它不起作用。以下是连接成功与失败(重新启动后)时来自autossh的日志。

我想知道重启是否实际上不能正常终止当前的autossh连接,因此,当启动后尝试重新连接时,服务器会拒绝。如果这是问题所在,我如何确保autossh连接在重新启动时被正确终止?

自动连接成功后:

OpenSSH_7.4p1 Debian-10+deb9u4, OpenSSL 1.0.2l  25 May 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to fw1.sshreach.me [139.162.161.211] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file prvtkey2.pem type -1
debug1: key_load_public: No such file or directory
debug1: identity file prvtkey2.pem-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4p1 Debian-10+deb9u4
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6
debug1: match: OpenSSH_6.6 pat OpenSSH_6.5*,OpenSSH_6.6* compat 0x14000000
debug1: Authenticating to fw1.sshreach.me:22 as 
'HMzg8fspYGfW11oJ7Jl6bkDnT2lYrB'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: ecdh-sha2-nistp256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: 
<implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: 
<implicit> compression: none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 
SHA256:nCeg0Bt8GiwhKOuEf4Q72pWxxjasEIbxm4yRhAqgkos
debug1: Host 'fw1.sshreach.me' is known and matches the ECDSA host key.
debug1: Found key in /home/debian/.ssh/known_hosts:3
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: prvtkey2.pem
debug1: Authentication succeeded (publickey).
Authenticated to fw1.sshreach.me ([139.162.161.211]:22).
debug1: Remote connections from LOCALHOST:13532 forwarded to local address 
localhost:22
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: Remote: Forwarding listen address "localhost" overridden by server 
GatewayPorts
debug1: remote forward success for: listen 13532, connect localhost:22
debug1: All remote forwarding requests processed

当autossh连接失败时,重新引导后(它停留在SSH2_MSG_KEXINIT发送状态):

OpenSSH_7.4p1 Debian-10+deb9u4, OpenSSL 1.0.2l  25 May 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to fw1.sshreach.me [139.162.161.211] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file prvtkey2.pem type -1
debug1: key_load_public: No such file or directory
debug1: identity file prvtkey2.pem-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4p1 Debian-10+deb9u4
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6
debug1: match: OpenSSH_6.6 pat OpenSSH_6.5*,OpenSSH_6.6* compat 0x14000000
debug1: Authenticating to fw1.sshreach.me:22 as 
'HMzg8fspYGfW11oJ7Jl6bkDnT2lYrB'
debug1: SSH2_MSG_KEXINIT sent

0 个答案:

没有答案