# ssh反复重启?
查看日志发现ssh在不停的重启
Aug 23 23:11:11 localhost systemd: sshd.service failed. Aug 23 23:11:53 localhost systemd: sshd.service holdoff time over, scheduling restart. Aug 23 23:11:53 localhost systemd: Stopped OpenSSH server daemon. Aug 23 23:11:53 localhost systemd: Starting OpenSSH server daemon... Aug 23 23:13:23 localhost systemd: sshd.service start operation timed out. Terminating. Aug 23 23:13:24 localhost systemd: Failed to start OpenSSH server daemon. Aug 23 23:13:24 localhost systemd: Unit sshd.service entered failed state. Aug 23 23:13:24 localhost systemd: sshd.service failed. Aug 23 23:14:06 localhost systemd: sshd.service holdoff time over, scheduling restart. Aug 23 23:14:06 localhost systemd: Stopped OpenSSH server daemon. Aug 23 23:14:06 localhost systemd: Starting OpenSSH server daemon... Aug 23 23:15:36 localhost systemd: sshd.service start operation timed out. Terminating. Aug 23 23:15:36 localhost systemd: Failed to start OpenSSH server daemon. Aug 23 23:15:36 localhost systemd: Unit sshd.service entered failed state.
systemctl status sshd发现处于 activating(start) 状态
journalctl -xe:
sshd.service start operation timed out. Terminating. sshd[8425]: Received signal 15; terminating. systemd[1]: Failed to start OpenSSH server daemon. -- Subject: Unit sshd.service has failed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit sshd.service has failed. -- -- The result is failed. systemd[1]: Unit sshd.service entered failed state. systemd[1]: sshd.service failed. polkitd[457]: Unregistered Authentication Agent for unix-process:8419:67059806 (system bus name :1.2658, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus)
ssh -t 没有输出
解释: 出现命令挂起的原因就是 sshd在启动完成后,没有给systemd发消息,systemd就一直在那傻等,所以下面我们就修改源码,添加消息;
源码修改:
在源码openssh-8.3p1目录下,找sshd.c这个主函数文件,找到调用server_accept_loop 这个函数的行,注意这个函数的定义也在这个文件,不要找错了!
前加一行代码,效果如下:
/* Signal systemd that we are ready to accept connections */ sd_notify(0, "READY=1"); /* Accept a connection and return in a forked child */ server_accept_loop(&sock_in, &sock_out,&newsock, config_s);
相应的,在源文件开头几行添加引用头文件:
#include <systemd/sd-daemon.h>
编译,安装
由于默认的依赖中,不包含sd_notify 这个函数,所以还需要安装依赖的包
yum install systemd-devel
编译时还需要在makefile中指明,编辑文件:Makefile ,找到变量 LIBS,修改如下:
LIBS=-lcrypto -ldl -lutil -lz -lcrypt -lresolv -lsystemd
下面就可以直接编译,安装
make & make install