當架好Ubuntu Linux後,可以進階使用Iptables功能,將Ubuntu變成一台Router並對應到內部的IP開放相關服務,如[ftp][www]...等等。
環境介紹:
Server端
兩張網卡:當然你也可以將一張網卡給兩個IP
eth0:192.168.10.10 ===>外部IP
eth1:192.168.20.10 ===>內部IP(所有內網LAN透過這IP當作Gateway出去)

PC端網路卡設定
IP Address:192.168.20.20
Gateway:192.168.20.10

Server設定如下所示:
一、將以下這段文字寫入rc.local檔案,這樣重新啟動Linux就會載入這些設定。
#vim /etc/rc.local  

#!/bin/sh
##### iptables.rule #####
EIF="eth0" # 對外的網路介面
IIF="eth1" # 對內的網路介面
INNET="192.168.20.0/24" # 內部子網域

# forwarding
# 讓內部網路的封包可以轉送到外部
echo "1" > /proc/sys/net/ipv4/ip_forward

# flush all rules
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X

# 定義 policy
# Policy指的是當進來的封包不屬於rule中的任何一條時,所預設的動作。
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

# localhost
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# 讓主機主動建立的連線可以進來
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

# 設定主機上提供的服務可讓外部網路存取
iptables -A INPUT -i $EIF -p tcp --dport 22 -j ACCEPT # ssh
iptables -A INPUT -i $EIF -p udp --dport 22 -j ACCEPT
iptables -A INPUT -i $EIF -p tcp --dport 80 -j ACCEPT # http

# NAT 內部區域透過NAT瀏覽外網
iptables -t nat -A POSTROUTING -o $EIF -s $INNET -j MASQUERADE

#將80port對應到內部IP 192.168.20.20
iptables -t nat -A PREROUTING -p tcp -i $EIF --dport 80 -j DNAT --to-destination 192.168.20.20:80 # WWW

二、重新啟動Linux
#sudo reboot

測試看看是否只有提供ssh與www服務而已,開啟IE連接192.168.10.10,應該會導入到192.168.20.20的網頁。

arrow
arrow
    全站熱搜

    蒼穹 發表在 痞客邦 留言(4) 人氣()