iptables常用命令总结

1.iptables 是什么

在Linux中,iptables就是一款强大而灵活的防火墙工具,它为系统管理员提供了广泛的配置选项,可以有效地控制数据包的流动,实现网络访问的控制及安全性增强。

iptables 使用三个不同的链来允许或阻止流量:输入(input)、输出(output)和转发(forward)

输入(input) ------ 此链用于控制传入连接的行为

输出(output) ------ 此链用于传出连接

转发(forward) ------ 这条链用于传入的连接,这些连接实际上不是在本地传递的,比如路由和 NAT

2.iptables 安装

CentOS 7 上默认安装了 firewalld,使用 iptables 需要先关闭并禁用 firewalld

bash 复制代码
systemctl stop firewalld
systemctl disable firewalld

使用rpm包安装iptables,准备好安装包:

bash 复制代码
iptables-1.4.21-34.el7.x86_64.rpm
iptables-services-1.4.21-34.el7.x86_64.rpm

执行命令安装:

bash 复制代码
rpm -ivh --force ./*.rpm

查看版本:

bash 复制代码
iptables --version

iptables 服务管理

bash 复制代码
systemctl status iptables  # 查看服务状态
systemctl enable iptables  # 启用服务
systemctl disable iptables # 禁用服务
systemctl start iptables   # 启动服务
systemctl stop iptables    # 关闭服务
systemctl restart iptables # 重启服务

3.iptables命令及参数

bash 复制代码
基本语法:

$ iptables(选项)(参数)
-P	设置默认策略:
iptables -P INPUT (DROP
-F	清空规则链
-L	查看规则链
-A	在规则链的末尾加入新规则
-I	num 在规则链的头部加入新规则
-D	num 删除某一条规则
-s	匹配来源地址 IP/MASK
加叹号"!"表示除这个 IP 外
-d	匹配目标地址
-i	网卡名称 匹配从这块网卡流入的数据
-o	网卡名称 匹配从这块网卡流出的数据
-p	匹配协议,如 tcp,udp,icmp
--dport num	匹配目标端口号
--sport num	匹配来源端口号

$ iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作
# 语法规则
iptables [-t 表名] 选项 [链名] [条件] [-j 控制类型]
# 例如下列两条
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.0/24 -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.100:80

4.iptables 应用实例

(1)清空当前的所有规则和计数

bash 复制代码
iptables -F  # 清空所有的防火墙规则(手动配的,默认没有)
iptables -X  # 删除用户自定义的空链
iptables -Z  # 清空计数

(2)配置允许 ssh 端口连接

bash 复制代码
# 22 为你的 ssh 端口, -s 192.168.1.0/24 表示允许这个网段的机器来连接。-j ACCEPT 表示接受这样的请求
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT

(3)配置白名单

bash 复制代码
# 允许机房内网机器可以访问
iptables -A INPUT -p all -s 192.168.1.0/24 -j ACCEPT 
# 允许机房内网机器可以访问
iptables -A INPUT -p all -s 192.168.140.0/24 -j ACCEPT 
# 允许 183.121.3.7 访问本机的3380端口
iptables -A INPUT -p tcp -s 183.121.3.7 --dport 3380 -j ACCEPT

(4)开启相应的服务端口

bash 复制代码
# 开启 80 端口,因为web对外都是这个端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# 允许被 ping
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
# 已经建立的连接得让它进来
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

(5)列出已设置的规则

bash 复制代码
$ iptables -L [-t 表名][链名]
四个表名 raw,nat,filter,mangle
五个规则链名 INPUT、OUTPUT、FORWARD、PREROUTING、POSTROUTING
filter 表包含INPUT、OUTPUT、FORWARD三个规则链
# 列出 nat 上面的所有规则
$ iptables -L -t nat                
# -t 参数指定,必须是 raw, nat,filter,mangle 中的一个
# 规则带编号
$ iptables -L -t nat  --line-numbers
$ iptables -L INPUT
# 查看,这个列表看起来更详细
$ iptables -L -nv

(6)删除已添加的规则

bash 复制代码
# 添加一条规则
iptables -A INPUT -s 192.168.1.5 -j DROP

# 将所有 iptables 以序号标记显示,执行:
iptables -L -n --line-numbers

# 比如要删除 INPUT 里序号为 8 的规则,执行:
iptables -D INPUT 8

(7)开放指定的端口

bash 复制代码
# 允许本地回环接口(即运行本机访问本机)
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
# 允许已建立的或相关连的通行
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许所有本机向外的访问
iptables -A OUTPUT -j ACCEPT
# 允许访问22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 禁止访问22端口:iptables -A INPUT -p tcp --dport 22 -j DROP
# 怎么开就怎么关 ACCEPT --> DROP
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# 允许ftp服务的21端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
# 允许FTP服务的20端口
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
# 禁止其他未允许的规则访问
iptables -A INPUT -j reject
# 禁止其他未允许的规则访问
iptables -A FORWARD -j REJECT

(8)查看已添加的规则

bash 复制代码
$ iptables -L -n -v
Chain INPUT (policy DROP 48106 packets, 2690K bytes)
 pkts bytes target     prot opt in     out     source               destination
 5075  589K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
 191K   90M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22
1499K  133M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:80
4364K 6351M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
 6256  327K ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 3382K packets, 1819M bytes)
 pkts bytes target     prot opt in     out     source               destination
 5075  589K ACCEPT     all  --  *      lo      0.0.0.0/0            0.0.0.0/0
相关推荐
蓝易云1 小时前
Qt框架中connect()方法的ConnectionType参数使用说明 点击改变文章字体大小
linux·前端·后端
花落已飘1 小时前
多线程 vs 异步
linux·网络·系统架构
PanZonghui2 小时前
Centos项目部署之Nginx部署项目
linux·nginx
码出钞能力2 小时前
linux内核模块的查看
linux·运维·服务器
星辰云-3 小时前
# Linux Centos系统硬盘分区扩容
linux·运维·centos·磁盘扩容
聽雨2373 小时前
02每日简报20250704
linux·科技·金融·生活·社交电子·娱乐·媒体
Maki Winster4 小时前
Peek-Ubuntu上Gif录制工具-24.04LTS可装
linux·ubuntu·peek
Maki Winster5 小时前
在 Ubuntu 下配置 oh-my-posh —— 普通用户 + root 各自使用独立主题(共享可执行)
linux·运维·ubuntu
守望时空335 小时前
Linux下KDE桌面创建自定义右键菜单
linux
l0sgAi5 小时前
vLLM在RTX50系显卡上部署大模型-使用wsl2
linux·人工智能