# iptables

## 經由先前的簡易測試，我們針對ipv4設置iptables的過濾規則

### 清理規則

-F : Deleting (flushing) all the rules.&#x20;

-X : Delete chain.&#x20;

-t table\_name : Select table (called nat or mangle) and delete/flush rules.&#x20;

-P : Set the default policy (such as DROP, REJECT, or ACCEPT).

```
# Flush filter
IPTABLES=/sbin/iptables
$IPTABLES -F
$IPTABLES -X

echo "Flush mangle table ......"
echo
# Flush mangle
$IPTABLES -F -t mangle
$IPTABLES -t mangle -X


echo "Flush nat table ......"
echo
# Flush nat
$IPTABLES -F -t nat
$IPTABLES -t nat -X
###-----------------------------------------------------###
# 設定 filter table 的預設政策
###-----------------------------------------------------###
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT

```

IPTables中，主要有三張table，每張table內有專屬的chain，每個chain裡面有一系列的rule。

![圖片來源：http://www.thegeekstuff.com/2011/01/iptables-fundamentals/](/files/-M0L1yk8fbvX4HaELe4-)

所以，在進行規則規劃前，都應先清除所有的規則，保持乾淨

## 以前寫的shell

```
#!/bin/bash
#Author:kawsing
#Date:2012/11/27
#快速關閉防火牆，並回復預設值
ipt="/sbin/iptables"
#預設policies，全部ACCEPT
$ipt -t filter -P INPUT ACCEPT
$ipt -t filter -P FORWARD ACCEPT
$ipt -t filter -P OUTPUT ACCEPT
$ipt -t nat -P PREROUTING ACCEPT
$ipt -t nat -P OUTPUT ACCEPT
$ipt -t nat -P POSTROUTING ACCEPT
$ipt -t mangle -P INPUT ACCEPT
$ipt -t mangle -P OUTPUT ACCEPT
$ipt -t mangle -P FORWARD ACCEPT
$ipt -t mangle -P PREROUTING ACCEPT
$ipt -t mangle -P POSTROUTING ACCEPT

#計數器歸零
$ipt -t filter -Z
$ipt -t nat -Z
$ipt -t mangle -Z

#清除chain的規則
$ipt -t filter -F
$ipt -t nat -F
$ipt -t mangle -F
$ipt -t filter -X
$ipt -t nat -X
$ipt -t mangle -X
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kawsing.gitbook.io/opensystem/andoid-shou-ji/wang-lu-da-xiao-shi/xi-tong-an-quan/iptables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
