Chinaunix首页 | 论坛 | 博客
  • 博客访问: 219174
  • 博文数量: 70
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 440
  • 用 户 组: 普通用户
  • 注册时间: 2015-08-09 09:16
个人简介

淡泊以明志,宁静以致远。

文章分类

全部博文(70)

分类: LINUX

2019-01-12 18:34:09

由于服务器处于公网上,默认的远程端口22,在上线不到24小时的时间内,就会不断地遭到端口扫描攻击,因此推荐将SSH远程端口由默认的22端口,修改为其它端口。这里以修改为5122端口为例说明。
1. 查看当前远程端口,如下,显示监听端口是22:
[root@iqtzs88 ~]# ss -tulnp | grep sshd
tcp    LISTEN     0      128       *:22                    *:*                   users:(("sshd",pid=4164,fd=3))
tcp    LISTEN     0      128      :::22                   :::*                   users:(("sshd",pid=4164,fd=4))2.

2. 使用vi或vim 修改ssh服务的配置文件
/etc/ssh/sshd_config,同时添加 22 5122 两个端口,以免端口添加失败,导致自己无法远程登录服务器。
如下所示:

[root@iqtzs88 ~]# vim /etc/ssh/sshd_config
Port 22
Port 5122
:wq
# 保存退出。

3. 向SELinux中添加修改的SSH端口
在向SELinux中添加端口之前,需要先安装SELinux的管理工具 semanage ,在此我们查询semange由哪个软件安装包提供,使用命令:yum provides semanage
经查询得知,semanage 是由 policycoreutils-python 安装包提供的,我们在此安装此软件 :,
[root@iqtzs88 ~]# yum install policycoreutils-python

安装好之后,可以直接使用 semanage 命令:
查询当前 ssh 服务端口: semanage port -l | grep ssh
[root@iqtzs88 ~]# semanage port -l | grep ssh
ssh_port_t                     tcp      22

向 SELinux 中添加 ssh 端口: semanage port -a -t ssh_port_t -p tcp 5122
[root@iqtzs88 ~]# semanage port -a -t ssh_port_t -p tcp 5122

验证 ssh 端口是否添加成功:
[root@iqtzs88 ~]# semanage port -l | grep ssh
ssh_port_t                     tcp      5122, 22

查看到5122端口和22端口都在SELinux中添加成功。

4.重启 ssh 服务

[root@iqtzs88 ~]# systemctl restart sshd.service
[root@iqtzs88 ~]# ss -tulnp | grep sshd
tcp    LISTEN     0      128       *:22                    *:*                   users:(("sshd",pid=4265,fd=5))
tcp    LISTEN     0      128       *:5122                  *:*                   users:(("sshd",pid=4265,fd=3))
tcp    LISTEN     0      128      :::22                   :::*                   users:(("sshd",pid=4265,fd=6))
tcp    LISTEN     0      128      :::5122                 :::*                   users:(("sshd",pid=4265,fd=4))
可以看到22和5122端口已处于监听状态。

5. 在firewalld防火墙上放行5122端口
[root@iqtzs88 ~]# firewall-cmd --zone=public --add-port=5122/tcp --permanent
success
重新载入防火墙配置:
[root@iqtzs88 ~]# firewall-cmd --reload
success
查询5122端口是否添加成功:
[root@iqtzs88 ~]# firewall-cmd --zone=public --query-port=5122/tcp
yes
可以看到5122端口已经成功添加到防火墙。
现在已经可以通过5122端口进行SSH远程登录了。登录后,在 /etc/ssh/sshd_config 配置文件中,将 Port 22 注释禁用掉,再重启sshd服务即可。


参考:https://blog.csdn.net/mrqiang9001/article/details/78308830
阅读(2337) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~