在CentOS7上更改端口号时报错:Job for sshd.service failed because the control process exited with error

发布时间 2023-10-30 10:57:08作者: hkgan

1、问题描述

在在CentOS7×上更改端口号时报错:

“Job for sshd.service failed because the control process exited with error code.See ‘systemcl status& sshd service" and fournalcti xe'for details.”

2、修改ssh端口号的方法

sudo vim /etclssh/sshd_config

将 “#Port 22” 改为 “Port 2222”
将 “#PermitRootLogin yes” 改为 “PermitRootLogin no”

重启: sudo systemctl restart sshd.service

重启后报错:Job for sshd.service failed because the control process exited with error code.See ‘systemcl status& sshd service" and fournalcti xe'for details.

3、原因分析

查看SElinux允许的ssh的端口号

需要用到semanage工具

yum install policycoreutils-python -y
# 该命令将安装policycoreutils-python软件包,其中包含了semanage工具。
# 查看SElinux允许的ssh的端口号
$ sudo semanage port -l | grep ssh
ssh_port_t                     tcp      22

发现SELinux允许的ssh的端口号和ssh配置文件中设置的端口号不一致,还是默认的22端口。

4、解决方法

使用semanage工具添加ssh端口号

sudo semanage port -a -t ssh_port_t -p tcp 2222

重启

sudo systemctl restart sshd.service

成功解决。