Linux防火墙入门:学会使用firewalld和iptables

1 防火墙

1.1 防火墙介绍

防火墙就是过滤的表格,被记录在表格中的信息,才允许通过访问。

1.2 两种火墙策略

iptables

bash 复制代码
dnf install iptables-services -y
systemctl disable --now firewalld 
systemctl mask firewalld 
systemctl enable --now iptables


firewalld

bash 复制代码
dnf install firewalld -y
systemctl disable --now iptables
systemctl mask iptables
systemctl enable --now firewalld

需要注意的是在对一个策略加锁后,重新启用的时候要进行解锁

bash 复制代码
systemctl unmask iptables 

2 iptables

2.1 iptables策略的保存

临时清空

bash 复制代码
iptables -F # 该方法重启服务后会重新加载策略

永久清空

bash 复制代码
/etc/sysconfig/iptables # 火墙策略文件永久保存在这里
bash 复制代码
iptables -F
service iptables save

2.2 iptables的使用

2.2.1 查看-L

bash 复制代码
iptables -nL
iptables -t nat -nL # -t指定表的名称
iptables -t mangle -nL # -n不做解析


2.2.2 添加-A

2.2.3 插入-I

2.2.4 修改-R

2.2.5 删除-D

2.2.6 新建链-N、更改链名称-E、删除链-X



2.2.7 更改默认规则-P

bash 复制代码
-p ##协议
--dport ##目的地端口
-s ##来源
-j ##动作
ACCEPT ##允许
DROP ##丢弃
REJECT ##拒绝
SNAT ##源地址转换
DNAT ##目的地地址转换

2.3 iptables的优化

bash 复制代码
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -m state --state NEW -i lo -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -m state --state NEW ! -s 192.168.0.10 -p tcp --dport 22 -j ACCEPT
ptables -A INPUT -m state --state NEW -j REJECT
service iptables save

2.4 iptables中NAT表的使用

bash 复制代码
snat
iptable -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 192.168.0.20

dnat
iptables -t nat -A PREROUTING -i ens160 -j DNAT --to-dest 172.25.254.30

3 firewalld

3.1 开启firewalld

bash 复制代码
systemctl disable --now iptables
systemctl mask iptables 
systemctl unmask firewalld
systemctl enable --now firewalld

3.2 关于firewalld的域

trusted ##接受所有的网络连接

home ##用于家庭网络,允许接受ssh mdns ipp-client samba-client dhcp-client

work ##工作网络 ssh ipp-client dhcp-client

public ##公共网络 ssh dhcp-client

dmz ##军级网络 ssh

block ##拒绝所有

drop ##丢弃 所有数据全部丢弃无任何回复

internal ##内部网络 ssh mdns ipp-client samba-client dhcp-client

external ##ipv4网络地址伪装转发 sshd

3.3 关于firewalld的设定原理及数据存储

/etc/firewalld ##火墙配置目录

/lib/firewalld ##火墙模块目录

3.4 firewalld的管理命令

bash 复制代码
firewall-cmd --state ##查看火墙状态
firewall-cmd --get-active-zones ##查看当前火墙中生效的域
firewall-cmd --get-default-zone ##查看默认域
firewall-cmd --list-all ##查看默认域中的火墙策略
firewall-cmd --list-all --zone=work ##查看指定域的火墙策略
firewall-cmd --set-default-zone=trusted ##设定默认域

3.4.1 基本规则











3.4.2 高级规则

bash 复制代码
firewall-cmd --direct --get-all-rules ##查看高级规则
firewall-cmd --direct --add-rule ipv4 filter INPUT 0 ! -s 172.25.254.250 -p tcp --dport 22 -j
REJECT

3.4.3 SNAT

bash 复制代码
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload



3.4.4 DNAT

bash 复制代码
firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=172.25.254.30
firewall-cmd --reloa
相关推荐
️️(^~^)1 分钟前
静态路由综合配置实验报告
服务器·网络·计算机网络·智能路由器
江公望1 小时前
Linux kernel regcache_cache_only()函数详解
linux
Andy杨1 小时前
20250710-2-Kubernetes 集群部署、配置和验证-网络组件存在的意义?_笔记
网络·笔记·kubernetes
沫儿笙1 小时前
弧焊机器人气体全方位节能指南
网络·人工智能·机器人
liuyunluoxiao1 小时前
基于生产者消费者模型的线程池【Linux操作系统】
linux
CSJ200203141 小时前
LUMP+NFS架构的Discuz论坛部署
运维·架构
不知疲倦的仄仄2 小时前
2025最新版Docker讲解/面试/命令/容器化技术
运维·docker·容器
程序员黄老师2 小时前
Ubuntu 24.04上安装 Intelligent Pinyin 中文输入法
linux·运维·ubuntu
HONG_YANG3 小时前
ERPNext 介绍、安装和维护建议
linux
HONG_YANG3 小时前
ERPNext 搭建教程:Linux 一键部署与维护
linux