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
相关推荐
技术小齐16 分钟前
网络运维学习笔记 022 HCIA-Datacom新增知识点03园区网典型组网架构及案例实战
运维·网络·学习
致奋斗的我们25 分钟前
HAProxy介绍与编译安装
linux·汇编·数据库·mysql·青少年编程·haproxy·openeurler
Java潘老师33 分钟前
Automa 浏览器自动化编排 实现自动化浏览器操作
运维·自动化
waves浪游39 分钟前
Linux基本指令(上)
linux·运维·服务器
黄卷青灯771 小时前
抓包工具 wireshark
网络·测试工具·wireshark
不确定性确定你我1 小时前
`sh` 与 `bash` 的区别详解
linux·bash·运维开发
是北欢吆2 小时前
QQ登录测试用例报告
运维·服务器·测试用例
LUCIAZZZ2 小时前
Https解决了Http的哪些问题
java·网络·网络协议·spring·http·rpc·https
、BeYourself2 小时前
计算机网络常考大题
网络
ZachOn1y2 小时前
计算机网络:应用层 —— 文件传送协议 FTP
网络·计算机网络·应用层·文件传送协议ftp·考研408·知识积累