close

OS: FreeBSD 9.0-Release

這台有二張網卡,要做防火牆IPFW和轉址Nat(Network Address Translation)的功能。使用NAT可以讓多台電腦使用一個網址連出去即可。

內網卡為:xl0,外網卡為:fxp0。

 

1. 首先要在Kernel中加入ipfw和nat的options,然後重編Kernel。

參考網址:http://www.twbsd.org/cht/book/ch12.htm
參考網址:http://mail.lsps.tp.edu.tw/~gsyan/freebsd2001/kernel.html

步驟如下:

a. 更改kernel檔:

#cd /sys/i386/conf
#cp GENERIC newkernel
#vi newkernel

在newkernel中加上:
# ----- Firewall Settings ----- #
options IPFIREWALL
options IPDIVERT                                              // for NAT
options IPFIREWALL_DEFAULT_TO_ACCEPT      //預設全部通道全開,以防沒有設好ipfw後全部斷線。
options IPFIREWALL_VERBOSE
options IPFIREWALL_FORWARD                        //or ip forward 根據測試,不加這條無妨。
options DUMMYNET                                            //use pipe to limit brand限制連線頻寬用。
# ----- END ----- #

存檔後進行編譯:
#config newkernel
#cd ../compile/newkernel/
#make depend
#make
#make install
#reboot

以上這段請儘可能不要用遠端操作,以防做完失敗叫不起來。

開機正確的話,請檢查:
#uname -a
FreeBSD server.dns 9.0-RELEASE FreeBSD 9.0-RELEASE #1: Mon Sep  3 14:06:58 CST 2012     id@server.dns:/sys/i386/compile/newkernel  i386

可以看得到使用的是剛剛重編過的kernel,並可正常使用。

 

2. 建立ipfw rule:

#cd /etc
#vi rc.firewall

檔案內容如下:
/sbin/ipfw -f flush
/sbin/ipfw add divert natd all from any to any via fxp0
/sbin/ipfw add allow icmp from any to any icmptypes 0,8,11
/sbin/ipfw add deny icmp from any to any
/sbin/ipfw add pass all from any to any

表示從內部到外部全部通過,外部不能連到內部,並且除了icmptypes中的0,8,11有反應之外,其餘不反應。這是最簡單的防火牆設法,其他使用系統給定的方法。請見以下rc.conf中的設定。

編寫rc.conf,當電腦開機後自動使用防火牆的功能與限制條件。

#vi /etc/rc.conf

# -- Firewall Settings -- #  //防火牆設定,要跟NAT配合使用,並且啟用前要重編Kernel
firewall_enable="Yes"       //啟用防火牆功能。
firewall_type="simple"      //使用系統中的防火牆規則,如果有新的再自行寫在rc.firewall中另外增減。
#firewall_type="open"     //表示全開。這條是測試用的。
firewall_quiet="No"
firewall_logging="Yes"      //記錄下防火牆的狀況。

 

3. NAT的設定。請先建立一個natd.conf檔,並在其中加入要轉址的設定。有些設定也可以寫在rc.conf中。

#touch /etc/natd.conf
#vi natd.conf

內容如下:
interface fxp0      //表示對外的網卡
redirect_port tcp 192.9.100.3:80 61.218.108.179:8080   //轉址設定

或是用指令來下:
#natd -f natd.conf   //有natd.conf檔的情況。
#/sbin/natd -f /etc/natd.conf -n fxp0
或是
#natd -redirect_port tcp 192.168.0.3:80 1.2.3.4:80 -interface fxp0
↑在做此動作之前,要先把舊的natd kill掉。

可是這一段我一直設不出來,一直有問題,待解中。

 

NATD需在開機時自動啟用,所以要編入/etc/rc.conf:

#vi /etc/rc.conf

# ------ NATD Settings ----- #       //轉址用,但這個功能要啟用要重編kernel
natd_enable="Yes"
natd_interface="fxp0"                 //指定對外網卡為哪一張。
natd_flags="-f /etc/natd.conf"     //如果有轉址設定,應讀取哪一個檔案。
# NAT Example #                         //如果轉址直接寫在rc.conf中,範例如下:
#natd_flags="-redirect_port tcp 192.168.0.3:80 1.2.3.4:80"

 

NATD設定參考:每次重設過NATD都要先把舊的natd kill掉。

1. http://www.twbsd.org/cht/book/ch12.htm
2. http://www.tek-tips.com/viewthread.cfm?qid=140700
3. http://hintsforums.macworld.com/archive/index.php/t-7468.html

arrow
arrow

    貓熊麗麗 發表在 痞客邦 留言(0) 人氣()