centos7.4查看防火墙状态的命令(CentOS8.0开启iptable防火墙)
当明白如何查询Linux系统下的各种日志文档时,通过命令:,我来为大家讲解一下关于centos7.4查看防火墙状态的命令?跟着小编一起来看一看吧!
centos7.4查看防火墙状态的命令
1. 介绍当明白如何查询Linux系统下的各种日志文档时,通过命令:
lastb | awk '{ print $3}' | sort | uniq -c | sort -n
筛选到,有大量的国外ip地址,频繁尝试登录服务器。
一开始,我只是将相关IP地址通过阿里云的安全组进行添加,但是操作过程太繁琐了。
而阿里云的云防火墙又比较贵。
所以,我直接通过iptables防火墙进行限制相关ip的访问吧。
2. iptables防火墙首先,安装iptables防火墙:示例如下:yum install iptables -y
[root@iZuf63tu3fn1swasqa62h8Z ~]# yum install iptables -y
Last metadata expiration check: 0:04:11 ago on Sun 11 Dec 2022 04:52:13 PM CST.
Package iptables-1.8.4-10.el8.x86_64 is already installed.
Dependencies resolved.
============================================================================
....
.....
Complete!
在安装服务: yum install iptables-services -y
[root@iZuf63tu3fn1swasqa62h8Z ~]# yum install iptables-services -y
CentOS-8 - Base 117 kB/s | 3.9 kB 00:00
Dependencies resolved.
==================================================================================================
Package Architecture Version Repository Size
==================================================================================================
....
Complete!
出现了Complete就代表安装成功了。
3. 配置iptables首先,我们先配置防火墙,让它开放所有的请求:
[root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -P INPUT ACCEPT
然后,执行清空iptables的默认规则:
[root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -F
清除所有自定义的规则:(PS:新安装并且没有配置过。可以不用清理,但是如果你不确定。可以尝试清理一下。)
[root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -X
防火墙计数器清理:
[root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -Z
当我们清理完毕后,通过 iptables -L -n 输出如下所示:
[root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
代表我们的防火墙,没有任何的限制,进出访问无限制。
3.1 配置本机127.0.0.1访问本地我们首先需要放行本地的服务器允许访问。例如我们通过Nginx代理访问本地的其他对的Web服务器。
那都是外网通过访问nginx,然后nginx再访问我们服务器本地的各种服务:iptables -A INPUT -i lo -j ACCEPT
示例效果如下:
[root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -A INPUT -i lo -j ACCEPT
[root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
我们配置完毕后,就会在INPUT下看到一条规则。也就是我们刚才配置的允许访问本地127.0.0.1地址的服务。
3.2 配置允许访问的端口例如默认SSH端口为22,我们可以通过命名:
[root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
开放22端口。 我们通过-L -n 就可看到新增加的规则了:
[root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
如果,你想删除该端口配置。那么可以通过:iptables -D INPUT 2 进行删除。
ps:每一条规则是按照顺序从1开始进行排序的。我们如果想删除哪条。就选择删除哪个就可以了。
如果想删除OUTPUT或FORWARD,可以设置为:
iptables -D OUTPUT 1 或 iptables -D FORWARD1
我们如果SSH端口 不是22,可以改为指定的端口。
还可以配置允许 443端口(https请求),80端口(http请求)
3.3 允许已建立的或相关连的通行
[root@iZuf63tu3fn1swasqa62h8Z ~]#iptables -A INPUT -p tcp --dport 80 -j ACCEPT [root@iZuf63tu3fn1swasqa62h8Z ~]#iptables -A INPUT -p tcp --dport 443 -j ACCEPT
[root@iZuf63tu3fn1swasqa62h8Z ~]#iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
上面这个也是必须的,它是允许所有对外请求的返回包。例如在服务器上执行下载或者安装命令,那么服务器就需要访问外网数据,那得到的返回数据包对于我们本地服务器来说,就是一个INPUT事件了。
添加成功后在规则中的展示效果如下:
3.4 过滤其他规则
[root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED
当我们配置完毕允许开放的规则之后,添加过滤规则。有两种添加方式:
- 过滤所有非以上规则的请求: iptables -P INPUT DROP。
- 其他访问规则禁止访问:iptables -A INPUT -j REJECT。
上面两个命令,执行其中一个都可以了。 执行第一个命名后的效果:
[root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -L -n Chain INPUT (policy DROP) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
我们会看到Chain INPUT (policy ACCEPT) 变成了Chain INPUT (policy DROP)
而如果执行的第二条命名就会:
Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
到这里,我们的端口开放就配置完毕了。也就是说只有这三个端口才能访问服务器,如果是其他端口。就会直接被拒绝。
PS:我们可以通过开放和关闭80端口,来验证一下防火墙是否正常拦截。
注意:你如果是使用的阿里云服务器,在阿里云后台中的安全组中也要开放相关的端口。否则仍然无法访问。
3.5. 屏蔽IP我们上面指定了固定开放的端口。其他端口全部禁止访问。如果有攻击的ip地址,例如通过lastb 命令查询到的大量工具访问的ip。我们如何添加到防火墙配置中呢?很简单,直接指定IP禁止访问即可:
[root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -I INPUT -s 85.31.46.0/24 -j DROP [root@iZuf63tu3fn1swasqa62h8Z ~]# iptables -I INPUT -s 170.64.130.0/24 -j DROP
0/24 代表屏蔽了:170.64.130.0~170.64.130.255 之间的全部ip。
5. 保存iptables 配置当我们配置完毕后,需要执行保存操作。否则重启系统后。规则会丢失。因为当前操作只是存储在了内存中。
关键命令为: service iptables save。具体示例如下所示:
[root@iZuf63tu3fn1swasqa62h8Z ~]# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
就代表保存成功了。我们还可以将iptables添加到自启动chkconfig中,示例如下:
[root@iZuf63tu3fn1swasqa62h8Z ~]# chkconfig iptables on Note: Forwarding request to 'systemctl enable iptables.service'. Created symlink /etc/systemd/system/multi-user.target.wants/iptables.service → /usr/lib/systemd/system/iptables.service. [root@iZuf63tu3fn1swasqa62h8Z ~]#
通过这个方法,我们可以限制更多的IP地址,突破阿里云安全组可限制的ip范围。
,
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com