SSHD服务启动失败

一台在线才5天的服务器,用 lastb |wc -l 统计了下,有近十万次的暴力破解登录失败记录。隧想改下sshd 的监听端口,免得招蜂引蝶。
打开配置文件 /etc/ssh/sshd_config 把默认的22端口改成高位端口后,用 systemctl restart sshd.service 重启sshd服务失败。
报错如下:

Job for sshd.service failed because the control process exited with error code. See "systemctl status sshd.service" and "journalctl -xe" for details.

systemctl status sshd.service 的输出如下:

● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since 四 2018-09-27 10:10:42 CST; 38s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 47324 ExecStart=/usr/sbin/sshd -D $OPTIONS (code=exited, status=255)
Main PID: 47324 (code=exited, status=255)

9月 27 10:10:42 english systemd[1]: sshd.service: main process exited, code=exited, status=255/n/a
9月 27 10:10:42 english systemd[1]: Failed to start OpenSSH server daemon.
9月 27 10:10:42 english systemd[1]: Unit sshd.service entered failed state.
9月 27 10:10:42 english systemd[1]: sshd.service failed.

没啥有用的提示,再看 journalctl -xe

-- Unit sshd.service has begun starting up.
9月 27 10:12:07 english sshd[47352]: error: Bind to port 23389 on 0.0.0.0 failed: Permission denied.
9月 27 10:12:07 english sshd[47352]: error: Bind to port 23389 on :: failed: Permission denied.
9月 27 10:12:07 english sshd[47352]: fatal: Cannot bind any address.
9月 27 10:12:07 english systemd[1]: sshd.service: main process exited, code=exited, status=255/n/a
9月 27 10:12:07 english systemd[1]: Failed to start OpenSSH server daemon.
-- Subject: Unit sshd.service has failed
-- Defined-By: systemd

说我没权限绑定 23389 端口,老夫是 root 咋会没权限呢?/var/log/messages 里面没有啥有用的提示,/var/log/secure里面的提示跟上面显示一样,没权限。两头雾水。

经验告诉我,当日志里面没明显提示,而服务又是和安全相关的时候,锅往selinux上甩准没错,果然 getenforce 返回的是 Enforcing 。
执行 setenforce 0 关闭selinux后,一切正常。

最后提醒下,修改sshd配置文件属于高危操作,要慎之又慎,如上文,端口改成23389后,你重启sshd服务让修改生效,重启这个动作分解是这样的:
1、停止SSHD服务
2、用修改后的新配置文件启动SSHD服务,而此时配置文件有误,无法启动SSHD。
无法启动SSHD服务,这想想都刺激呢?若是云服务器还好,可以通过web控制台的console连接上去操作,若是托管在IDC机房的机器,只能去机房接显示器了。比较安全的方法就是用一个脚本每隔20分钟把正确的sshd配置文件覆盖回来,然后启动SSHD服务,当你确认修改成功后,再取消掉这个脚本。

原创文章,转载请注明: 转载自笛声

本文链接地址: SSHD服务启动失败

5 条评论

  • 西枫里博客 2018年10月18日 回复

    三头雾水,有没有自动运维脚本或者控制面板图形化可以操作的。我的端口还没改过呢

    • dige 2018年10月18日 回复

      1、你可以不修改,看看这个暴力破解次数什么时候上亿。
      2、宝塔面板上可以修改,点击左侧的“安全”,进入“系统安全”最上面就可以改SSHD端口。修改端口后,记得在系统防火墙上放行你修改后的端口,在云服务器的安全组放行端口。

  • 姜辰 2018年10月19日 回复

    我突然感觉自己是真的不怕作死。

    每次都是先修改ssh端口,然后重启。= =、

  • dige 2018年10月28日 回复

    IP测试。

  • 心灵博客 2019年6月3日 回复

    还要记得提前改好防火墙哦

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Copyright © 2015-2024 笛声博客 All Rights Reserved     浙ICP备15036123号-1