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
相关推荐
神秘人X70718 分钟前
Linux高效备份:rsync + inotify实时同步
linux·服务器·rsync
轻松Ai享生活24 分钟前
一步步学习Linux initrd/initramfs
linux
轻松Ai享生活28 分钟前
一步步深入学习Linux Process Scheduling
linux
绵绵细雨中的乡音2 小时前
网络基础知识
linux·网络
Peter·Pan爱编程2 小时前
Docker在Linux中安装与使用教程
linux·docker·eureka
kunge20133 小时前
Ubuntu22.04 安装virtualbox7.1
linux·virtualbox
清溪5493 小时前
DVWA中级
linux
Sadsvit4 小时前
源码编译安装LAMP架构并部署WordPress(CentOS 7)
linux·运维·服务器·架构·centos
xiaok4 小时前
为什么 lsof 显示多个 nginx 都在 “使用 443”?
linux
苦学编程的谢5 小时前
Linux
linux·运维·服务器