ubuntu 16 上自訂建置 openssh-hpn-sshd 的無限啟動狀態

ubuntu 16 上自訂建置 openssh-hpn-sshd 的無限啟動狀態

我已經編譯了hpn版本的openssh(OpenSSH_7.2p2-hpn14v11),sshd本身運作得很好。問題是 systemd 每 2-3 分鐘就會重新啟動 sshd,因為它無法正確啟動服務。當我替換為相同版本的 Ubuntu 軟體包時,它可以正常工作。我甚至在虛擬機器上進行了全新安裝的測試 - 同樣的事情。我究竟做錯了什麼?

 ● ssh.service - OpenBSD Secure Shell server 
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled) 
Active: activating (start) since Wed 2016-09-28 20:18:49 EDT; 42s ago 
Main PID: 24279 (sshd) 
Tasks: 9 
Memory: 6.8M 
CPU: 164ms 
CGroup: /system.slice/ssh.service ├─20041 sshd: 
root@pts/0 ├─20047 
-bash ├─24279 
/usr/sbin/sshd -D ├─24628 
└─24629 pager
Sep 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
cat /lib/systemd/system/ssh.service
[Unit]
Description=OpenBSD Secure Shell server
After=network.target auditd.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run

[Service]
EnvironmentFile=-/etc/default/ssh
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify

[Install]
WantedBy=multi-user.target
Alias=sshd.service


Logs:
    Sep 29 02:22:03 xxx sshd[15007]: Server listening on 0.0.0.0 port 22.
    Sep 29 02:22:03 xxx sshd[15007]: Server listening on :: port 22.
    Sep 29 02:23:33 xxx systemd[1]: ssh.service: Start operation timed out. Terminating.
    Sep 29 02:23:33 xxx systemd[1]: Failed to start OpenBSD Secure Shell server.
    Sep 29 02:23:33 xxx systemd[1]: ssh.service: Unit entered failed state.
    Sep 29 02:23:33 xxx systemd[1]: ssh.service: Failed with result 'timeout'.
    Sep 29 02:23:33 xxx systemd[1]: ssh.service: Service hold-off time over, scheduling restart.
    Sep 29 02:23:33 xxx systemd[1]: Stopped OpenBSD Secure Shell server.
    Sep 29 02:23:33 xxx systemd[1]: Starting OpenBSD Secure Shell server...
    Sep 29 02:23:33 xxx sshd[15775]: Server listening on 0.0.0.0 port 22.
    Sep 29 02:23:33 xxx sshd[15775]: Server listening on :: port 22.

答案1

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
    
  2. 使用以下命令建立您的 OpenSSH修補來自 Debian/Ubuntu:


From fe97848e044743f0bac019a491ddf0138f84e14a Mon Sep 17 00:00:00 2001
From: Michael Biebl <[email protected]>
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);

答案2

截至今天,2019-10-28,在 Ubuntu 19.04 上編譯 openssh-8.1p1 仍然需要這樣做。

您可以在此處找到所需補丁的最新版本:https://salsa.debian.org/ssh-team/openssh/blob/master/debian/patches/systemd-readiness.patch

您還需要安裝pkg-config( apt-get install pkg-config),否則./configure --with-systemd將無法辨識libsystemd-dev補丁所需的庫。

答案3

sshdOpenSSH 伺服器陷入「啟動」狀態還有另一個原因。

也就是說,原因可能是“熵耗盡”,它阻止了相關 OpenSSL 模組的初始化。

要驗證「熵耗盡」確實是問題所在,請先停止 OpenSSH 服務,這樣它就不再停留在「啟動」狀態,方法是發出以下命令:

systemctl stop ssh

……停止後sshd,發出以下命令再次啟動它:

/usr/sbin/sshd -ddd

....並觀察偵錯輸出是否出現以下訊息:
順便一提:此-ddd選項在 OpenSSH 服務啟動期間啟用詳細偵錯輸出。

隨機:由於速率限製而錯過了 N 個隨機警告

如果在發出命令後出現此訊息/usr/sbin/sshd -ddd,則表示 OpenSSH 服務的 SSL 模組無法初始化,因為熵池已耗盡。

若要解決此問題,請發出命令:

apt-get install haveged

..這將安裝更快的熵源。您可以閱讀更多有關haveged 這裡

這使其成為永久修復。

相關內容