网站首页 > 技术教程 正文
使用过Linux系统的人对iptables一定不陌生,它是Linux从2.4.x版本内核开始,系统自带的防火墙。如今Linux内核已经更新至5.11.x版本,Linux的防火墙在iptables基础上泛生出UFW和Firewalld,并且在一些发行版中已经替代iptables。
Netfilter/Iptables
Netfilter/Iptables 是Linux系统自带的防火墙,Iptables管理规则,Netfilter是规则的执行者,它们一起组成Linux下包过滤防火墙。
iptables内置4个表,即filter表、nat表、mangle表和raw表,分别用于实现包过滤,网络地址转换、包重构(修改)和数据跟踪处理。每个表都会有相应的链。下图是iptables中的四表五链:
链(chains)是数据包传播的路径。每一条链中可以有一条或多条规则,当一个数据包到达一个链时,iptables就会从链中第一条规则开始检查,看该数据包是否满足规则所定义的条件。如果满足,系统就会根据该条规则所定义的方法处理该数据包;否则iptables将继续检查下一条规则。如果该数据包不符合链中任一条规则,iptables就会根据该链预先定义的默认策略来处理数据包。
关于iptables的原理这篇文章讲得很清楚:
https://blog.csdn.net/tennysonsky/article/details/44596515
在Linux中,可以使用如下命令启用或关闭iptables
// 启动iptables
systemctl start iptables
// 停止iptables
systemctl stop iptables
以下是常用命令:
// 列出 INPUT 表中规则
iptables -L INPUT
// 允许3306端口TCP协议访问
iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
// 允许IP为xxx.xxx.xxx.xxx通过udp访问本地500端口
iptables -I INPUT -p udp --dport 500 -s xxx.xxx.xxx.xxx -j ACCEPT
// -I参数是将规则插入表开头,-A是把规则添加到表未尾,优先级最低,可做为默认规则
// 当不符合前面规则后拒绝所有请求
iptables -A INPUT -p tcp -j REJECT
// 删除INPUT表中第1条规则
iptables -D INPUT 1
// 清空规则列表
iptables -F
iptables还能做基于内核的包转发。关于iptables用法网上文章太多了,就不多写。
需要注意的是,iptables规则虽然能即时生效,但并未保存。开机后会丢失。
RedHat系执行保存:
service iptables save
Debian系执行 iptables-save 列出所有规则,输出到文件中保存。系统启动时用 iptables-restore 恢复。
// 保存
iptables-save > /etc/iptables.conf
// 恢复
iptables-restore < /etc/iptables.conf
UFW 和 Firewalld
iptables很强大也很复杂,于是便有UFW和Firewalld。它们的命令简单清晰很多,底层都是调用iptables。
- UFW
UFW是Ubuntu下防火墙:
// 启动ufw
systemctl start ufw
// 停用ufw
systemctl stop ufw
以下是常用命令:
// 查询ufw开启状态,打印规则
ufw status
// 允许80端口访问
ufw allow 80
// 拒绝8000端口访问
ufw deny 9000
// 拒绝ip为xxx.xxx.xxx.xxx访问
ufw deny from xxx.xxx.xxx.xxx
// 允许通过tcp协议,9000-9002访问
ufw allow 9000:9002/tcp
// 删除规则
ufw delete allow http
- Firewalld
Firewalld是Fedora/CentOS 8之后版本自带的防火墙:
// 启动firewalld
systemctl start firewalld
// 停用firewalld
systemctl stop firewalld
以下是常用命令:
// 允许tcp协议8161端口访问,--zone(作用域),--permanent(永久生效)
firewall-cmd --zone=public --add-port=8161/tcp --permanent
// 重新加载规则
firewall-cmd --reload
// 将80端口的流量转发至8080
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080
// 将80端口的流量转发至192.168.0.1
firewall-cmd --add-forward-port=proto=80:proto=tcp:toaddr=192.168.1.0.1
// 将80端口的流量转发至192.168.0.1的8080端口
firewall-cmd --add-forward-port=proto=80:proto=tcp:toaddr=192.168.0.1:toport=8080
Firewalld也有图形界面:
除非注明,否则均为"攻城狮·正"原创文章,请注明出处。
本文链接:
https://engr-z.com/402.html
猜你喜欢
- 2025-06-23 Linux 操作系统不需要防病毒软件和防火墙的几个原因
- 2025-06-23 Linux防火墙——iptables原理介绍
- 2025-06-23 防火墙/路由器Linux发行版Devil-Linux
- 2025-06-23 五问 Linux 网络防火墙(四):nftables 的高效框架与数据结构
- 2025-06-23 linux防火墙过滤技术iptables的原理及操作命令详解
- 2025-06-23 Linux,操作系统,防火墙,Netfilter,命令及操作
- 2025-06-23 LINUX IPTABLES 防火墙配置(linux防火墙怎么配置)
- 2025-06-23 Linux安全之网络防火墙(linux防火墙在哪)
- 2025-06-23 linux系统相关防火墙iptables命令操作
- 2025-06-23 linux 防火墙基本命令(linux防火墙主要功能)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)