防火墙iptables.service服务
查看iptables.service状态,
[root@NEO1000 log]# systemctl start iptables.service
[root@NEO1000 log]# systemctl status iptables.service
iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled)
Active: active (exited) since 日 2022-08-07 02:31:08 CST; 1s ago
Process: 2876 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
8月 07 02:31:08 NEO1000 iptables.init[2876]: iptables: Applying firewall rules: [ 确定 ]
8月 07 02:31:08 NEO1000 systemd[1]: Started IPv4 firewall with iptables.
关闭防火墙
[root@NEO1000 log]# systemctl disable iptables.service
[root@NEO1000 log]# systemctl stop iptables.service
关闭selinux
[root@NEO1000 log]# cat /etc/selinux/config | grep -Ev '^$|^#'
SELINUX=disabled
SELINUXTYPE=targetted
iptable规则
服务器会通过网卡收到很多数据包,当数据包与规则匹配时,iptables就根据规则所定义的方法来处理这些数据包,如放行(accept)、拒绝(reject)和丢弃(drop)等。配置防火墙的主要工作就是添加、修改和删除这些规则。
显示规则的命令格式如下:
iptables [-t tables] [-L] [-nv]
-t :后面接 table ,例如 nat 或 filter ,若省略此项目,则使用默认的 filter
-L :列出某个 table 的所有链或某个链的规则
-n :直接显示 IP,速度会快很多
-v :列出更多的信息,包括通过该规则的数据包总位数、相关的
iptables-save 另一种显示风格
清除规则的命令格式如下:
iptables [-t tables] [-FXZ]
-F:清除所有已制定的规则
-X:删除所有使用者自定义的 chain(其是 tables)
-Z:将所有的 chain 的计数与流量统计都清零
[root@NEO1000 log]# iptables -L #列出某个 table 的所有链或某个链的规则
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
[root@NEO1000 log]# iptables -L INPUT #列出input的规则
Chain INPUT (policy ACCEPT)
target prot opt source destination
添加两条规则
[root@NEO1000 log]# iptables -A INPUT -s 192.168.0.0/16 -j ACCEPT
#接受192.168网段的数据包
[root@NEO1000 log]# iptables -A INPUT -s 100.100.0.0/16 -j DROP
#丢弃100.100网段的数据包
[root@NEO1000 log]# iptables -L -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
162 10025 ACCEPT tcp -- any any anywhere anywhere tcp dpt:ssh
0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:csms2
0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:rtmp-port
0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:vt-ssl
0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:802-11-iapp
6 588 ACCEPT icmp -- any any anywhere anywhere
0 0 ACCEPT udp -- any any anywhere anywhere udp dpt:personal-agent
616 33007 ACCEPT all -- any any 192.168.0.0/16 anywhere
0 0 DROP all -- any any 100.100.0.0/16 anywhere
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 159 packets, 11420 bytes)
pkts bytes target prot opt in out source destination
具体规则说明
iptables [-A I chain] [-i o interface] [-p 协议] [-s 来源 IP] [-d 目标 IP] -j [ACCEPT,DROP,REJECT,LOG]
-A:针对某个规则链添加一条规则,新添加的规则排在现有规则的后面。
-I:针对某个规则链插入一条规则,可以为新插入的规则指定在链中的序号。如果不指定序号,则新的规则会变成第一条规则。
-i:指定数据包进入的那个网络接口。
-o: 指定传出数据包的那个网络接口。
-p: 指定此规则适用于那种网络协议(常用的协议有 tcp、udp、icmp,all 指适用于所有的协议)。
-s:指定数据包的来源 IP/网段,可以指定单个 IP,如 192.168.1.100,也可以指定一个网段,如 192.168.1.0/24。还可以通过 !表示非的意思,如 ! 192.168.1.0/24 表示除了 192.168.1.0/24 之外的数据包。
-d:指定数据包的目标 IP/网段,其它与 -s 选项相同。
-j:指定匹配成功后的行为,主要有 ACCEPT、DROP、REJECT 和 LOG。