歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> 有關用linux作NAT服務(代理,透明代理等)

有關用linux作NAT服務(代理,透明代理等)

日期:2017/2/27 14:20:59   编辑:更多Linux
  一、准備工作:  1.一台 Redhat Linux主機(這是必備的),分別在redhat linux 7.1以後的任何一版本上都 測試沒有問題 2.兩塊網卡:eth0 eth1      3.系統支持iptables: [root@NetShare linux-2.4]# rpm -qagrep iptables iptables-1.2.5-3 iptables-ipv6-1.2.5-3 iptables的rpm包已經安裝上了,不過還要看一下內核的支持情況,也決定了iptables所 能發揮的功效 4.內核情況如下: [root@NetShare linux-2.4]# cd /usr/src/linux-2.4 [root@NetShare linux-2.4]# make menUConfig 在 Networking options ---> 這個選項裡有 IP: Netfilter Configuration ---> (以下是偶的選擇,供參考) <*> Connection tracking (required for masq/NAT) <M> FTP protocol support <M> IRC protocol support <M> Userspace queueing via NETLINK (EXPERIMENTAL) <*> IP tables support (required for filtering/masq/NAT) <M> limit match support <M> MAC address match support <M> netfilter MARK match support <M> Multiple port match support <M> TOS match support <M> AH/ESP match support <M> LENGTH match support <M> TTL match support <M> tcpmss match support <M> Connection state match support <M> Unclean match support (EXPERIMENTAL) <M> Owner match support (EXPERIMENTAL) <*> Packet filtering <M> REJECT target support <M> MIRROR target support (EXPERIMENTAL) <*> Full NAT <M> MASQUERADE target support <M> REDIRECT target support <M> Basic SNMP-ALG support (EXPERIMENTAL) <*> Packet mangling <M> TOS target support <M> MARK target support <M> LOG target support


<M> ULOG target support <M> TCPMSS target support 這是內核所支持的一些模塊,相關模塊都是作什麼的,可以去參考內核的說明文檔! 二、具體實施:   針對於此,我這幾天寫了一個簡單的小腳本,大家可以利用他來作代理服務,如果有能力,可 以添加一些相關的規則... 程序如下: #! /bin/sh ##### written by wind521 2002/12/17 ##### ##### mail: [email protected] ##### IPTABLES=/usr/sbin/iptables EXTERNAL="eth1" ---> 外網的接口 INTERNAL="eth0"  ---> 內網的接口 IP=192.168.0.0/24 ---> 內網地址 # ### 重置三條鏈默認的規則 # $IPTABLES -P INPUT ACCEPT $IPTABLES -P FORWARD ACCEPT $IPTABLES -P OUTPUT ACCEPT # ### 重置nat表 # $IPTABLES -t nat -P PREROUTING ACCEPT $IPTABLES -t nat -P POSTROUTING ACCEPT $IPTABLES -t nat -P OUTPUT ACCEPT # ### 刷新nat與鏈的所有規則 # $IPTABLES -F $IPTABLES -t nat -F # ### 刪除非默認的鏈和nat表的規則 # $IPTABLES -X $IPTABLES -t nat -X start() { # ###打開ip轉發 # echo -n $"Starting firewall " echo 1 > /proc/sys/net/ipv4/ip_forward # ###加載必要的模塊 # echo -n "Staring modprobe the necessary mod for iptables" for i in /lib/modules/`uname -r`/kernel/net/ipv4/netfilter/* do t=`echo $i sed 's/.o$//g'` module=`basename $t` modprobe $module done # ###允許ICMP數據包(ping) # $IPTABLES -A INPUT -p icmp -j ACCEPT # ###允許內部網之間的數據通訊 # $IPTABLES -A INPUT -i $INTERNAL -s $PRINET -j ACCEPT $IPTABLES -A OUTPUT -o $INTERNAL -d $PRINET -j ACCEPT # ###NAT轉發的關鍵 # $IPTABLES -t nat -A POSTROUTING -o $EXTERNAL -j MASQUERADE echo_success } stop(){ echo -n $"Stopping Firewall" flush for i in /lib/modules/`uname -r`/kernel/net/ipv4/netfilter/* do t=`echo $i sed 's/.o$//g'` module=`basename $t` modprobe -r $module done # Disale IPV4 Packet Forwarding echo "0" > /proc/sys/net/ipv4/ip_forward echo_success } restart() { stop start } # See how we were called. case "$1" in start) start

;; stop) stop ;; restart) restart ;; *) echo $"Usage: $0 {startstoprestart}" exit 1 esac 按照需要將你所對應的變量改動一下,應該沒有問題的! 說明:這個基本上能保證代理上網的功能,其他的什麼都沒有作,包括SNAT,DNAT,都沒有作, 如果有需要的可以自己去改動,如有問題,請與偶mail聯系!



stop) stop ;; restart) restart ;; *) echo $"Usage: $0 {startstoprestart}" exit 1 esac 按照需要將你所對應的變量改動一下,應該沒有問題的! 說明:這個基本上能保證代理上網的功能,其他的什麼都沒有作,包括SNAT,DNAT,都沒有作, 如果有需要的可以自己去改動,如有問題,請與偶mail聯系!



Copyright © Linux教程網 All Rights Reserved