ubuntu 16 上自定义构建 openssh-hpn-sshd 的无限激活状态

Str*_*r04 5 ssh ubuntu systemd

我已经编译了hpn版本的openssh(OpenSSH_7.2p2-hpn14v11),sshd本身工作得很好。问题是 systemd 每 2-3 分钟就会重新启动 sshd,因为它无法正确启动该服务。当我替换为相同版本的 Ubuntu 软件包时,它可以正常工作。我什至在虚拟机上进行了全新安装的测试 - 同样的事情。我究竟做错了什么?

\n\n
\n
 \xe2\x97\x8f ssh.service - OpenBSD Secure Shell server \nLoaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled) \nActive: activating (start) since Wed 2016-09-28 20:18:49 EDT; 42s ago \nMain PID: 24279 (sshd) \nTasks: 9 \nMemory: 6.8M \nCPU: 164ms \nCGroup: /system.slice/ssh.service \xe2\x94\x9c\xe2\x94\x8020041 sshd: \nroot@pts/0 \xe2\x94\x9c\xe2\x94\x8020047 \n-bash \xe2\x94\x9c\xe2\x94\x8024279 \n/usr/sbin/sshd -D \xe2\x94\x9c\xe2\x94\x8024628 \n\xe2\x94\x94\xe2\x94\x8024629 pager\nSep 28 20:18:49 hostname systemd[1]: Starting OpenBSD Secure Shell server... Sep 28 20:18:49 hostname sshd[24279]: Server listening on 0.0.0.0 port 22\n
Run Code Online (Sandbox Code Playgroud)\n
\n\n
cat /lib/systemd/system/ssh.service\n[Unit]\nDescription=OpenBSD Secure Shell server\nAfter=network.target auditd.service\nConditionPathExists=!/etc/ssh/sshd_not_to_be_run\n\n[Service]\nEnvironmentFile=-/etc/default/ssh\nExecStart=/usr/sbin/sshd -D $SSHD_OPTS\nExecReload=/bin/kill -HUP $MAINPID\nKillMode=process\nRestart=on-failure\nRestartPreventExitStatus=255\nType=notify\n\n[Install]\nWantedBy=multi-user.target\nAlias=sshd.service\n\n\nLogs:\n    Sep 29 02:22:03 xxx sshd[15007]: Server listening on 0.0.0.0 port 22.\n    Sep 29 02:22:03 xxx sshd[15007]: Server listening on :: port 22.\n    Sep 29 02:23:33 xxx systemd[1]: ssh.service: Start operation timed out. Terminating.\n    Sep 29 02:23:33 xxx systemd[1]: Failed to start OpenBSD Secure Shell server.\n    Sep 29 02:23:33 xxx systemd[1]: ssh.service: Unit entered failed state.\n    Sep 29 02:23:33 xxx systemd[1]: ssh.service: Failed with result 'timeout'.\n    Sep 29 02:23:33 xxx systemd[1]: ssh.service: Service hold-off time over, scheduling restart.\n    Sep 29 02:23:33 xxx systemd[1]: Stopped OpenBSD Secure Shell server.\n    Sep 29 02:23:33 xxx systemd[1]: Starting OpenBSD Secure Shell server...\n    Sep 29 02:23:33 xxx sshd[15775]: Server listening on 0.0.0.0 port 22.\n    Sep 29 02:23:33 xxx sshd[15775]: Server listening on :: port 22.\n
Run Code Online (Sandbox Code Playgroud)\n

Jak*_*uje 6

Ubuntu 放弃使用 systemd 方式让 systemd 知道它何时启动。从选项中可以明显看出Type=notify,没有Systemd补丁就无法使用OpenSSH。有两种可能的解决方案:

  1. 将行更改Type=notifyType=forking并添加一个新行,PIDFile=/var/run/sshd.pid并且ExecStart应更改为/usr/sbin/sshd $SSHD_OPTS

    Type=forking
    PIDFile=/var/run/sshd.pid
    ExecStart=/usr/sbin/sshd $SSHD_OPTS
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用 Debian/Ubuntu 的补丁构建您的 OpenSSH :


From fe97848e044743f0bac019a491ddf0138f84e14a Mon Sep 17 00:00:00 2001
From: Michael Biebl <biebl@debian.org>
Date: Mon, 21 Dec 2015 16:08:47 +0000
Subject: Add systemd readiness notification support

Bug-Debian: https://bugs.debian.org/778913
Forwarded: no
Last-Update: 2016-01-04

Patch-Name: systemd-readiness.patch
---
 configure.ac | 24 ++++++++++++++++++++++++
 sshd.c       |  9 +++++++++
 2 files changed, 33 insertions(+)

diff --git a/configure.ac b/configure.ac
index f822fb3..6cafb15 100644
--- a/configure.ac
+++ b/configure.ac
@@ -4319,6 +4319,29 @@ AC_ARG_WITH([kerberos5],
 AC_SUBST([GSSLIBS])
 AC_SUBST([K5LIBS])

+# Check whether user wants systemd support
+SYSTEMD_MSG="no"
+AC_ARG_WITH(systemd,
+   [  --with-systemd          Enable systemd support],
+   [ if test "x$withval" != "xno" ; then
+       AC_PATH_TOOL([PKGCONFIG], [pkg-config], [no])
+       if test "$PKGCONFIG" != "no"; then
+           AC_MSG_CHECKING([for libsystemd])
+           if $PKGCONFIG --exists libsystemd; then
+               SYSTEMD_CFLAGS=`$PKGCONFIG --cflags libsystemd`
+               SYSTEMD_LIBS=`$PKGCONFIG --libs libsystemd`
+               CPPFLAGS="$CPPFLAGS $SYSTEMD_CFLAGS"
+               SSHDLIBS="$SSHDLIBS $SYSTEMD_LIBS"
+               AC_MSG_RESULT([yes])
+               AC_DEFINE(HAVE_SYSTEMD, 1, [Define if you want systemd support.])
+               SYSTEMD_MSG="yes"
+           else
+               AC_MSG_RESULT([no])
+           fi
+       fi
+   fi ]
+)
+
 # Looking for programs, paths and files

 PRIVSEP_PATH=/var/empty
@@ -5121,6 +5144,7 @@ echo "                   libedit support: $LIBEDIT_MSG"
 echo "  Solaris process contract support: $SPC_MSG"
 echo "           Solaris project support: $SP_MSG"
 echo "         Solaris privilege support: $SPP_MSG"
+echo "                   systemd support: $SYSTEMD_MSG"
 echo "       IP address in \$DISPLAY hack: $DISPLAY_HACK_MSG"
 echo "           Translate v4 in v6 hack: $IPV4_IN6_HACK_MSG"
 echo "                  BSD Auth support: $BSD_AUTH_MSG"
diff --git a/sshd.c b/sshd.c
index 837409b..868df9e 100644
--- a/sshd.c
+++ b/sshd.c
@@ -85,6 +85,10 @@
 #include <prot.h>
 #endif

+#ifdef HAVE_SYSTEMD
+#include <systemd/sd-daemon.h>
+#endif
+
 #include "xmalloc.h"
 #include "ssh.h"
 #include "ssh1.h"
@@ -2117,6 +2121,11 @@ main(int ac, char **av)
            unsetenv("SSH_SIGSTOP");
        }

+#ifdef HAVE_SYSTEMD
+       /* Signal systemd that we are ready to accept connections */
+       sd_notify(0, "READY=1");
+#endif
+
        /* Accept a connection and return in a forked child */
        server_accept_loop(&sock_in, &sock_out,
            &newsock, config_s);
Run Code Online (Sandbox Code Playgroud)