如果在 sshd_config 中使用 ListenAddress,则 ssh.service 将不会在启动时启动,但如果在启动后手动启动,则它会启动

如果在 sshd_config 中使用 ListenAddress,则 ssh.service 将不会在启动时启动,但如果在启动后手动启动,则它会启动

/etc/ssh/sshd_config我的.txt文件中有 ListenAddressssh.service不会在启动时启动,但如果我service ssh start在启动后运行它就会启动。

这是来自的错误/var/log/syslog

$ sudo cat /var/log/syslog | grep -i ssh
Feb  9 10:03:50 nook systemd[1]: ssh.service: Main process exited, code=exited, status=255/n/a
Feb  9 10:03:50 nook systemd[1]: ssh.service: Unit entered failed state.
Feb  9 10:03:50 nook systemd[1]: ssh.service: Failed with result 'exit-code'.

我在网上查了一下,但似乎无法弄清楚出了什么问题。如果我在启动后手动启动它,它会起作用,但它不会在启动时启动。 :/

这是我的/etc/ssh/sshd_config

UsePAM yes
PrintMotd no
AcceptEnv LANG LC_*
HostKey /etc/ssh/ssh_host_ed25519_key
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
KexAlgorithms [email protected],ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha256
Ciphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr
MACs [email protected],[email protected],[email protected],hmac-sha2-512,hmac-sha2-256,[email protected]
LogLevel VERBOSE
Subsystem sftp  /usr/lib/ssh/sftp-server -f AUTHPRIV -l INFO
UsePrivilegeSeparation sandbox
Protocol 2
X11Forwarding no
AllowTcpForwarding no
AllowStreamLocalForwarding no
GatewayPorts no
PermitTunnel no
PermitEmptyPasswords no
IgnoreRhosts yes
UseDNS yes
Compression no
TCPKeepAlive no
AllowAgentForwarding no
PermitRootLogin no
AllowGroups sshusers
ClientAliveCountMax 0
ClientAliveInterval 300
LoginGraceTime 30
ListenAddress 192.168.1.100   # IP of server
MaxAuthTries 2
MaxSessions 2
MaxStartups 2
ChallengeResponseAuthentication yes

我的完整启动日志/var/log/syslog位于https://pastebin.com/SfjPQzXu

答案1

我有同样的问题。问题的根本原因是开箱即用的 sshd 单元文件刚刚网络.目标作为依赖。当您在 sshd 配置文件中显式指定 IP 地址时,必须在 sshd 启动之前在接口上配置它,而 network.target 不应该关心这一点。要解决该问题,您需要在 root 下的控制台中执行以下操作:

# systemctl edit sshd

编辑器打开后,定义对 network-online.target 的依赖关系:

[Unit]
After=network-online.target

'systemctl edit ...'命令创建一个文件

/etc/systemd/system/${SERVICE_NAME}.d/override.conf

使用您的输入并在保存后重新加载依赖关系树,通常在手动编辑单元文件后执行“systemctl daemon-reload”来实现。

请注意:不要编辑 /lib/systemd/system 下的单元文件,因为它们由包管理器管理,并将在下次更新时被覆盖。

然而,将 network-online.target 作为单元依赖项仍然不够。在大多数随 systemd 一起提供的基于 Linux 的操作系统上,network-online.target 在 network.target 之后立即启动。这是因为默认情况下,network-online.target 依赖的另一个服务被禁用。它被称为systemd-networkd-wait-online.service。因此请确保 systemd-networkd-wait-online.service 已启用

# systemctl is-enabled systemd-networkd-wait-online
disabled
# systemctl enable systemd-networkd-wait-online


更新:该建议仅适用于使用 systemd-networkd 进行网络管理的操作系统。对于未启用 systemd-networkd 的系统,“重新启动=失败时“选项可能会有所帮助”重启秒=5“控制启动尝试之间的睡眠

相关内容