📄 10.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0066)http://qref.sourceforge.net/Debian/reference/ch-gateway.zh-cn.html -->
<HTML><HEAD><TITLE>Debian 参考手册 - 使用Debian系统架设网关</TITLE>
<META http-equiv=content-type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.3790.118" name=GENERATOR></HEAD>
<BODY><A name=ch-gateway></A>
<HR>
[ <A
href="http://qref.sourceforge.net/Debian/reference/ch-tune.zh-cn.html">上一页</A> ]
[ <A
href="http://qref.sourceforge.net/Debian/reference/reference.zh-cn.html#contents">目录</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-preface.zh-cn.html">1</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-system.zh-cn.html">2</A> ]
[ <A
href="http://qref.sourceforge.net/Debian/reference/ch-install.zh-cn.html">3</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-tutorial.zh-cn.html">4</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-woody.zh-cn.html">5</A> ]
[ <A
href="http://qref.sourceforge.net/Debian/reference/ch-package.zh-cn.html">6</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-kernel.zh-cn.html">7</A> ]
[ <A
href="http://qref.sourceforge.net/Debian/reference/ch-tips.zh-cn.html">8</A> ] [
<A href="http://qref.sourceforge.net/Debian/reference/ch-tune.zh-cn.html">9</A>
] [ 10 ] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-edit.zh-cn.html">11</A> ]
[ <A
href="http://qref.sourceforge.net/Debian/reference/ch-vcs.zh-cn.html">12</A> ] [
<A
href="http://qref.sourceforge.net/Debian/reference/ch-program.zh-cn.html">13</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-gnupg.zh-cn.html">14</A> ]
[ <A
href="http://qref.sourceforge.net/Debian/reference/ch-support.zh-cn.html">15</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ap-appendix.zh-cn.html">A</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-edit.zh-cn.html">下一页</A> ]
<HR>
<H1>Debian 参考手册 <BR>第 10 章 - 使用Debian系统架设网关 </H1>
<HR>
<P>用Debian可以架设一个全能的网关,它可以承担NAT、mail、DHCP、DNS cache、HTTP proxy
cache、CVS、NFS的工作以及提供面向家庭LAN系统的Samba services。有关这方面网络配置的讨论可参阅<CODE><A
href="http://www.netfilter.org/">Netfilter</A></CODE>。
<HR>
<A name=s10.1></A>
<H2>10.1 网络配置</H2>
<HR>
<A name=s-ip-class></A>
<H3>10.1.1 网关的主机设置</H3>
<P>LAN按下面方法分段使用IP地址以避免Internet上的IP地址冲突。 <PRE> 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
</PRE>
<P>在Debian中<CODE>/etc/network/interfaces</CODE>文件用于IP设置。
<P>举个例子,假如某台机器的<CODE>eth0</CODE>使用DHCP动态IP地址连接Internet,<CODE>eth1</CODE>连接LAN,则其<CODE>/etc/network/interfaces</CODE>的设置如下(适用于Woody及后继版本)
<PRE> 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
</PRE>
<P>修改了<CODE>/etc/network/interfaces</CODE>后,可执行下面的命令使其生效: <PRE> # /etc/init.d/networking restart
</PRE>
<P>注意:在Woody及其后继版本中的<CODE>/etc/network/interfaces</CODE>文件不适用于Potato。(同样的差异会出现在Sarte和Woody之间。)
<P>如果系统使用PCMCIA
NIC,则在Potato系统中需要设置<CODE>/etc/pcmcia/network.opts</CODE>而不是interfaces,在Woody就不必了,系统已解决了这个问题。
<P>想检查设置结果可查看下列命令输出: <PRE> # ifconfig
# cat /proc/pci
# cat /proc/interrupts
# dmesg | more
</PRE>
<P>有时,DSL(PPPoE)连接存在MTU问题,参阅<CODE><A
href="http://www.tldp.org/HOWTO/DSL-HOWTO/">DSL-HOWTO</A></CODE>。如果存在无法访问某些站点的问题,参阅<A
href="http://qref.sourceforge.net/Debian/reference/ch-install.zh-cn.html#s-killecn">无法访问某此站点的怪问题,
第 3.7.5 节</A>。
<HR>
<A name=s-ip-check></A>
<H3>10.1.2 网络设置检查</H3>
<P>典型的软件集合: <PRE> # apt-get install nfs samba dhcpd dhcp-client bind squid procmail fetchmail
# apt-get install ssh cvs
</PRE>
<P>检查下列文件: <PRE> /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)
</PRE>
<P><CODE>bind</CODE>创建一个本地的cache DNS server并且changes DNS to
localhost。检查<CODE>/etc/resolv.conf</CODE>: <PRE> nameserver 127.0.0.1
search lan.aokiconsulting.com
</PRE>
<HR>
<A name=s-netfilter></A>
<H2>10.2 Netfilter设置</H2>
<P>在Linux 2.4及其后继版本中加入了netfilter/iptables项目,作为一个防火墙子系统。参阅<CODE><A
href="http://www.netfilter.org/">Netfilter</A></CODE>,那儿有许多有关其配置的讨论和解释。
<HR>
<A name=s-netfilter-basics></A>
<H3>10.2.1 netfilter基础</H3>
<P>Netfilter内建了5条链路来处理数据包,它们分别是:PREROUTING、INPUT、FORWARD、OUTPUT和POSTROUTING: <PRE> 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
</PRE>
<HR>
<A name=s-netfilter-table></A>
<H3>10.2.2 过滤表(Netfilter table)</H3>
<P>数据包在每条内建的链路中传输时按如下过滤表中的规则进行处理。
<UL>
<LI>filter(数据包过滤器,链路中默认的过滤器)
<UL>
<LI>INPUT(作用于进入本机的数据包)
<LI>FORWARD(作用于路由到本机的数据包)
<LI>OUTPUT(作用于本地产生的数据包) </LI></UL>
<LI>nat(网络地址翻译)
<UL>
<LI>PREROUTING(作用于刚进入的待转换数据包)
<LI>OUTPUT(作用于在路由之前待转换的本地产生的数据包)
<LI>POSTROUTING(作用于待发出的已转换的数据包) </LI></UL>
<LI>mangle (network address mangling, good only after 2.4.18)
<UL>
<LI>适用于所有5条链路。 </LI></UL></LI></UL>
<HR>
<A name=s-netfilter-target></A>
<H3>10.2.3 过滤目标(Netfilter target)</H3>
<P>Firewall rules have several targets:
<UL>
<LI>4个基本目标:
<UL>
<LI>ACCEPT 允许数据包通过。
<LI>DROP 阻拦数据包。
<LI>QUEUE 允许数据包进入用户空间(userspace)(如果内核支持的话)。
<LI>RETURN means stop traversing this chain and resume at the next rule in
the previous (calling) chain. </LI></UL>
<LI>扩展目标:
<UL>
<LI>LOG 打开内核日志。
<LI>REJECT 回送错误数据包并阻拦该数据包。
<LI>SNAT 修改数据包源地址,仅作用于POSTROUTING链路。(仅适用于nat过滤表) <PRE> --to-source ipaddr[-ipaddr][:port-port]
</PRE>
<LI>MASQUERADE 作用和SNAT一样,但面向使用动态IP请求建立的连接(拔号连接)。(仅适用于nat过滤表) <PRE> --to-ports port[-port]
</PRE>
<LI>DNAT 修改数据包目的地址,仅作用于PREROUTING、OUTPUT链路以及由它们调用的用户自定义链路。(仅适用于nat过滤表) <PRE> --to-destination ipaddr[-ipaddr][:port-port]
</PRE>
<LI>REDIRECT 修改数据包目标地址使其发送给本机。 <PRE> --to-ports port[-port]
</PRE></LI></UL></LI></UL>
<HR>
<A name=s-netfilter-command></A>
<H3>10.2.4 网络过滤器命令</H3>
<P><CODE>iptables</CODE>的基本命令有: <PRE> iptables -N <VAR>chain</VAR> # create a <VAR>chain</VAR>
iptables -A <VAR>chain</VAR> \ # add rule to <VAR>chain</VAR>
-t <VAR>table</VAR> \ # use <VAR>table</VAR> (filter, nat, mangle)
-p <VAR>protocol</VAR> \ # tcp, udp, icmp, or all,
-s <VAR>source-address[/mask]</VAR> \
--sport <VAR>port[:port]</VAR> \ # source port if -p is tcp or udp
-d <VAR>destination-address[/mask]</VAR> \
--dport <VAR>port[:port]</VAR> \ # dest. port if -p is tcp or udp
-j <VAR>target</VAR> \ # what to do if match
-i <VAR>in-interface-name</VAR> \ # for INPUT, FORWARD, PREROUTING
-o <VAR>out-interface-name</VAR> # for FORWARD, OUTPUT, POSTROUTING
</PRE>
<HR>
<A name=s-ip-masq></A>
<H3>10.2.5 IP伪装(IP-masquerade)</H3>
<P>一个运用IP伪装(NAT)的网关可实现LAN内的机器通过共享一个单独的可访问外网的IP地址来访问Internet资源。 <PRE> # apt-get install ipmasq
</PRE>
<P>执行样例规则来加强<CODE>ipmasq</CODE>的保护机制。参阅<CODE>/usr/share/doc/ipmasq/examples/stronger/README</CODE>。对于使用2.4版内核镜像的Debian,请确认加载了相应的模块。有关的必要设置参阅<A
href="http://qref.sourceforge.net/Debian/reference/ch-kernel.zh-cn.html#s-kernel-net">网络功能,
第 7.2.3 节</A>。
<P>对于使用2.2版内核镜像的Debian,可按下面的方法编辑<CODE>/etc/masq/rules</CODE>中的<CODE>Z92timeouts.rul</CODE>文件,以保证可长时间连接远程站点(如发送大容量的email,等):
<PRE> # tcp, tcp-fin, udp
# 2hr, 10 sec, 160 sec - default
# 1 day, 10 min, 10 min - longer example
$IPCHAINS -M -S 86400 600 600
</PRE>
<P>同样,如果是通过PCMCIA
NIC访问网络,<CODE>ipmasq</CODE>需要从<CODE>/etc/pcmcia/network.opts</CODE>启动。参阅<CODE>/usr/share/doc/ipmasq/ipmasq.txt.gz</CODE>.
<HR>
<A name=s-ip-redirect></A>
<H3>10.2.6 重定向SMTP联接(2.4版内核)</H3>
<P>假设你将一台笔记本电脑重新配置成可连入其它的LAN环境,而你不想再重新配置用户邮件代理,即:想直接用原来的配置收发邮件。
<P>使用<CODE>iptables</CODE>命令向网关机器中加入下面的规则,就可以实现重定向与网关机器的SMTP连接。 <PRE> # 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
</PRE>
<P>想使用更完备的重定向规则集,建议安装<CODE>ipmasq</CODE>软件包,并在<CODE>/etc/ipmasq/rules/</CODE>目录中添加<CODE><CODE><A
href="http://qref.sourceforge.net/Debian/reference/examples/">M30redirect.def</A></CODE></CODE>文件。
<HR>
<A name=s10.3></A>
<H2>10.3 管理多重网络联接</H2>
<P>[FIXME] 路由策略(by Phil Brutsche <CODE><A
href="mailto:pbrutsch@tux.creighton.edu">pbrutsch@tux.creighton.edu</A></CODE>):
详情参阅<CODE><A href="http://lartc.org/">iproute manual</A></CODE>。Traffic control
(tc) 也很有趣。
<P>Environment: <PRE> 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.
</PRE>
<P>Special magic:
<OL type=1>
<LI>ip rule add from 192.168.1.2 lookup 1
<LI>ip rule add from 10.0.0.2 lookup 2
<LI>ip route add to default via 10.0.0.1 metric 0
<LI>ip route add to default via 192.168.1.1 metric 1
<LI>ip route add table 1 to 192.168.1.0/24 via eth0
<LI>ip route add table 1 to 10.0.0.2/24 via eth1
<LI>ip route add table 1 to default via 192.168.1.1
<LI>ip route add table 2 to 192.168.1.0/24 via eth0
<LI>ip route add table 2 to 10.0.0.2/24 via eth1
<LI>ip route add table 2 to default via 10.0.0.2 </LI></OL>
<P>[FIXME] 我没亲自做过。如何利用自动拔号特性使拔号连接保持高速?如果你知道请发补丁我:)
<HR>
[ <A
href="http://qref.sourceforge.net/Debian/reference/ch-tune.zh-cn.html">上一页</A> ]
[ <A
href="http://qref.sourceforge.net/Debian/reference/reference.zh-cn.html#contents">目录</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-preface.zh-cn.html">1</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-system.zh-cn.html">2</A> ]
[ <A
href="http://qref.sourceforge.net/Debian/reference/ch-install.zh-cn.html">3</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-tutorial.zh-cn.html">4</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-woody.zh-cn.html">5</A> ]
[ <A
href="http://qref.sourceforge.net/Debian/reference/ch-package.zh-cn.html">6</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-kernel.zh-cn.html">7</A> ]
[ <A
href="http://qref.sourceforge.net/Debian/reference/ch-tips.zh-cn.html">8</A> ] [
<A href="http://qref.sourceforge.net/Debian/reference/ch-tune.zh-cn.html">9</A>
] [ 10 ] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-edit.zh-cn.html">11</A> ]
[ <A
href="http://qref.sourceforge.net/Debian/reference/ch-vcs.zh-cn.html">12</A> ] [
<A
href="http://qref.sourceforge.net/Debian/reference/ch-program.zh-cn.html">13</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-gnupg.zh-cn.html">14</A> ]
[ <A
href="http://qref.sourceforge.net/Debian/reference/ch-support.zh-cn.html">15</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ap-appendix.zh-cn.html">A</A>
] [ <A
href="http://qref.sourceforge.net/Debian/reference/ch-edit.zh-cn.html">下一页</A> ]
<HR>
<P>Debian 参考手册
<ADDRESS>CVS, 星期三 四月 28 19:36:38 UTC 2004<BR><BR>Osamu Aoki <CODE><A
href="mailto:osamu@debian.org">osamu@debian.org</A></CODE><BR>Translator: Hao
"Lyoo" Liu <CODE><A
href="mailto:iamlyoo@163.net">iamlyoo@163.net</A></CODE><BR><A
href="http://qref.sourceforge.net/Debian/reference/ap-appendix.zh-cn.html#s-authors">作者,
第 A.1 节</A><BR><BR></ADDRESS>
<HR>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -