文章目录
-
- 第七章 Iptables与Firewalld防火墙
-
-
- 一、Iptables
- 二、Firewalld
-
- 1、firewalld中常用的区域名称及策略规则
- 2、终端管理工具
-
- (1)、firewall-cmd命令中使用的参数以及作用
- (2)、生效模式
- (3)、查看当前所使用的区域
- (4)、查询指定网卡在firewalld服务中绑定的区域
- (5)、更改网卡默认区域为external,并在系统重启后生效
- (6)、把firewalld服务的默认区域设置为public
- (7)、启动和关闭应急状况模式
- (8)、查询ssh和https协议的流量是否允许放行
- (9)、把https协议的流量设置为永久允许放行并立即生效
- (10)、把https协议的流量设置为永久失效放行并立即生效
- (11)、将8080和8081端口的流量策略设置为允许,仅限当前生效
- (12)、把原本访问本机888端口的流量转发到22端口,要求当前和长期有效
- (13)、富规则的设置:拒绝192.168.2.0/24网段内的所有用户访问本机的ssh服务
- 3、图形管理工具
- 三、服务的访问控制列表
- 四、Cockpit驾驶舱管理工具
-
第七章 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 <