第六章 Iptables与Firewalld防火墙
一、Iptables
1、策略与规则链
(1)、防火墙策略规则的设置
一种是“通”即放行,另一种是“堵”即阻止。
当防火墙的默认策略为拒绝时(堵),就要设置允许规则通,否则谁都进不来。
如果防火墙的默认策略为允许时,就要设置拒绝规则,否则谁都能进来,防火墙也就失去了防范的作用。
(2)、数据包处理位置
iptables服务把用于处理或过滤流量的策略条目称之为规则,多条规则可以组成一个规则链,而规则链则依据数据包处理位置的不同进行分类,具体如下:
在进行路由选择前处理数据包(PREROUTING)
处理流入的数据包(INPUT)
处理流出的数据包(OUTPUT)
处理转发的数据包(FORWARD)
在进行路由选择后处理数据包(POSTROUTING)
2、基本的命令参数
(1)、iptables中常用的参数以及作用
| 参数 | 作用 |
|---|---|
| -P | 设置默认策略 |
| -F | 清空规则链 |
| -L | 查看规则链 |
| -A | 在规则链的末尾加入新规则 |
| -l num | 在规则链的头部加入新规则 |
| -D num | 删除某一条规则 |
| -s | 匹配来源地址IP/MASK,加感叹号表示出这个IP外 |
| -d | 匹配目标地址 |
| -i 网卡名称 | 匹配从这块网卡流入的数据 |
| -o 网卡名称 | 匹配从这块网卡流出的数据 |
| -p | 匹配协议 |
| –dport num | 匹配目标端口号 |
| –sport num | 匹配来源端口号 |
(2)、查看已有的防火墙规则链
[root@centos ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
LIBVIRT_INP all -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
LIBVIRT_FWX all -- anywhere anywhere
LIBVIRT_FWI all -- anywhere anywhere
LIBVIRT_FWO all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
LIBVIRT_OUT all -- anywhere anywhere
Chain LIBVIRT_INP (1 references)
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 LIBVIRT_OUT (1 references)
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:bootpc
ACCEPT tcp -- anywhere anywhere tcp dpt:bootpc
Chain LIBVIRT_FWO (1 references)
target prot opt source destination
ACCEPT all -- 192.168.122.0/24 anywhere
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
Chain LIBVIRT_FWI (1 references)
target prot opt source destination
ACCEPT all -- anywhere 192.168.122.0/24 ctstate RELATED,ESTABLISHED
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
Chain LIBVIRT_FWX (1 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere
(3)、清空已有的防火墙规则链并查看
//清空规则链
[root@centos ~]# iptables -F
//查看规则链
[root@centos ~]# 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
Chain LIBVIRT_INP (0 references)
target prot opt source destination
Chain LIBVIRT_OUT (0 references)
target prot opt source destination
Chain LIBVIRT_FWO (0 references)
target prot opt source destination
Chain LIBVIRT_FWI (0 references)
target prot opt source destination
Chain LIBVIRT_FWX (0 references)
target prot opt source destination
(4)把INPUT规则链的默认策略设置为拒绝并查看
//更改默认策略
[root@centos ~]# iptables -P INPUT DROP
//查看规则链
[root@centos ~]# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
(5)、向INPUT链中添加允许ICMP流量进入的策略规则
//设置策略规则
[root@centos ~]# iptables -I INPUT -p icmp -j ACCEPT
(6)、删除INPUT规则链中刚刚加入的那条策略,并把默认策略设置为允许
//删除策略
[root@centos ~]# iptables -D INPUT 1
//更改默认策略
[root@centos ~]# iptables -P INPUT ACCEPT
(7)、将INPUT规则链设置为只允许指定网段的主机访问本机的22端口,拒绝来自其他所有主机的流量
//设置指定网段访问22端口
[root@centos ~]# iptables -I INPUT -s 192.168.2.0/24 -p tcp --dport 22 -j ACCEPT
//拒绝其他所有主机流量
[root@centos ~]# iptables -A INPUT -p tcp --dport 22 -j REJECT
(8)、防火请策略永久生效
//新版本
[root@centos ~]# iptables-save
//旧版本
[root@centos ~]# service iptables save
二、Firewalld
1、firewalld中常用的区域名称及策略规则
| 区域 | 默认规则策略 |
|---|---|
| trusted | 允许所有的数据包 |
| home | 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、mdns、ipp-client、amba-client、dhcpv6-client服务相关,则允许流量 |
| intermal | 等同于home区域 |
| work | 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、ipp-client、dhcpv6-client服务相关,则允许流量 |
| public | 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、dhcpv6-client服务相关,则允许流量 |
| external | 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh服务相关,则允许流量 |
| dmz | 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh服务相关,则允许流量 |
| block | 拒绝流入的流量,除非与流出的流量相关 |
| drop | 拒绝流入的流量,除非与流出的流量相关 |
2、终端管理工具
(1)、firewall-cmd命令中使用的参数以及作用
| 参数 | 作用 |
|---|---|
| –get-default-zone | 查询默认的区域名称 |
| –set-default-zone=<区域名称> | 设置默认的区域,使其永久生效 |
| –get-zones | 显示可用的区域 |
| –get-services | 显示预先定义的服务 |
| –get-active-zones | 显示当前正在使用的区域与网卡名称 |
| –add-source= | 将源自此IP或子网的流量导向指定的区域 |
| –remove-source= | 不再将源自此IP或子网的流量导向某个指定区域 |
| –add-interface=<网卡名称> | 将源自该网卡的所有流量都导向某个指定区域 |
| –change-interface=<网卡名称> | 将某个网卡与区域进行关联 |
| –list-all | 显示当前区域的网卡配置参数、资源、端口以及服务等信息 |
| –list-all-zones | 显示所有区域的网卡配置参数、资源、端口以及服务等信息 |
| –add-service=<服务名> | 设置默认区域允许该服务的流量 |
| –add-port=<端口号/协议> | 设置默认区域允许该端口的流量 |
| –remove-service=<服务名> | 设置默认区域不再允许该服务的流量 |
| –remove-port=<端口号/协议> | 设置默认区域不再允许该端口的流量 |
| –reload | 让永久生效的配置规则立即生效,并覆盖当前的配置规则 |
| –panic-on | 开启应急状况模式 |
| –panic-off | 关闭应急状况模式 |
(2)、生效模式
Runtime:当前立即生效,重启后失效
Permanent:当前不生效,重启后生效
(3)、查看当前所使用的区域
[root@centos ~]# firewall-cmd --get-default-zone
public
(4)、查询指定网卡在firewalld服务中绑定的区域
[root@centos ~]# firewall-cmd --get-zone-of-interface=ens160
public
(5)、更改网卡默认区域为external,并在系统重启后生效
[root@centos ~]# firewall-cmd --permanent --zone=external --change-interface=ens160
The interface is under control of NetworkManager, setting zone to 'external'.
success
[root@centos ~]# firewall-cmd --get-zone-of-interface=ens160 --permanent
external
(6)、把firewalld服务的默认区域设置为public
[root@centos ~]# firewall-cmd --set-default-zone=public
Warning: ZONE_ALREADY_SET: public
success
[root@centos ~]# firewall-cmd --get-default-zone
public
[root@centos ~]# firewall-cmd --get-zone-of-interface=ens160
external
(7)、启动和关闭应急状况模式
//启动应急状况模式
[root@centos ~]# firewall-cmd --panic-on
success
//关闭应急状况模式
[root@centos ~]# firewall-cmd --panic-off
success
(8)、查询ssh和https协议的流量是否允许放行
//查询ssh
[root@centos ~]# firewall-cmd --zone=public --query-service=ssh
yes
//查询https
[root@centos ~]# firewall-cmd --zone=public --query-service=https
no
(9)、把https协议的流量设置为永久允许放行并立即生效
[root@centos ~]# firewall-cmd --zone=public --add-service=https --permanent
success
[root@centos ~]# firewall-cmd --reload
success
(10)、把https协议的流量设置为永久失效放行并立即生效
[root@centos ~]# firewall-cmd --zone=public --remove-service=https --permanent
success
[root@centos ~]# firewall-cmd --reload
success
(11)、将8080和8081端口的流量策略设置为允许,仅限当前生效
[root@centos ~]# firewall-cmd --zone=public --add-port=8080-8081/tcp
success
(12)、把原本访问本机888端口的流量转发到22端口,要求当前和长期有效
语法格式:firewall-cmd ---zone=<区域> --add-forward-port=port=<源端口号>:proto=<协议>:toport=<目标端口号>:toaddr=<目标IP地址>
[root@centos ~]# firewall-cmd --zone=public --add-forward-port=port=888:proto=tcp:toport=22:toaddr=192.168.2.22 --permanent
success
[root@centos ~]# firewall-cmd --reload
success
(13)、富规则的设置:拒绝192.168.2.0/24网段内的所有用户访问本机的ssh服务
[root@centos ~]# firewall-cmd --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.2.0/24" service name="ssh" reject" --permanent
success
[root@centos ~]# firewall-cmd --reload
success
3、图形管理工具
(1)、安装firewall-config
[root@centos ~]# yum install firewall-config
CentOS-8.5.2111 - Base - mirrors.ali 24 kB/s | 3.9 kB 00:00
CentOS-8.5.2111 - Extras - mirrors.a 25 kB/s | 1.5 kB 00:00
CentOS-8.5.2111 - AppStream - mirror 53 kB/s | 4.3 kB 00:00
依赖关系解决。
=====================================================================
软件包 架构 版本 仓库 大小
=====================================================================
安装:
firewall-config noarch 0.9.3-7.el8 AppStream 160 k
事务概要
=====================================================================
安装 1 软件包
总下载:160 k
安装大小:1.1 M
确定吗?[y/N]: y
下载软件包:
firewall-config-0.9.3-7.el8.noarch.r 648 kB/s | 160 kB 00:00
---------------------------------------------------------------------
总计 643 kB/s | 160 kB 00:00
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
准备中 : 1/1
安装 : firewall-config-0.9.3-7.el8.noarch 1/1
运行脚本: firewall-config-0.9.3-7.el8.noarch 1/1
验证 : firewall-config-0.9.3-7.el8.noarch 1/1
已安装:
firewall-config-0.9.3-7.el8.noarch
完毕!
(2)、允许放行当前区域中请求http服务的流量但仅限当前生效
第一步:选择运行时,public区域,在服务中选择http服务
第二步:在端口中添加端口范围8080-8088,协议为tcp
第三步:在选项菜单栏中点击重载防火墙



(3)、启动SNAT技术
第一步:选择运行时,public区域,在伪装栏中勾选伪装区域

(4)、将本机888端口流量转发到22端口,要求当前和长期均有效
第一步:选择永久有效,public区域,在端口转发一栏中配置端口信息
第二步:在选项菜单栏中点击重载防火墙


(5)、设置让192.168.2.20主机访问本机1234端口号,要求永久有效
第一步:选择永久有效,public区域,在富规则栏中配置信息
第二步:在选项菜单栏中点击重载防火墙

(6)、网卡与防火墙策略区域绑定
第一步:选择永久有效,public区域,在网卡栏中绑定网卡
第二步:在选项菜单栏中点击重载防火墙

三、服务的访问控制列表
1、TCP Wrappers介绍
TCP Wrappers是RHEL 6/7系统中默认启用的一款流量监控程序,它能够根据来访主机的地址与本机的目标服务程序作出允许或拒绝的操作,当前8版本中已经被Firewalld正是替代。换句话说,Linux系统中其实有两个层面的防火墙,第一种是基于TCP/IP协议的流量过滤工具,而TCP Wrappers服务则是能允许或禁止Linux系统提供服务的防火墙,从而在更高层面保护了Linux系统的安全运行。
TCP Wrappers服务的防火墙策略由两个控制列表文件所控制,用户可以编辑允许空值列表文件来放行对服务的请求流量,也可以编辑拒绝控制列表文件来组织对服务的请求流量。控制列表文件修改后会立即生效,系统将会先检查允许控制列表文件(/etc/hosts.allow),如果匹配到相应的允许策略则放行流量;如果没有匹配,则去进一步匹配拒绝控制列表文件(/etc/hosts.deny),若找到匹配项则拒绝该流量。如果这两个文件全都没有匹配到,则默认放行流量。
2、TCP Wrappers服务的控制列表文件中常用的参数
| 客户端类型 | 示例 | 满足示例的客户端列表 |
|---|---|---|
| 单一主机 | 192.168.0.10 | IP段为192.168.0.10的主机 |
| 指定网段 | 192.168.0 | IP段为192.168.0.0/24的主机 |
| 指定网段 | 192.168.0.0/255.255.255…0 | IP段为192.168.0.0/24的主机 |
| 指定DNS后缀 | .xxx.com | 所有DNS后缀为.xxx.com的主机 |
| 指定主机名称 | www.xxx.com | 主机名称为www.xxx.com的主机 |
| 指定所有客户端 | ALL | 所有主机全部包括在内 |
3、遵循原则
1、编写拒绝策略规则时,填写的是服务名称,而非协议名称。
2、建议先编写拒绝策略规则,在编写允许策略规则,以便直观地看到相应的效果。
4、编写拒绝策略规则文件,禁止访问本机sshd服务的所有流量
[root@centos ~]# vim /etc/hosts.deny
#
# hosts.deny This file contains access rules which are used to
# deny connections to network service that either use
# the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# The rules in this file can also be set up in
# /etc/hosts.allow with a 'deny' option instead
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
sshd:*
5、编写允许策略规则文件,放行192.168.2.0/24网段,访问本机sshd服务的所有流量
[root@centos ~]# vim /etc/hosts.allow
#
# hosts.allow This file contains access rules which are used to
# allow or deny connections to network service that either use
# the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
sshd:192.168.2.0/255.255.255.0
四、Cockpit驾驶舱管理工具
1、启动Cockpit服务程序并设置开机自启动
[root@centos ~]# systemctl start cockpit
[root@centos ~]# systemctl enable cockpit.socket
Created symlink /etc/systemd/system/sockets.target.wants/cockpit.socket → /usr/lib/systemd/system/cockpit.socket.
2、访问网站
URL:https://192.168.2.22:9090/
账号:root
密码:系统密码
四、Cockpit驾驶舱管理工具
1、启动Cockpit服务程序并设置开机自启动
[root@centos ~]# systemctl start cockpit
[root@centos ~]# systemctl enable cockpit.socket
Created symlink /etc/systemd/system/sockets.target.wants/cockpit.socket → /usr/lib/systemd/system/cockpit.socket.
2、访问网站
URL:https://192.168.2.22:9090/
账号:root
密码:系统密码




















