Centos7修改ssh服务监听端口
因为安全检查,所有的业务系统不能再使用22,3389,445,21,80等端口,以下记录Centos7系统修改ssh监听端口的方法,为什么要单独记录呢,因为从Centos7开始,修改方法就已经有一点小的改变,就是一小点的改变就让人很头疼。
[root@web-server ~]# vim /etc/ssh/sshd_config
#将屏蔽22的#号解除屏蔽状态,新增一行 端口改为你需要的端口号 注:不能服务器现有端口号冲突
-
SSH默认监听端口是22,如果你不强制说明别的端口,”Port 22”注不注释都是开放22访问端口。上面我保留了22端口,防止之后因为各种权限和配置问题,导致连22端口都不能访问了,那就尴尬了。等一切都ok了,再关闭22端口。
Ok,继续,我增加了10086端口,大家修改端口时候最好挑10000~65535之间的端口号,10000以下容易被系统或一些特殊软件占用,或是以后新应用准备占用该端口的时候,却被你先占用了,导致软件无法运行。
第二步:如果你关闭了SELinux,可以忽略第二步。
#修改SELINUX=disabled
[root@web-server ~]# vi /etc/selinux/config
#让配置文件生效
[root@web-server ~]# source /etc/selinux/config
[root@web-server ~]# semanage port -l|grep ssh
ssh_port_t tcp 22
如果你的系统是最小安装,可能会提示没得 `semanage` 这个命令,可以使用yum进行安装
#使用以下安装命令安装`semanage` 前提是你部署了yum源
[root@web-server ~]# yum install policycoreutils-python
[root@web-server ~]# semanage port -a -t ssh_port_t -p tcp 10086
ssh_port_t tcp 22,10086
[root@web-server ~]# semanage port -l|grep ssh
ssh_port_t tcp 22,10086
第三步:如果你关闭了防火墙,可以忽略第三步,话说防火墙不开启太危险了,建议开启。
[root@web-server ~]# firewall-cmd --permanent --query-port=10086/tcp
FirewallD is not running #提示FirewallD is not running,表示防火墙没有运行
[root@web-server ~]# systemctl start firewalld.service #开启防火墙
[root@web-server ~]# systemctl enable firewalld.service #开启防火墙开机自启动
[root@web-server ~]# firewall-cmd --permanent --query-port=10086/tc #再次进行查询
no #显示no表示该端口未开启,那么我们添加该端口
[root@web-server ~]#firewall-cmd --permanent --add-port=10086/tcp #添加10086tcp协议允许通过
success #显示success表示添加成功
[root@web-server ~]# firewall-cmd --reload #重信加载防火墙策略
[root@web-server ~]# firewall-cmd --permanent --query-port=10086/tcp #再次查看防火墙是否开启10086端口
yes #显示yes代表我们已经成功将ssh新加的端口加入到允许通过的列表
第四步:重启SSH服务和防火墙,最好也重启下服务器
[root@web-server ~]# systemctl restart sshd #重启ssh的服务,让修改的端口监听配置生效
[root@web-server ~]# systemctl restart firewalld.service #重启防火墙的服务
第五步:尝试通过10086端口登录SSH,或者进入该服务器直接本地访问SSH如下:
[root@web-server ~]# ssh root@localhost -p 10086 #可以弹出密码验证窗口代表修改成功
[root@web-server ~]# vim /etc/ssh/sshd_config #将配置文件22端口行删除掉

































