2011年4月22日 星期五

iptable基本

引自 http://blog.panyaya.com/2010/12/iptables.html
1. 觀察目前iptables設定
# iptables -L -n
2. 打開Forward
# echo "1" > /proc/sys/net/ipv4/ip_forward
3. 清除所有iptables規則
清除預設表filter中,所有規則鏈中的規則
# iptables -F
清除預設表filter中,使用者自訂鏈的規則
# iptables -X
清除mangle表中,所有規則鏈中的規則
# iptables -F -t mangle
清除mangle表中,使用者自訂鏈的規則
# iptables -t mangle -X
清除nat表中,所有規則鏈中的規則
# iptables -t nat -F
清除nat表中,使用者自訂鏈的規則
# iptables -t nat -X
參數說明
-F: 清除所有的已訂定的規則
-X: 清除所有使用者建立的規則
-Z: 將所有的chain的計數與流量統計都歸零
4. 選擇Policy
# iptables [-t tables] [-P] [INPUT,OUTPUT,FORWARD | PREROUTING,OUTPUT,POSTROUTING] [ACCEPT,DROP]
參數說明
-P: 定義政策
INPUT: 封包為輸入主機的方向
OUTPUT: 封包為輸出主機的方向
FORWARD: 封包為不進入主機而向外再傳輸出去的方向
PREROUTING: 在進入路由之前進行的工作
OUTPUT: 封包為輸出主機的方向
POSTROUTING: 在進入路由之後進行工作
設定全部丟棄
# iptables -P INPUT DROP
# iptables -P OUTPUT DROP
# iptables -P FORWARD DROP
設定全部接受
# iptables -P INPUT ACCEPT
# iptables -P OUTPUT ACCEPT
# iptables -P FORWARD ACCEPT
一般設定
# iptables -P INPUT DROP
# iptables -P OUTPUT ACCEPT
# iptables -P FORWARD ACCEPT
# iptables -t nat -P PREROUTING ACCEPT
# iptables -t nat -P OUTPUT ACCEPT
# iptables -t nat -P POSTROUTING ACCEPT

iptables服務先關閉

iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
iptables -t nat -P PREROUTING DROP
iptables -t nat -P POSTROUTING DROP
iptables -t nat -P OUTPUT DROP
再依你的需求一條一條的加進你的rule,一邊加邊測試

2011年4月21日 星期四

iptable限流量

linux下通過iptables限制流量,簡單,實用。首先需要將這台機器配置成閘道。

1.配置iptables轉發

2.限速腳本
vi /root/xiansu.sh
  #!/bin/bash
/sbin/iptables -F FORWARD
#限制網段
for ((i = 1; i < 253; i++))  do
/sbin/iptables -A FORWARD -s 192.168.2.$i -m limit \
   --limit 60/s -j ACCEPT
/sbin/iptables -A FORWARD -s 192.168.2.$i -j DROP

done
#限制單個ip
  /sbin/iptables -A FORWARD -s 192.168.1.135/32 -m limit \
     --limit 60/s -j ACCEPT
/sbin/iptables -A FORWARD -s 192.168.1.135/32 -j DROP


2.增加執行權限:
chmod+x /root/xiansu.sh

當流量小於60/s的時候,iptables 接受並轉發。當流量大於60/s 的時候,iptables丟棄數據包。

XP修改網路IP

  匯出
     netsh -c interface dump >c:\netset.txt
     當然,interface可以簡寫成int,dump更可簡化成d,所以就變成了
     netsh -c int d >c:\netset.txt
  匯入
     netsh -f c:\netset.txt 即可
-----------------------------------------------------
netset內容 可編寫批次檔 netsh -f netset.txt
---------------------------------------------
#=========
# 介面設定
#=========
pushd interface
reset all

popd
# 介面設定結束
#=========
# 介面設定
#=========
pushd interface ipv6
uninstall

popd
# 介面設定結束

# ----------------------------------
# ISATAP 設定
# ----------------------------------
pushd interface ipv6 isatap

popd
# ISATAP 設定結束

# ----------------------------------
# 6to4 設定
# ----------------------------------
pushd interface ipv6 6to4
reset

popd
# 6to4 設定結束
#========================
# 連接埠 Proxy 設定
#========================
pushd interface portproxy
reset

popd
# 連接埠 Proxy 設定結束

# ----------------------------------
# 介面 IP 設定        
# ----------------------------------
pushd interface ip

# "區域連線" 的介面 IP 設定
set address name="區域連線" source=static addr=192.168.1.7 mask=255.255.255.0
set address name="區域連線" gateway=192.168.1.1 gwmetric=0
set dns name="區域連線" source=static addr=168.95.1.1 register=PRIMARY
set wins name="區域連線" source=static addr=none

popd
# 介面 IP 設定結束

2011年4月19日 星期二