Skip to content

CentOS7开启SELinux下修改SSH端口

CentOS7在默认情况下是开启SELinux的,如果直接修改sshd_config来修改ssh端口的话,会造成ssh无法正常开启,需要向SELinux中添加修改的ssh端口。

使用semanage工具

semanage是SELinux的管理工具,依赖工具包policycoreutils-python。 先通过yum安装该依赖:

[root@server ~]# yum -y install policycoreutils-python

安装后即可使用semanage:

[root@server ~]# semanage 
boolean     export      import      login       node        port        
dontaudit   fcontext    interface   module      permissive  user

先查询当前ssh服务端口:

[root@server ~]# semanage port -l |grep ssh
ssh_port_t                     tcp      22

向SELinux中添加ssh端口,如8922

[root@server ~]# semanage port -a -t ssh_port_t -p tcp 8922

再查询ssh服务端口:

[root@server ~]# semanage port -l |grep ssh
ssh_port_t                     tcp      8922, 22

发现已经添加了8922端口,此时再修改sshd_config来添加或修改ssh的端口即可。

[root@server ~]# vi /etc/ssh/sshd_config 
[root@server ~]# systemctl restart sshd
[root@server ~]# ss -ntlp
State       Recv-Q Send-Q          Local Address:Port                         Peer Address:Port              
LISTEN      0      128                         *:22                                      *:*                   users:(("sshd",pid=12345,fd=3))
LISTEN      0      128                         *:8922                                    *:*                   users:(("sshd",pid=12345,fd=5))