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
相关推荐
Wy_编程1 小时前
Linux-文本搜索工具grep
linux·运维·服务器
qq998991 小时前
AAA服务器技术
运维·服务器
xujiangyan_1 小时前
linux的sysctl系统以及systemd系统。
linux·服务器·网络
Lovyk1 小时前
Linux Shell 常用操作与脚本示例详解
linux·运维·服务器
iCan_qi2 小时前
【Mac】【Minecraft】关于如何在Mac上搭建基岩版MC服务器的方法
运维·服务器·macos·minecraft
ezreal_pan4 小时前
Kubernetes 负载均衡现象解析:为何同一批次请求集中于单个 Pod
运维·云原生·k8s·traefik
朱皮皮呀4 小时前
Spring Cloud——服务注册与服务发现原理与实现
运维·spring cloud·eureka·服务发现·php
吱吱企业安全通讯软件4 小时前
吱吱企业通讯软件保证内部通讯安全,搭建数字安全体系
大数据·网络·人工智能·安全·信息与通信·吱吱办公通讯
yuanpan5 小时前
ubuntu系统上的conda虚拟环境导出方便下次安装
linux·ubuntu·conda
云边云科技6 小时前
零售行业新店网络零接触部署场景下,如何选择SDWAN
运维·服务器·网络·人工智能·安全·边缘计算·零售