使用 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

nc如果您有 OpenSSH 5.4 或更高版本,您可以使用 ssh 内置等效项替换 netcat 的使用-W

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

答案2

该服务可能会在网络完全启动之前尝试启动。尝试替换After=network.targetAfter=network-online.target.

请参阅网络目标页面有关区别的更多信息,请访问 systemd wiki。

答案3

在终端中以 root (sudo) 身份运行 autossh 命令并批准真实性(即键入“yes”)。这还会检查身份文件的路径是否正确。

相关内容