<linux就该这么学>第八章 Iptables与Firewalld防火墙
Firewalld是RHEL7开始增加的防火墙测量略配置工具。
Iptables的使用
iptables在生产环境中仍然具有顽强的生命力。iptables把用于处理或者过滤流量的测量略条目称为规则,多条规则组成一个规则链,在设置的策略规则中,防火墙会从上至下依次读取配置的策略规则,如果没有规则与之匹配,则会去执行默认的规则。
规则链依据数据包处理位置的不同进行分类,分为:
在进行路由选择前处理数据包(PREROUTING)
处理流入的数据包(INPUT)
处理流出的数据包(OUTPUT)
处理转发的数据包(FORWARD)
在进行路由选择后处理数据包(POSTROUTING)
我们最常使用的是INPUT规则链,可以防御外部风险。
处理流量的动作包括:
ACCEPT(允许流量通过)
REJECT(拒绝流量通过):拒绝消息,并响应被拒绝的消息
LOG(记录日志信息)
DROP(拒绝流量通过):直接删除信息,不回复拒绝消息
iptables的常用参数:
>>>
# 查看规则链
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
ACCEPT udp -- anywhere anywhere udp dpt:bootps
ACCEPT tcp -- anywhere anywhere tcp dpt:bootps
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere 192.168.122.0/24 ctstate RELATED,ESTABLISHED
ACCEPT all -- 192.168.122.0/24 anywhere
ACCEPT all -- anywhere anywhere
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:bootpc
# 清空规则链
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -L
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
# 设置默认规则为丢弃,此时如果使用ping命令ping该机器的ip,则无响应
[root@localhost ~]# iptables -P INPUT DROP
[root@localhost ~]# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
# 给规则链中加入icmp规则
[root@localhost ~]# iptables -I INPUT -p icmp -j ACCEPT
[root@localhost ~]# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
# 删除INPUT规则链中的第一条规则
[root@localhost ~]# iptables -D INPUT 1
[root@localhost ~]# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@localhost ~]# iptables -P INPUT ACCEPT
[root@localhost ~]# iptables -L
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
# 仅允许指定网段的ip访问指定端口
[root@linuxprobe ~]# iptables -I INPUT -s 192.168.10.0/24 -p tcp --dport 22 -j ACCEPT
[root@linuxprobe ~]# iptables -A INPUT -p tcp --dport 22 -j REJECT
[root@linuxprobe ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh
REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
# 拒绝12345的所有请求
[root@linuxprobe ~]# iptables -I INPUT -p tcp --dport 12345 -j REJECT
[root@linuxprobe ~]# iptables -I INPUT -p udp --dport 12345 -j REJECT
[root@linuxprobe ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable
REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable
ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh
REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
# 拒绝1000-1024接口的请求
[root@linuxprobe ~]# iptables -A INPUT -p tcp --dport 1000:1024 -j REJECT
[root@linuxprobe ~]# iptables -A INPUT -p udp --dport 1000:1024 -j REJECT
# 拒绝端口范围
iptables -I INPUT tcp --dport 10000:20000 -j
# 保存现有的策略,centos 8
[root@linuxprobe ~]# iptables-save
# 保存现有的策略 centos 5/6/7
[root@linuxprobe ~]# service iptables save
iptables修改的文件的位置
/etc/sysconfig/iptables-config
## Firewalld
Firewall是RHEL7后新增的防火墙命令,它底层还是使用 iptables 对内核命令动态通信包过滤.拥有CLI/GUI两种使用方式.
Firewalld中默认设置有一些规则:
CLI
firewall-cmd 常用参数:
在使用firewalld-cmd命令时有两种模式:
默认为当前生效模式(-runtime)
如果想让永久生效需要使用 -permanent,这样当前不会生效,在重启或者reload之后永久生效.
# 查看firewalld服务当前使用的区域
[root@localhost ~]# firewall-cmd --get-default-zone
public
# 查看网卡信息
[root@localhost ~]# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:d0:e4:db txqueuelen 1000 (Ethernet)
RX packets 1 bytes 247 (247.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 108 bytes 9188 (8.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 108 bytes 9188 (8.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:62:1d:54 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 查看网卡的默认信息(网络关闭时)
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens160
no zone
# 查看网卡的默认信息(网络开启后)
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens160
public
# 设置区域为home
[root@localhost ~]# firewall-cmd --permanent --zone=home --change-interface=ens160
The interface is under control of NetworkManager, setting zone to 'home'.
success
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens160
home
# 获取所有网卡的默认区域,优先级是最低的
[root@localhost ~]# firewall-cmd --get-default-zone
public
# 修改网卡的默认区域
[root@localhost ~]# firewall-cmd --set-default-zone=external
success
[root@localhost ~]# firewall-cmd --get-default-zone
external
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens160
home
# 启用紧急模式(会断网)
[root@localhost ~]# firewall-cmd --panic-on
success
# 关闭紧急模式
[root@localhost ~]# firewall-cmd --panic-off
success
# 查询区域中服务的状态
[root@localhost ~]# firewall-cmd --zone=public --query-service=ssh
yes
[root@localhost ~]# firewall-cmd --zone=public --query-service=https
no
# 修改区域中服务器的状态,并reload
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@localhost ~]# firewall-cmd --zone=public --query-service=https
no
[root@localhost ~]# firewall-cmd --reload
[root@localhost ~]# firewall-cmd --zone=public --query-service=https
yes
# 设置端口的规则
[root@localhost ~]# firewall-cmd --zone=public --add-port=8080-8081/tcp
success
[root@localhost ~]# firewall-cmd --zone=public --list-ports
8080-8081/tcp
# 端口转发
[root@localhost ~]# firewall-cmd --permanent --zone={区域名称} --add-forward-port=port={来源端口}:proto={使用的协议}:toport={目标端口}:toaddr={目标地址}
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-forward-port=port=888:proto=tcp:toport=22:toaddr=127.0.0.1
success
# 富规则
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.10.0/24" service name="ssh" reject"
success
查看系统中运行的服务及端口号:
cat /etc/services
查看目前系统使用的端口号:
netstat -an
## firewall-config
安装
dnf install firewall-config
License:
CC BY 4.0