@@ -15,189 +15,6 @@ iptables(选项)(参数)
15
15
16
16
### 选项
17
17
18
- ```
19
- -t<表>:指定要操纵的表;
20
- -A:向规则链中添加条目;
21
- -D:从规则链中删除条目;
22
- -i:向规则链中插入条目;
23
- -R:替换规则链中的条目;
24
- -L:显示规则链中已有的条目;
25
- -F:清楚规则链中已有的条目;
26
- -Z:清空规则链中的数据包计算器和字节计数器;
27
- -N:创建新的用户自定义规则链;
28
- -P:定义规则链中的默认目标;
29
- -h:显示帮助信息;
30
- -p:指定要匹配的数据包协议类型;
31
- -s:指定要匹配的数据包源ip地址;
32
- -j<目标>:指定要跳转的目标;
33
- -i<网络接口>:指定数据包进入本机的网络接口;
34
- -o<网络接口>:指定数据包要离开本机所使用的网络接口。
35
- ```
36
-
37
- ** iptables命令选项输入顺序:**
38
-
39
- ```
40
- iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作
41
- ```
42
-
43
- 表名包括:
44
-
45
- - ** raw** :高级功能,如:网址过滤。
46
- - ** mangle** :数据包修改(QOS),用于实现服务质量。
47
- - ** net** :地址转换,用于网关路由器。
48
- - ** filter** :包过滤,用于防火墙规则。
49
-
50
- 规则链名包括:
51
-
52
- - ** INPUT链** :处理输入数据包。
53
- - ** OUTPUT链** :处理输出数据包。
54
- - ** PORWARD链** :处理转发数据包。
55
- - ** PREROUTING链** :用于目标地址转换(DNAT)。
56
- - ** POSTOUTING链** :用于源地址转换(SNAT)。
57
-
58
- 动作包括:
59
-
60
- - ** ACCEPT** :接收数据包。
61
- - ** DROP** :丢弃数据包。
62
- - ** REDIRECT** :重定向、映射、透明代理。
63
- - ** SNAT** :源地址转换。
64
- - ** DNAT** :目标地址转换。
65
- - ** MASQUERADE** :IP伪装(NAT),用于ADSL。
66
- - ** LOG** :日志记录。
67
-
68
- ``` bash
69
- ┏╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍┓
70
- ┌───────────────┐ ┃ Network ┃
71
- │ table: filter │ ┗━━━━━━━┳━━━━━━━┛
72
- │ chain: INPUT │◀────┐ │
73
- └───────┬───────┘ │ ▼
74
- │ │ ┌───────────────────┐
75
- ┌ ▼ ┐ │ │ table: nat │
76
- │local process│ │ │ chain: PREROUTING │
77
- └ ┘ │ └─────────┬─────────┘
78
- │ │ │
79
- ▼ │ ▼ ┌─────────────────┐
80
- ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ │ ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ │table: nat │
81
- Routing decision └───── outing decision ─────▶│chain: PREROUTING│
82
- ┅┅┅┅┅┅┅┅┅┳┅┅┅┅┅┅┅┅┅ ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ └────────┬────────┘
83
- │ │
84
- ▼ │
85
- ┌───────────────┐ │
86
- │ table: nat │ ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ │
87
- │ chain: OUTPUT │ ┌─────▶ outing decision ◀──────────────┘
88
- └───────┬───────┘ │ ┅┅┅┅┅┅┅┅┳┅┅┅┅┅┅┅┅
89
- │ │ │
90
- ▼ │ ▼
91
- ┌───────────────┐ │ ┌────────────────────┐
92
- │ table: filter │ │ │ chain: POSTROUTING │
93
- │ chain: OUTPUT ├────┘ └──────────┬─────────┘
94
- └───────────────┘ │
95
- ▼
96
- ┏╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍┓
97
- ┃ Network ┃
98
- ┗━━━━━━━━━━━━━━━┛
99
- ```
100
-
101
-
102
- ### 实例
103
-
104
- ** 清除已有iptables规则**
105
-
106
- ```
107
- iptables -F
108
- iptables -X
109
- iptables -Z
110
- ```
111
-
112
- ** 开放指定的端口**
113
-
114
- ```
115
- iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT #允许本地回环接口(即运行本机访问本机)
116
- iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #允许已建立的或相关连的通行
117
- iptables -A OUTPUT -j ACCEPT #允许所有本机向外的访问
118
- iptables -A INPUT -p tcp --dport 22 -j ACCEPT #允许访问22端口
119
- iptables -A INPUT -p tcp --dport 80 -j ACCEPT #允许访问80端口
120
- iptables -A INPUT -p tcp --dport 21 -j ACCEPT #允许ftp服务的21端口
121
- iptables -A INPUT -p tcp --dport 20 -j ACCEPT #允许FTP服务的20端口
122
- iptables -A INPUT -j reject #禁止其他未允许的规则访问
123
- iptables -A FORWARD -j REJECT #禁止其他未允许的规则访问
124
- ```
125
-
126
- ** 屏蔽IP**
127
-
128
- ```
129
- iptables -I INPUT -s 123.45.6.7 -j DROP #屏蔽单个IP的命令
130
- iptables -I INPUT -s 123.0.0.0/8 -j DROP #封整个段即从123.0.0.1到123.255.255.254的命令
131
- iptables -I INPUT -s 124.45.0.0/16 -j DROP #封IP段即从123.45.0.1到123.45.255.254的命令
132
- iptables -I INPUT -s 123.45.6.0/24 -j DROP #封IP段即从123.45.6.1到123.45.6.254的命令是
133
- ```
134
-
135
- ** 查看已添加的iptables规则**
136
-
137
- ```
138
- iptables -L -n -v
139
- Chain INPUT (policy DROP 48106 packets, 2690K bytes)
140
- pkts bytes target prot opt in out source destination
141
- 5075 589K ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
142
- 191K 90M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
143
- 1499K 133M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
144
- 4364K 6351M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
145
- 6256 327K ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
146
-
147
- Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
148
- pkts bytes target prot opt in out source destination
149
-
150
- Chain OUTPUT (policy ACCEPT 3382K packets, 1819M bytes)
151
- pkts bytes target prot opt in out source destination
152
- 5075 589K ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0
153
- ```
154
-
155
- ** 删除已添加的iptables规则**
156
-
157
- 将所有iptables以序号标记显示,执行:
158
-
159
- ```
160
- iptables -L -n --line-numbers
161
- ```
162
-
163
- 比如要删除INPUT里序号为8的规则,执行:
164
-
165
- ```
166
- iptables -D INPUT 8
167
- ```
168
-
169
- ** 开机启动网络转发规则**
170
-
171
- 公网` 210.14.67.7 ` 让内网` 192.168.188.0/24 ` 上网
172
-
173
- ``` bash
174
- iptables -t nat -A POSTROUTING -s 192.168.188.0/24 -j SNAT --to-source 210.14.67.127
175
- ```
176
-
177
- ** 端口映射**
178
-
179
- 本机的 2222 端口映射到内网 虚拟机的22 端口
180
-
181
- ``` bash
182
- iptables -t nat -A PREROUTING -d 210.14.67.127 -p tcp --dport 2222 -j DNAT --to-dest 192.168.188.115:22
183
- ```
184
- iptables
185
- ===
186
-
187
- Linux上常用的防火墙软件
188
-
189
- ## 补充说明
190
-
191
- ** iptables命令** 是Linux上常用的防火墙软件,是netfilter项目的一部分。可以直接配置,也可以通过许多前端和图形界面配置。
192
-
193
- ### 语法
194
-
195
- ```
196
- iptables(选项)(参数)
197
- ```
198
-
199
- ### 选项
200
-
201
18
```
202
19
-t, --table table 对指定的表 table 进行操作, table 必须是 raw, nat,filter,mangle 中的一个。如果不指定此选项,默认的是 filter 表。
203
20
-L, --list [chain] 列出链 chain 上面的所有规则,如果没有指定链,列出表上所有链的所有规则。
@@ -287,6 +104,15 @@ iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协
287
104
288
105
### 实例
289
106
107
+ #### 列出已设置的规则
108
+
109
+ ``` bash
110
+ iptables -L -t nat # 列出 nat 上面的所有规则
111
+ # ^ -t 参数指定,必须是 raw, nat,filter,mangle 中的一个
112
+ iptables -L -t nat --line-numbers # 规则带编号
113
+ iptables -L INPUT
114
+ ```
115
+
290
116
#### 清除已有iptables规则
291
117
292
118
```
0 commit comments