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
相关推荐
江苏思维驱动智能研究院有限公司12 分钟前
Sophos 网络安全:全球领先的自适应安全解决方案提供商
网络·安全·web安全
开开心心就好17 分钟前
免费PDF处理软件,支持多种操作
运维·服务器·前端·spring boot·智能手机·pdf·电脑
面朝大海,春不暖,花不开37 分钟前
Java网络编程:TCP/UDP套接字通信详解
java·网络·tcp/ip
ChicagoTypewriter43 分钟前
计算机网络中的常用表项梳理
网络·计算机网络·智能路由器
m0_653031361 小时前
腾讯云认证考试报名 - TDSQL数据库交付运维专家(TCCE PostgreSQL版)
运维·数据库·腾讯云
IC 见路不走2 小时前
LeetCode 第91题:解码方法
linux·运维·服务器
没有名字的小羊2 小时前
8.Docker镜像讲解
运维·docker·容器·tomcat
翻滚吧键盘2 小时前
查看linux中steam游戏的兼容性
linux·运维·游戏
小能喵2 小时前
Kali Linux Wifi 伪造热点
linux·安全·kali·kali linux
Code季风2 小时前
深入理解微服务中的服务注册与发现(Consul)
java·运维·微服务·zookeeper·架构·go·consul