iptables(二)常用规则即操作示例
常用规则示例
修改chain默认策略
#filter表在INPUT chain默认策略为ACCEPT
[root@iptables_host02 ~]# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
#将filter表在INPUT chain默认策略修改为DROP
[root@iptables_host02 ~]# iptables -t filter -P INPUT DROP
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT
Chain INPUT (policy DROP 0 packets, 0 bytes)
chain的常用操作
清空规则
#清空指定table和chain下面的规则,什么都不指定会清空及其上所有的规则
#请空前表filter chain INPUT规则
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1052 82558 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
14 820 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
78 6024 INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
78 6024 INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
78 6024 INPUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
72 5616 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
[root@iptables_host02 ~]# iptables -t filter -F INPUT
#清空后表filter chain INPUT规则后及其就连不上了,截图如下
创建自定义chain
[root@iptables_host02 ~]# iptables -N test_chain
[root@iptables_host02 ~]# iptables -nvL test_chain
Chain test_chain (0 references)
pkts bytes target prot opt in out source destination
删除自定义的空chain
#删除用户自动的空chain
[root@iptables_host02 ~]# iptables -X test_chain
[root@iptables_host02 ~]# iptables -nvL test_chain
iptables: No chain/target/match by that name.
#当chain有规则时使用-X删除chain会返回chain非空
[root@iptables_host02 ~]# iptables -N test_X_chain
[root@iptables_host02 ~]# iptables -t filter -I test_X_chain -p TCP -s 10.1.1.1 --sport 80
[root@iptables_host02 ~]# iptables -nvL test_X_chain
Chain test_X_chain (0 references)
pkts bytes target prot opt in out source destination
0 0 tcp -- * * 10.1.1.1 0.0.0.0/0 tcp spt:80
[root@iptables_host02 ~]# iptables -X test_X_chain
iptables: Directory not empty.
重命名chain name
[root@iptables_host02 ~]# iptables -nvL test_X_chain
Chain test_X_chain (0 references)
pkts bytes target prot opt in out source destination
0 0 tcp -- * * 10.1.1.1 0.0.0.0/0 tcp spt:80
[root@iptables_host02 ~]# iptables -E test_X_chain test_Y_chain
[root@iptables_host02 ~]# iptables -nvL test_Y_chain
Chain test_Y_chain (0 references)
pkts bytes target prot opt in out source destination
0 0 tcp -- * * 10.1.1.1 0.0.0.0/0 tcp spt:80
清空指定chain talbe的规则计数
#对最后一条规则前两列的数字变化
[root@iptables_host02 ~]# iptables -t filter -nvL OUTPUT
Chain OUTPUT (policy ACCEPT 154 packets, 14444 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT udp -- * virbr0 0.0.0.0/0 0.0.0.0/0 udp dpt:68
0 0 ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0
915 84969 OUTPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
[root@iptables_host02 ~]# iptables -t filter -Z OUTPUT
[root@iptables_host02 ~]# iptables -t filter -Z OUTPUT
Chain OUTPUT (policy ACCEPT 5 packets, 516 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT udp -- * virbr0 0.0.0.0/0 0.0.0.0/0 udp dpt:68
0 0 ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0
5 516 OUTPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
查看指定chain table的规则
#iptables [-t表名] <-L> [链名]
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
403 29108 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 686 51000 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0
安全组规则的增加 删除 插入 替换
常用安全组规则的创建
#规则的增加和插入规则上只有-A(chain末尾追加)和-I(插入到chain第一条规则)参数不同,规则写法本身并无差异,下面统一以-I为例。
#规则详细语法参照iptales(一)的博客这里不在重复描述
#在VM2的INPUT chain filter表添加进入ens33网卡 源ip192.168.188.147 源端口8000 目的ip192.168.188.148 目的端口9000 协议tcp的报文DROP
[root@iptables_host02 ~]# iptables -t filter -I INPUT -i ens33 -s 192.168.188.147 -d 192.168.188.148 -p tcp --sport 8000 --dport 9000 -j DROP
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- ens33 * 192.168.188.147 192.168.188.148 tcp spt:8000 dpt:9000
2 2729 211K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0
#tcp指定多端口 udp类似
[root@iptables_host02 ~]# iptables -t filter -I INPUT -i ens33 -s 192.168.188.147 -d 192.168.188.148 -p tcp --sport 8000:9000 --dport 9000:9001 -j DROP
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- ens33 * 192.168.188.147 192.168.188.148 tcp spts:8000:9000 dpts:9000:9001
#控制icmp报文是否放通,可以添加icmp-type进行更细粒度的过滤
[root@iptables_host02 ~]# iptables -t filter -I INPUT -i ens33 -s 192.168.188.147 -d 192.168.188.148 -p icmp --icmp-type 0 -j DROP
[root@iptables_host02 ~]# iptables -t filter -I INPUT -i ens33 -s 192.168.188.147 -d 192.168.188.148 -p icmp --icmp-type 8 -j DROP
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 11 924 DROP icmp -- ens33 * 192.168.188.147 192.168.188.148 icmptype 8
2 0 0 DROP icmp -- ens33 * 192.168.188.147 192.168.188.148 icmptype 0
#匹配不连续端口,需要指定协议
[root@iptables_host02 ~]# iptables -t filter -I INPUT -i ens33 -s 192.168.188.147 -d 192.168.188.148 -p tcp -m multiport --dport 9800,9900:10000 -j DROP
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- ens33 * 192.168.188.147 192.168.188.148 multiport dports 9800,9900:10000
#指定范围ip,需要指定的范围不能准确用掩码匹配到一个段时候可以使用
[root@iptables_host02 ~]# iptables -t filter -I INPUT -i ens33 -p tcp -m iprange --src-range 10.1.1.3-10.1.1.9 --dst-range 10.2.1.2-10.2.1.5 -j DROP
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- ens33 * 0.0.0.0/0 0.0.0.0/0 source IP range 10.1.1.3-10.1.1.9 destination IP range 10.2.1.2-10.2.1.5
#匹配mac地址
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP udp -- ens33 * 0.0.0.0/0 0.0.0.0/0 MAC AA:BB:CC:DD:EE:FF
#状态检测
#禁止转发与正常TCP连接无关的非—syn请求数据包。“-m state”表示数据包的连接状态,“NEW”表示与任何连接无关的
[root@iptables_host02 ~]# iptables -I INPUT -m state --state NEW -p tcp ! --syn -j DROP
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp flags:!0x17/0x02
#拒绝访问防火墙的新数据包,但允许响应连接或与已有连接相关的数据包
#“ESTABLISHED”表示已经响应请求或者已经建立连接的数据包,“RELATED”表示与已建立的连接有相关性的,比如FTP数据连接等。
[root@iptables_host02 ~]# iptables -I INPUT -p tcp -m state --state NEW -j DROP
[root@iptables_host02 ~]# iptables -I INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
8 528 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW
#匹配的数据跳转到其它chain
[root@iptables_host02 ~]# iptables -I INPUT -p tcp -m state --state ESTABLISHED,RELATED -j test_chain
[root@iptables_host02 ~]# iptables -t filter -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
14 924 test_chain tcp -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
#匹配到的数据返回上上一级调用chain的调用点,这里相当于返回INPUT chain上面定义那条规则的后面一条规则
[root@iptables_host02 ~]# iptables -I test_chain -p tcp -m state --state ESTABLISHED,RELATED -j RETURN
[root@iptables_host02 ~]# iptables -t filter -nvL test_chain
Chain test_chain (1 references)
pkts bytes target prot opt in out source destination
38 2508 RETURN tcp -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
#sant dnat地址转换
[root@iptables_host02 ~]# iptables -t nat -I PREROUTING -d 192.168.10.18 -p tcp --dport 80 -j DNAT --to 172.16.100.2
[root@iptables_host02 ~]# iptables -t nat -nvL PREROUTING
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DNAT tcp -- * * 0.0.0.0/0 192.168.10.18 tcp dpt:80 to:172.16.100.2
[root@iptables_host02 ~]# iptables -t nat -I POSTROUTING -s 192.168.10.0/24 -j SNAT --to-source 172.16.100.1
[root@iptables_host02 ~]# iptables -t nat -nvL POSTROUTING
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 SNAT all -- * * 192.168.10.0/24 0.0.0.0/0 to:172.16.100.1
规则删除 替换
#规则删除,指定具体表和链以及规则编号进行删除
[root@iptables_host02 ~]# iptables -t nat -nvL POSTROUTING --line-numbers
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 SNAT all -- * * 192.168.10.0/24 0.0.0.0/0 to:172.16.100.1
2 0 0 RETURN all -- * * 192.168.122.0/24 224.0.0.0/24
[root@iptables_host02 ~]# iptables -t nat -D POSTROUTING 1
[root@iptables_host02 ~]# iptables -t nat -nvL POSTROUTING --line-numbers
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 RETURN all -- * * 192.168.122.0/24 224.0.0.0/24
2 0 0 RETURN all -- * * 192.168.122.0/24 255.255.255.255
#替换规则,指定chain和规则编号,当编号不存在会替换失败
[root@iptables_host02 ~]# iptables -t filter -nvL test_chain --line-number
Chain test_chain (1 references)
num pkts bytes target prot opt in out source destination
1 1304 98508 RETURN tcp -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
[root@iptables_host02 ~]# iptables -t filter -R test_chain 1 -s 10.1.1.23/32 -p tcp -j ACCEPT
[root@iptables_host02 ~]# iptables -t filter -nvL test_chain --line-number
Chain test_chain (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT tcp -- * * 10.1.1.23 0.0.0.0/0
iptables(二)常用规则即操作示例的更多相关文章
- iptables的nat规则骚操作
水一枪 我对防火墙这块的认知是比较低的, 之前一直没怎么去用 最多的要么就是 iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A OUT ...
- Linux iptables 防火墙常用规则
iptables 安装 yum install iptables iptables 规则清除 iptables -F iptables -X iptables -Z 开放指定的端口允许本地回环接口(即 ...
- iptables最常用的规则示例
iptables v1.4.21 iptables基础 规则(rules)其实就是网络管理员预定义的条件,规则一般的定义为“如果数据包头符合这样的条件,就这样处理这个数据包”.规则存储在内核空间的信息 ...
- iptables常用规则:屏蔽IP地址、禁用ping、协议设置、NAT与转发、负载平衡、自定义链
iptables常用规则:屏蔽IP地址.禁用ping.协议设置.NAT与转发.负载平衡.自定义链 时间 -- :: IT社区推荐资讯 原文 http://itindex.net/detail/4772 ...
- java-redis列表数据操作示例(二)
接上篇博文<java-redis字符类数据操作示例(一)>,redis连接管理类的代码请跳转查看. 一.列表类型缓存测试类 public class ListTest { /** * 主测 ...
- iptables防火墙常用配置介绍
参考地址 http://www.cnblogs.com/metoy/p/4320813.html http://netfilter.org/ iptables http://man.chinaunix ...
- VC++常用数据类型及其操作详解
原文地址:http://blog.csdn.net/ithomer/article/details/5019367 VC++常用数据类型及其操作详解 一.VC常用数据类型列表 二.常用数据类型转化 2 ...
- MySQL数据库(二)--库相关操作、表相关操作(1)、存储引擎、数据类型
一.库相关操作 1.创建数据库 (1)语法 create database 数据库 charset utf8; (2)数据库命名规范 可以由字母.数字.下划线.@.#.$ 区分大小写 唯一性 不能使用 ...
- JS中的常用的代码操作
本文件介绍常用的js代码的DOM操作.CSS操作.对象(Object对象.Array对象.Number对象.String对象.Math对象.JSON对象和Console对象)操作说明. 一.DOM树的 ...
- python3速查参考- python基础 5 -> 常用的文件操作
文件的打开方式 打开方式 详细释义 r 以只读方式打开文件.文件的指针会放在文件的开头.这是默认模式. rb 以二进制只读方式打开一个文件.文件指针会放在文件的开头. r+ 以读写方式打开一个文 ...
随机推荐
- [C#]为debug添加DebuggerDisplay属性
最近才发现,DebuggerDisplay 非常弓虽大,给类添加DebuggerDisplay属性后可以让调试变得更简单.如何使用? 1.定义一个有DebuggerDisplay的类:在类的头部添加& ...
- Socket:由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作
https://blog.csdn.net/weixin_45932157/article/details/113999801 最近服务器的Socket代理软件经常报这个错误:log:Error On ...
- 新的学习历程-python3 基本运算
1 print(5 / 2) # 2.5 2 print(5 // 3) #1 取整除 3 print(5 % 3) #2 取余数(取模) 4 print(5 ** 2) #25 5的2次方 5 pr ...
- 记录下vue表单验证
公共common文件夹下建立validate.js /* 是否邮编*/ export function validateMail(rule, value,callback) { const reg = ...
- [学习计划]mysql常用语句-随学随整理
<> 不等于 三元表达式 select *, if (num=1, "第一", "其他") as 别名 from 表 COUNT 统计总数并按某 ...
- mysql修改密码遇到的问题
在docker上安装了 mysql 容器,mysql镜像是8.0+版本 修改密码语句: 只针对本机生效 alter user "root"@'localhost' identifi ...
- PLC数据块中的偏移量
PLC数据块中的偏移量是什么? 偏移量定义为:把存储单元的实际地址与其所在段的段地址之间的距离称为段内偏移,也称为"有效地址或偏移量". 段地址左移四位,与有效地址相加,就构成了逻 ...
- .net core格式化响应数据(json驼峰格式)
//表格字段都是大写的 想要实现首字母小写(特定操作配置输出序列化选项)[HttpPost, ActionName("QueryAll")] public ActionResult ...
- HCIP-ICT实战进阶05-路由策略与策略路由
HCIP-ICT实战进阶05-路由策略与策略路由 0 前言 什么是路由策略? 基于报文的目的IP地址进行路由表查找, 之后转发数据; 针对控制平面, 为路由协议和路由表服务, 针对路由信息进行过滤或者 ...
- 当win7遭遇蓝屏代码0x0000006b
转载请注明来源:https://www.cnblogs.com/Sherlock-L/p/15069877.html 关键词:win7.蓝屏.0x0000006b 事发 话说在某个周末,当我打开电脑, ...