systemdで始まるときのAutossh

systemdで始まるときのAutossh

以下のガイドに従って、systemdでautosshを使用して起動時に永続SSH接続を開始しようとしています。これ。これにより、ログに情報がほとんどなく接続され、すぐに切断されます。私のシステムは(uname -a)です:

Linux local_machine_name 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u2 (2017-06-26) x86_64 GNU/Linux

実行時間:

autossh -M 0 dbase1

上記のコマンドを使用すると、数日間接続が確実に維持されます。 dbase1は私の設定ファイルで定義されており、次のようになります(匿名)。

Host dbase1
HostName x.x.x.x
User serverusername
LocalForward 54320 localhost:5432
ServerAliveInterval 30
ServerAliveCountMax 3
ExitOnForwardFailure yes
ProxyCommand ssh -q [email protected] nc %h %p 2> /dev/null

その後、/etc/systemd/system/に次のサービスを作成しました。

[Unit]
Description=AutoSSH tunnel service
After=network.target
[Service]
Type=simple
User=*username with cert and config file in home/username/.ssh/ folder*
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -vvv -M 0 dbase1
[Install]
WantedBy=multi-user.target

systemctlデーモンを再ロードしてサービスを開始した後、接続が確立された後に接続が失われます。これはJournalctl -u myautossh.serviceの出力です(リモートサーバーからのウェルカムメッセージで始まり、接続を確認します)。

Jul 21 14:38:30 local_machine_name autossh[555]: *** Connection successful, welcome to the remote server! ***
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: client_input_channel_req: channel 2 rtype exit-status reply 
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: rcvd eof
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: output open -> drain
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: obuf empty
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: close_write
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: output drain -> closed
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: rcvd close
Jul 21 14:38:30 local_machine_name autossh[555]: debug3: channel 2: will not send data after close
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: almost dead
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: gc: notify user
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: gc: user detached
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: send close
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: is dead
Jul 21 14:38:30 local_machine_name autossh[555]: debug2: channel 2: garbage collecting
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: channel 2: free: client-session, nchannels 3
Jul 21 14:38:30 local_machine_name autossh[555]: debug3: channel 2: status: The following connections are open:
Jul 21 14:38:30 local_machine_name autossh[555]: #2 client-session (t4 r0 i3/0 o3/0 fd -1/-1 cc -1)
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: channel 0: free: port listener, nchannels 2
Jul 21 14:38:30 local_machine_name autossh[555]: debug3: channel 0: status: The following connections are open:
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: channel 1: free: port listener, nchannels 1
Jul 21 14:38:30 local_machine_name autossh[555]: debug3: channel 1: status: The following connections are open:
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: fd 0 clearing O_NONBLOCK
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: fd 1 clearing O_NONBLOCK
Jul 21 14:38:30 local_machine_name autossh[555]: debug3: fd 2 is not O_NONBLOCK
Jul 21 14:38:30 local_machine_name autossh[555]: Transferred: sent 3372, received 3384 bytes, in 0.7 seconds
Jul 21 14:38:30 local_machine_name autossh[555]: Bytes per second: sent 4633.6, received 4650.1
Jul 21 14:38:30 local_machine_name autossh[555]: debug1: Exit status 0

/var/log/syslogを見ると、次の行だけが追加されます(サーバーに正常に接続し、上記のエラーメッセージが表示された後)。

Jul 21 14:38:30 local_machine_name autossh[555]: ssh exited with status 0; autossh exiting

なぜ接続が切断されたのかを知っている人はいますか?

ベストアンサー1

問題は、systemdに標準入力がないため、sshコマンドが接続してから入力を読み込もうとし、eofで停止することです。欠落しているオプションは次のとおりです-N

ExecStart=/usr/bin/autossh -vvv -N -M 0 dbase1

ncOpenSSH 5.4以降がある場合は、netcatの使用をssh組み込みの機能に置き換えることができます-W

ProxyCommand ssh -q -W %h:%p [email protected]

おすすめ記事