用 Debian 可以架設一個全功能的 gateway,它可以處理 NAT, mail, DHCP, DNS cache,
HTTP proxy cache, CVS, NFS 並提供家庭 LAN 系統的 Samba
services。有關這方面網路配置的討論可參閱 Netfilter
。
LAN 使用下面私有網路範圍的 IP 位址以避免和 Internet 上的 IP 位址衝突。
Class A: 10.0.0.0 with mask 255.0.0.0 Class B: 172.16.0.0 - 172.31.0.0 with mask 255.255.0.0 Class C: 192.168.0.0 - 192.168.255.0 with mask 255.255.255.0
Debian 使用 /etc/network/interfaces
來設定 IP。
舉個例子,假如某台機器的 eth0
使用 DHCP 提供的 IP 位址來連接
Internet,eth1
連接至 LAN ,則其
/etc/network/interfaces
的設定如下 (適用於 Woody 及之後的版本) :
auto lo iface lo inet loopback auto eth0 iface eth0 inet dhcp auto eth1 iface eth1 inet static address 192.168.1.1 network 192.168.1.0 netmask 255.255.255.0 broadcast 192.168.1.255
修改了 /etc/network/interfaces
後,可執行下面的命令使網路設定生效:
# /etc/init.d/networking restart
注意:在 Woody 及之後版本中的 /etc/network/interfaces
文件不相容於
Potato 。 (同樣的情況會出現在 Sarge 和 Woody 之間。)
如果系統使用 PCMCIA NIC 的話,則在 Potato 系統中需要設定
/etc/pcmcia/network.opts
,在 Woody 系統中已解決了這個問題。
如果懷疑的話可查看下列命令輸出:
# ifconfig # cat /proc/pci # cat /proc/interrupts # dmesg | more
很多新的 ADSL 伺服器使用 PPPoE 來建立 IP 連線。 Woody 安裝程式的選單中沒有
PPPoE ,但需要的套件都有。舉例來說, pppoe
和
pppoeconf
。在安裝過程中的 console 上執行 pppoeconf
可以進行簡單的設定。有時候, DSL (PPPoE) 會有 MTU 的問題。請參閱 LDP DSL-HOWTO
。
如果存在無法連至某些網站的問題,參閱 無法存取某此站台的怪問題, 第 3.7.5 節 。
一些具代表性的軟體:
# apt-get install nfs samba dhcpd dhcp-client bind squid procmail fetchmail # apt-get install ssh cvs
然後檢查一下下列的檔案:
/etc/init.d/dhcpd (edit to serve only LAN = eth1) /etc/host.allow (ALL: 192.168.0.0/16 127.0.0.0/8) for NFS /etc/exports (Need this for NFS) /etc/bind/db.192.168.1 (add) /etc/bind/db.lan (add) /etc/bind/named.conf (edit) /etc/resolv.conf (edit) /etc/hosts /etc/dhcpd.conf (edit for LAN = eth1) /etc/dhclient.conf (edit to force local DNS) /etc/samba/smb.conf /etc/exim/exim.conf /etc/mailname /etc/aliases /etc/squid.conf (add all LAN host IPs as allowed)
bind
建立一個本地端的 cache DNS server 並且將 DNS 改成 localhost
。檢查一下 /etc/resolv.conf
:
nameserver 127.0.0.1 search lan.aokiconsulting.com
Netfilter/iptables 是針對 Linux 2.4 及其後繼版本的一個防火牆子系統。參閱
Netfilter
,那兒有許多有關其設定的討論和解釋。
Netfilter 內建了 5 條 chain 來處理封包: PREROUTING 、 INPUT 、 FORWARD 、 OUTPUT 和 POSTROUTING :
routing decision IN ------> PRE ---> ------> FORWARD -----> ----> POST -----> OUT interface ROUTING \ filter / ROUTING interface DNAT | tracking ^ SNAT REDIRECT | | MASQUERADE v | INPUT OUTPUT | filter ^ filter,DNAT v | \--> Local Process --/ user-space programs
封包在每條內建的 chain 中傳輸時,會按下列過濾表中的規則進行處理。
防火牆規則有幾個目標:
--to-source ipaddr[-ipaddr][:port-port]
--to-ports port[-port]
--to-destination ipaddr[-ipaddr][:port-port]
--to-ports port[-port]
iptables
的基本命令有:
iptables -N chain # 建立一個 chain iptables -A chain \ # 在 chain 加入規則 -t table \ # 建立 table (filter, nat, mangle) -p protocol \ # tcp, udp, icmp, or all, -s source-address[/mask] \ --sport port[:port] \ # source port if -p is tcp or udp -d destination-address[/mask] \ --dport port[:port] \ # dest. port if -p is tcp or udp -j target \ # what to do if match -i in-interface-name \ # for INPUT, FORWARD, PREROUTING -o out-interface-name # for FORWARD, OUTPUT, POSTROUTING
在 LAN 中的機器可經由一個執行 IP-masquerade (NAT) 的 gateway 來共享一個可連外的 IP 位址,以存取 Internet 資源。
# apt-get install ipmasq
套用範例規則來加強 ipmasq
的保護機制。參閱
/usr/share/doc/ipmasq/examples/stronger/README
。對於使用 Debian
kernel-image-2.4 ,請確認載入了適當的模組。有關的必要設定請參閱 Network function, 第 7.2.3 節 。
如果使用 Debian kernel-image-2.2 的話,可按下面的方法編輯
/etc/masq/rules
中的 Z92timeouts.rul
檔案,以保證可長時間連接遠端網站 (有利於發送大容量的 email 等等):
# tcp, tcp-fin, udp # 2hr, 10 sec, 160 sec - default # 1 day, 10 min, 10 min - longer example $IPCHAINS -M -S 86400 600 600
同樣,如果是通過 PCMCIA NIC 連接網絡, ipmasq
需要從
/etc/pcmcia/network.opts
啟動。參閱
/usr/share/doc/ipmasq/ipmasq.txt.gz
。
假設你將一台筆記型電腦重新配置成可連入其它的 LAN 環境,你想使用筆記型電腦的郵件代理而不想重新設定。
使用 iptables
命令向 gateway 機器中加入下面的規則,就可重導向與
gateway 機器的 SMTP 連接。
# iptables -t nat -A PREROUTING -s 192.168.1.0/24 -j REDIRECT \ -p tcp --dport smtp --to-port 25 # smtp=25, INPUT is open
想使用更完備的重導向規則,建議安裝 ipmasq
套件,並在
/etc/ipmasq/rules/
目錄中加入
。
M30redirect.def
[FIXME] Policy routing (by Phil Brutsche pbrutsch@tux.creighton.edu
):
詳情請參閱 iproute manual
。
Traffic control (tc) may also be interesting.
環境:
eth0: 192.168.1.2/24; gateway 192.168.1.1 eth1: 10.0.0.2/24; gateway 10.0.0.1 No masquerading on this machine.
Special magic:
[FIXME] I've never done this. How to set up dialup as backup to a fast connection with auto dial-features? Please send me a patch here :)
Debian 參考手冊
1.07-12, 週三 九月 8 02:54:31 UTC 2004osamu@debian.org
wctang@csie.nctu.edu.tw