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
相关推荐
@t.t.16 分钟前
Docker容器资源控制--CGroup
linux·运维·docker·容器·云计算
像风一样自由202019 分钟前
浏览器自动化工具:Selenium 和 Playwright
运维·selenium·自动化
林九生22 分钟前
【运维】Ubuntu apt 更新失败?Temporary failure resolving ‘cn.archive.ubuntu.com‘ 问题
运维·ubuntu·postgresql
不想当程序猿_30 分钟前
Centos 7系统 宝塔部署Tomcat项目(保姆级教程)
linux·redis·centos·tomcat·minio·宝塔
吴声子夜歌32 分钟前
Linux运维——Vim基础
linux·运维·vim
照海19Gin1 小时前
HCIA-Datacom 高阶:VLAN、VLANIF 与静态路由综合实验
网络·智能路由器
Zz_waiting.1 小时前
网络原理 - 11(HTTP/HTTPS - 2 - 请求)
运维·网络·网络协议·mysql·http·https·抓包
davenian2 小时前
< 自用文 rclone > 在 Ubuntu 24 访问 Google Drive 网络内容
linux·ubuntu·rclone
单车少年ing2 小时前
linux两个特殊的宏 _RET_IP_ 和_THIS_IP_ 实现
linux·arm