Iptables的幾點實用方法

2021-12-30 08:02:11 字數 1956 閱讀 3070

在我們的日常運維工作中,經常要用到iptables這個工具來設定ip資訊包過濾和防火牆配置,首先,iptables的配置檔案為/etc/sysconfig/iptables,所有的規則都是要寫到這個檔案裡的,不然重啟後就會失效。

1、iptables通常包括三個**(fliter、nat、mangle)

filter∶主要跟linux本機有關,是預設的table。通常下面有三個鏈(chain):

input∶主要與資料報想要進入我們linux本機有關;

output∶主要與linux本機所要送出的資料有關;

forward∶這個與linux本機比較沒有關係,他可以將資料報**到後端的電腦中,與nat這個table相關

nat:主要用來做源與目的ip或者埠的轉換,與linux本機無關。

prerouting∶在進行路由判斷之前所要進行的規則(dnat/redirect)postrouting∶在進行路由判斷之後所要進行的規則(snat/masquerade)output∶與傳送出去的封包有關

mangle∶這個**主要是與特殊的封包的路由旗標有關,早期僅有prerouting及output鏈,不過從kernel2.4.18之後加入了input及forward鏈。由於這個**與特殊旗標相關性較高,所以像咱們這種單純的環境當中,較少使用mangle這個**

2、iptables的常用命令

檢視iptables現有規則:

#iptables-l-n

清除現有iptables規則:

#iptables-f#清除預設表filter中的所有規則鏈的規則:

允許區域網內192.168.0.0/24的所有主機訪問**伺服器,除了192.168.0.3這台主機:

#iptables-ainput-ieth0-s192.168.0.3-jdrop

#iptables-ainput-ieth0-s192.168.0.0/24-jaccept

將來自eth0的資料報全部接收:

#iptables-ainput-ieth0-jaccept

將伺服器80埠重定向到8080埠:

#iptables-tnat-aprerouting-ptcp--dport80-jredirect--to-ports8080

使伺服器允許來自網路介面eth1,並且**埠為80的資料進入伺服器:

#iptables-ainput-ieth1-ptcp--sport80-jaccept

讓伺服器的80埠對外開放:

#iptables-ainput-ieth0-ptcp--dport80-jaccept

允許來自外部的ping測試

#iptables-ainput-picmp--icmp-typeecho-request-jaccept#iptables-aoutput-picmp--icmp-typeecho-reply-jaccept僅允許來自指定網路的ssh連線請求

以下規則僅允許來自192.168.100.0/24的網路:

#iptables-ainput-ieth0-ptcp-s192.168.100.0/24--dport22-mstate--statenew,established-jaccept#iptables-aoutput-oeth0-ptcp--sport22-mstate--stateestablished-jaccept允許出站dns連線

#iptables-aoutput-pudp-oeth0--dport53-jaccept#iptables-ainput-pudp-ieth0--sport53-jaccept

iptables使用中幾點注意的問題

iptables共有3張表 filter,nat,mangle,用 t name選擇。filter是預設表,有input,output,forward三條鏈 nat在資料報建立新連線的時候用,有prerouting,postrouting,output三條鏈 mangle在資料報改變時使用,有pre...

關於iptables的常用技巧方法

iptables的起源發展防火牆的工作原理等這裡我們就不在複述。這裡主要記錄下日常運維過程中關於iptables的命令,引數及實際運用。iptables f 清除預設表filter中的所有規則鏈的規則 iptables x 清除預設表filter中使用者自定鏈中的規則 iptables p 設定預設...

iptables中DNAT的配置方法

1.一對一流量完全dnat 首先說一下網路環境,普通主機一台做防火牆用,網絡卡兩塊 eth0 192.168.0.1內網 eth1 202.202.202.1 外網 內網中一台主機 192.168.0.101 現在要把外網訪問202.202.202.1的所有流量對映到192.168.0.101上 命...