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+ 以读写方式打开一个文 ...
随机推荐
- 自己动手从零写桌面操作系统GrapeOS系列教程——8.x86介绍
由于GrapeOS目前只支持x86,所以本讲我们简单的介绍一下x86. 百度百科上是这样介绍的:x86泛指一系列基于Intel 8086且向后兼容的中央处理器指令集架构. 中央处理器就是我们平时说的C ...
- 云服务器 CentOS 的使用历程
------------恢复内容开始------------ CentOS图形页面的下载 安装X(X Window System),命令如下: yum groupinstall "X Win ...
- P7213 [JOISC2020] 最古の遺跡 3
这个题刚好是一个月前我们学校联赛组的人考的题的 T4 .今天有幸看见原题. 我当时顺便看了他们的题.想了一个小时,才想出来了如下的麻烦做法. 然后教练让我来讲这个题,我讲得很累,大家也都没有改. 题意 ...
- Python安装第三库超时的解决方法
Python安装第三库超时的解决方法 1. 在很多时候使用python的时候需要使用到某些第三方库,比较常规的方法是使用cmd命令使用在线安装的方法(前提是在安装好了python相应版本时候配置好了相 ...
- MessageUtil
1 public abstract class MessageUtil { 2 3 public static String changeMsg(CustomerReportQueryObject q ...
- python使用win32gui操作窗口
激活指定窗口 import win32gui import win32con def match_windows(win_title): """ 查找指定窗口 :para ...
- .NET版本发展史
.NET从始至今可以分为3个阶段,分别是.NET Framework阶段..NET Core阶段..NET阶段: .NET Framework终结于.NET Framework4.8版本,.NET C ...
- Datax-web入门配置与启动
在idea中启动Datax-web 需要先将Datax在本地安装,可以参考这篇文章(datax在win10中的安装) 1.从github上拉取源码 https://github.com/WeiYe-J ...
- Github好用的镜像网站
最近Github越来越不好用了,发现一个特别好用的镜像网站,无论是进入还是下载都非常的快. https://hub.yzuu.cf/ 首页和Github没有任何区别, 注意请不要在连着梯子的时候使用, ...
- rabbitMq客户端连接超时
rabbitMq客户端连接超时 rabbitmq有两个端口号:15672,用户web页面的http连接:5672用户客户端的tcp长连接. 用腾讯云搭建时:需要在防火墙策略处将两个端口都打开.