Home router

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Home router and the translation is 64% complete.
Outdated translations are marked like this.
Other languages:

本文档详细说明如何将旧的Gentoo机器变成路由器以将家庭网络连接到互联网。

介绍

与购买由大公司(Linksys、D-Link、Netgear 等)制造的预制路由器相比,使用旧备件构建个人路由器具有许多优势。迄今为止最大的优势是对连接的控制。其他优点可以留给用户想象;在这种情况下几乎可以做任何事情,这只是一个需求的问题。

This guide will provide instructions on how to setup Network Address Translation (NAT) on a custom router (kernel and iptables), add and configure common services (Domain Name System (DNS) via net-dns/dnsmasq, DHCP via net-misc/dhcpcd, ADSL via net-dialup/ppp), and conclude with some elaborate and fun things custom routers are capable of (port forwarding, traffic shaping, proxies/caching, etc...).

在开始之前,请查看基本要求列表:

  1. 至少安装了 2 个网络接口卡 (NIC) 的计算机。
  2. Internet 连接的配置设置(可能包括 IP/DNS/网关/用户名/密码等)。
  3. (可选)支持主模式的 Wi-Fi 卡。建议使用主模式以避免具有旁路功能的 NIC,因为某些 NIC 需要专有驱动程序。如果无法访问专有驱动程序,该卡将永久保持旁路模式并且无法使用。
  4. 一点空闲时间和对 Gentoo 的热爱,以便成功地遵循本指南并实现一个功能良好的家庭路由器。

本指南中使用的约定包括:

  • eth0 - NIC 连接到局域网 (LAN) 或由多个 NIC 组成的网桥
  • eth1 - 连接到广域网 (WAN) 的 NIC
  • LAN 使用私有 192.168.0.xxx 网络
  • 路由器硬编码为标准的 192.168.0.1 IP 地址
重要
出于安全预防措施,强烈建议关闭路由器上所有不需要的服务直到防火墙启动。要查看当前运行的服务,请运行 rc-status.

内核设置(先了解你自己)

内核需要为系统上存在的两个 NIC 安装驱动程序。要查看网卡是否已设置,请使用 ip link 命令。输出可能与以下示例略有不同。重要的是接口存在。

root #ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether 00:60:F5:07:07:B8 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether 00:60:F5:07:07:B9 brd ff:ff:ff:ff:ff:ff

If only one (or none) of the two cards show up try running lspci | grep Ethernet.

附注
The lspci utility is part of the sys-apps/pciutils package.

Once the make(s)/model(s) of the NIC(s) has been obtained, configure the kernel with support for the correct drivers. For more information on kernel configuration see the Kernel Configuration Guide.

The next thing needed is support for IP tables and NAT (and packet shaping if desired). The following list is split up into always required (*), required only for ADSL via PPPoE (a), suggested for everyone (x), and only for shaper (s) features. It does not matter whether the features are built into the kernel or as modules as long as when the feature is needed, the correct module(s) are loaded. For more information on loading modules see the Kernel Modules Guide.

内核 Enabling the necessary networking options
-*- Networking support  --->
   Networking options  --->
      [*] TCP/IP networking
         [*] IP: advanced router
      [*] Network packet filtering framework (Netfilter)  --->
         [*]   Advanced netfilter configuration
            IPv6: Netfilter Configuration  ---> 
               <*> IPv6 connection tracking support
               <*> IP6 tables support (required for filtering)

Those who would rather search for these features directly in menuconfig can find them under the following names: CONFIG_INET, CONFIG_IP_ADVANCED_ROUTER, and CONFIG_NETFILTER, CONFIG_NETFILTER_ADVANCED, CONFIG_NF_CONNTRACK_IPV6, and CONFIG_IP6_NF_IPTABLES.

附注
Some things may be slightly different in the menuconfig interface on newer kernels, for example, CONFIG_NF_CONNTRACK_IPV6 does not exist in Kernel 5.10 and has now been consolidated into CONFIG_NF_CONNTRACK for both IPV6 and IPV4 (in the "Core Netfilter Configuration" tab)!

使用 2.4.x 内核时,必须为 DHCP 启用以下功能:

内核 网络选项
[*] Socket Filtering
  
   IP: Netfilter Configuration  --->
      [*] Connection tracking (required for masq/NAT)
         [x] FTP protocol support
         [x] IRC protocol support
      [*] IP tables support (required for filtering/masq/NAT)
         [*] IP range match support
         [x] MAC address match support
         [*] Multiple port match support
         [*] Packet filtering
            [*] REJECT target support
            [x] REDIRECT target support
         [*] Full NAT
            [*] MASQUERADE target support
         [s] Packet mangling
            [s] MARK target support
         [x] LOG target support
  
   QoS and/or fair queueing  --->
      [s] QoS and/or fair queueing
         [s] HTB packet scheduler
         [s] Ingress Qdisc
  
   [a] PPP (point-to-point protocol) support
      [a] PPP filtering
      [a] PPP support for async serial ports
      [a] PPP support for sync tty ports
      [a] PPP Deflate compression
      [a] PPP BSD-Compress compression
      [a] PPP over Ethernet
附注
Some things may be slightly different in the menuconfig interface on newer kernels, however things should not be too difficult to figure out. Good luck!

Hug the WAN (a.k.a. The Internet)

Intro

There are many ways to connect to the Internet, however there are generally only a couple of ways that are used by most of the public. ADSL (PPPoE) and cable modems (static/dynamic) are the two most common methods ISP (Internet Service Providers) provide. If there are other methods available, feel free to add them to this wiki article. Skip any of the following sections in this chapter that are not applicable to the needed use case. This chapter addresses getting the router connected to the Internet via eth1.

ADSL and PPPoE

All the fancy PPPoE software that used to be provided by rp-pppoe (Roaring Penguin) has been integrated into the standard PPP package. Simply emerge ppp to install PPPoE. Remember how username and password information was a requirement listed above? Load up /etc/conf.d/net in a favorite text editor and configure it accordingly.

Replace <username> and <user_password> in the following example with the required username with the password:

文件 /etc/conf.d/netUse ADSL over eth1 for ppp0
config_ppp0="ppp"
link_ppp0="eth1"
plugins_ppp0="pppoe"
pppd_ppp0="
  defaultroute
  usepeerdns
"
username_ppp0="<username>"
password_ppp0="<user_password>"
root #ln -s net.lo /etc/init.d/net.ppp0
root #rc-update add net.ppp0 default
root #service net.ppp0 start
警告
When the DSL interface comes up, it will create a "ppp0" entry to the output of ifconfig command. Although the NIC is called eth1, the IP address is actually bound to ppp0. From now on substitute eth1 with ppp0
警告
Be sure to change the permissions of the /etc/conf.d/net file so that only users with root privileges can read/write to it. This important because the a username and password have been entered to the file in plain text format.
警告
For users transitioning from the net-dialup/rp-pppoe package, or for users who experience weird connection resets, see the MTU section in the Troubleshooting chapter below.

Cable and/or dynamic/static IP

如果需要静态 IP,则需要其他配置详细信息。静态用户 IP 的用户需要添加 IP 地址、网关地址和 DNS 服务器地址。

动态IP用户:

root #emerge --ask net-misc/dhcpcd
文件 /etc/conf.d/net动态 IP 地址
config_eth1="dhcp"

静态IP用户:

文件 /etc/conf.d/net静态 IP 地址配置
config_eth1="66.92.78.102/24"
routes_eth1="default via 66.92.78.1"
文件 /etc/resolv.conf添加DNS信息
nameserver 123.123.123.123

动态和静态设置:

root #ln -s net.lo /etc/init.d/net.eth1
root #rc-update add net.eth1 default
root #service net.eth1 start

完成上述更改后,系统应准备好继续。

拥抱局域网(带上一些朋友)

This step is a breeze compared to the previous one. To use more than two devices (more than the one for LAN and the one for WAN), a Network bridge will need to be setup between all NICs using the LAN. This will allow multiple NICs to be reached by the same IP address.

If a network bridge will be necessary, follow the instructions to set up a Network bridge. The name of the bridge (default br0) will then replace eth0 for the LAN device in the steps in this wiki. If a large number of network devices in the home router, consider renaming them via udev to make administration easier. Setting up a bridge and renaming devices is completely optional but recommended for larger home networks.

创建 Wi-Fi 接入点时,请确保 Wi-Fi 卡支持主模式并设置 Hostapd

文件 /etc/conf.d/net
config_eth0="192.168.0.1/24"
root #rc-update add net.eth0 default
root #service net.eth0 start

LAN 服务(因为我们是好人)

DHCP 服务器

It would be nice if everyone in the house could plug their computers into the network and things would just work. No need to remember mind-numbing details or make them stare at confusing configuration screens! Life would be grand, eh? Introducing the Dynamic Host Configuration Protocol (DHCP) and why everyone should care.

DHCP is exactly what its name implies: a protocol that allows dynamic configuration of hosts automatically. Run a DHCP server on the router, give it all the information about the network (valid IPs, DNS servers, gateways, etc...), then when the other hosts start up, they can run a DHCP client to automatically configure themselves. No fuss, no muss! For more information about DHCP, visit Wikipedia's DHCP article.

This section will use the net-dns/dnsmasq package which will provide both DHCP and DNS services. For now lets focus on the DHCP aspect. Note: to run a different DHCP server, another example can be found in the Fun Things section below. Also, to tinker with the DHCP server settings read the comments in the /etc/dnsmasq.conf file.

root #emerge --ask net-dns/dnsmasq
文件 /etc/dnsmasq.conf
dhcp-range=eth0,192.168.0.100,192.168.0.250,72h
interface=eth0
root #rc-update add dnsmasq default
root #service dnsmasq start

Setting the interface is very important. Using default dnsmasq settings will open the router to DNS amplification attacks which could create some scary email from the ISP providing the connection. Check to make sure the router is not allowing for DNS amplification attacks by using [1].

Now the little router is a bona-fide DHCP server. Plug in those computers and watch them work! With Windows systems navigate to the TCP/IP Properties and select the Obtain an IP address automatically and Obtain DNS server address automatically options. Sometimes the changes are not instantaneous, so opening a command prompt and running ipconfig /release and ipconfig /renew might be necessary. Enough about Windows, time to get back to everyone's favorite penguin!

DHCP 服务器

When people want to visit a place on the Internet, they remember names, not a string of funky numbers. After all, what is easier to remember, eBay.com or 66.135.192.87? This is where the DNS steps in. DNS servers run all over the Internet, and whenever someone wants to visit eBay.com, these servers turn the text "eBay.com" (what we understand) into IP address "66.135.192.87" (what computers understand). For more information about DNS visit Wikipedia.

Since dnsmasq is being used for the DHCP server, and it includes a DNS server, there is nothing left to do here! The little router is already providing DNS to its DHCP clients. Shouldn't everything be this easy? ;)

It is possible to choose other DNS servers if they are more comfortable to setup. dnsmasq is used in this article because it was designed to do exactly what this guide required. It is a little DNS caching/forwarding server for local networks. The scope of this howto is not to provide DNS for a domain; but it does offer simple DNS services to every user of a home-based LAN.

NAT(又名IP伪装)

At this point, people on the network can talk to each other and look up hostnames via DNS, but they still cannot actually connect to the Internet. While the network administrator (the person reading this) may think it is great (more bandwidth for the Admin!), the other users are probably not very happy without an Internet connection.

This is where Network Address Translation (NAT) steps in. NAT is a way of connecting multiple computers in a private LAN to the Internet when a small number of public IP addresses are available. Typically a home Internet user is provided with 1 public IP address by an ISP for the whole house to connect to the Internet. NAT is the magic that makes this possible. For more information about NAT, please visit Wikipedia.

附注
Before getting started, make sure IPtables has been installed on the system. If it is not installed, install it: emerge iptables

After IPtables is installed, flush the current rules:

root #iptables -F
root #iptables -t nat -F

设置默认策略以处理不匹配的流量:

root #iptables -P INPUT ACCEPT
root #iptables -P OUTPUT ACCEPT
root #iptables -P FORWARD DROP

复制并粘贴以下内容:

root #export LAN=eth0
root #export WAN=eth1

下一步锁定服务,使它们只能在 LAN 中工作:

root #iptables -I INPUT 1 -i ${LAN} -j ACCEPT
root #iptables -I INPUT 1 -i lo -j ACCEPT
root #iptables -A INPUT -p UDP --dport bootps ! -i ${LAN} -j REJECT
root #iptables -A INPUT -p UDP --dport domain ! -i ${LAN} -j REJECT

(可选)允许从 WAN 访问 ssh 服务器:

root #iptables -A INPUT -p TCP --dport ssh -i ${WAN} -j ACCEPT

将 TCP / UDP 数据包丢弃到特权端口:

root #iptables -A INPUT -p TCP ! -i ${LAN} -d 0/0 --dport 0:1023 -j DROP
root #iptables -A INPUT -p UDP ! -i ${LAN} -d 0/0 --dport 0:1023 -j DROP

最后添加NAT规则:

root #iptables -I FORWARD -i ${LAN} -d 192.168.0.0/16 -j DROP
root #iptables -A FORWARD -i ${LAN} -s 192.168.0.0/16 -j ACCEPT
root #iptables -A FORWARD -i ${WAN} -d 192.168.0.0/16 -j ACCEPT
root #iptables -t nat -A POSTROUTING -o ${WAN} -j MASQUERADE

通知内核 IP 转发正常:

root #echo 1 > /proc/sys/net/ipv4/ip_forward
root #for f in /proc/sys/net/ipv4/conf/*/rp_filter ; do echo 1 > $f ; done

指示 IPtables 守护进程保存对规则的更改,然后将 IPtables 添加到默认运行级别:

root #/etc/init.d/iptables save
root #rc-update add iptables default
文件 /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1

对于动态 Internet,应启用以下设置:

文件 /etc/sysctl.conf
net.ipv4.ip_dynaddr = 1

输入上述文本后,其他网络用户现在应该可以像自己直接连接一样使用 Internet。

The ip_dynaddr option is useful for dial on demand systems or when the ISP gives out dynamic addresses. This works around the problem where a connection is attempted before the Internet interface is fully setup. This provides a smoother network experience for users behind the router.

有趣的事情(在一个下雨天)

介绍

信不信由你,它已经完成了!从现在开始,将涵盖一些你可能感兴趣的其他常见主题。以下部分内容都是完全可选的。

端口转发

Sometimes users need to be able to host services on a computer behind the router, or need to be able to connect remotely to a computer behind the router. Perhaps a FTP, HTTP, SSH, or VNC server is needed on one or more machines behind the router and outsiders need to connect to them all. The only caveat to Port Forwarding is only one service/machine combo can be established per port. For example, there is no practical way to setup three FTP servers behind the router and connect to them all through port 21; only one system can be on port 21 while the others would need to be on other ports (port 123 and port 567 would be fine options).

All the port forwarding rules are of the form iptables -t nat -A PREROUTING -p [protocol] --dport [external port on router] -i ${WAN} -j DNAT --to [ip/port to forward to]. Unfortunately, iptables does not accept hostnames when port forwarding. When forwarding an external port to the same port on the internal machine, omit the destination port. See the iptables(8) man page for more information.

root #export LAN=eth0
root #export WAN=eth1

将端口 2 转发到内部主机上的 ssh:

root #iptables -t nat -A PREROUTING -p tcp --dport 2 -i ${WAN} -j DNAT --to 192.168.0.2:22

FTP 转发到内部主机:

root #iptables -t nat -A PREROUTING -p tcp --dport 21 -i ${WAN} -j DNAT --to 192.168.0.56

HTTP 转发到内部主机:

root #iptables -t nat -A PREROUTING -p tcp --dport 80 -i ${WAN} -j DNAT --to 192.168.0.56

内部主机的 VNC 转发:

root #iptables -t nat -I PREROUTING -p tcp --dport 5900 -i ${WAN} -j DNAT --to 192.168.0.2
root #iptables -t nat -I PREROUTING -p tcp --dport 5901 -i ${WAN} -j DNAT --to 192.168.0.3:5900
附注
VNC clients by default connect to port 5900. If a different port needs to be selected, most VNC clients use a suffix to the connection address which will be added to 5900 to find the final port. So, in the above case, to VNC in to 192.168.0.3, add :1 to the target IP address (which is the WAN address of the router) so that the VNC client connects to port 5901.

SAMBA 转发到内部主机(覆盖 Windows 的多余端口):

root #iptables -t nat -I PREROUTING -p tcp --dport 135 -i ${WAN} -j DNAT --to 192.168.0.2
root #iptables -t nat -I PREROUTING -p tcp --dport 139 -i ${WAN} -j DNAT --to 192.168.0.2
root #iptables -t nat -I PREROUTING -p tcp --dport 445 -i ${WAN} -j DNAT --to 192.168.0.2
root #iptables -t nat -I PREROUTING -p udp --dport 137:138 -i ${WAN} -j DNAT --to 192.168.0.2
root #iptables -t nat -I PREROUTING -p udp --dport 445 -i ${WAN} -j DNAT --to 192.168.0.2

Bittorrent 转发:

root #iptables -t nat -A PREROUTING -p tcp --dport 6881:6889 -i ${WAN} -j DNAT --to 192.168.0.2

eDonkey/eMule 转发:

root #iptables -t nat -A PREROUTING -p tcp --dport 4662 -i ${WAN} -j DNAT --to 192.168.0.55

Game Cube Warp Pipe support:

root #iptables -t nat -A PREROUTING -p udp --dport 4000 -i ${WAN} -j DNAT --to 192.168.0.56

Playstation 2 在线支持:

root #iptables -t nat -A PREROUTING -p tcp --dport 10070:10080 -i ${WAN} -j DNAT --to 192.168.0.11
root #iptables -t nat -A PREROUTING -p udp --dport 10070:10080 -i ${WAN} -j DNAT --to 192.168.0.11

Xbox Live:

root #iptables -t nat -A PREROUTING -p tcp --dport 3074 -i ${WAN} -j DNAT --to 192.168.0.69
root #iptables -t nat -A PREROUTING -p udp --dport 3074 -i ${WAN} -j DNAT --to 192.168.0.69
root #iptables -t nat -A PREROUTING -p udp --dport 88 -i ${WAN} -j DNAT --to 192.168.0.69

Identd (用于 IRC)

Internet Relay Chat utilizes the ident service pretty heavily. Now that the IRC clients are behind the router, a way to host ident for both the router and the clients is needed. A server has been created for this purpose. It is called net-misc/midentd.

root #emerge --ask net-misc/midentd
root #rc-update add midentd default
root #service midentd start

Portage 树中还有一些其他身份的服务器。其他可行的选项是 net-misc/oidentdnet-misc/fakeidentd

时间服务器

Keeping the system time correct is essential to maintaining a healthy system. One of the most common ways of accomplishing this is with the Network Time Protocol (NTP) and the net-misc/ntp package (which provides implementations for both server and client).

Many users run ntp clients on their computers. Obviously, the more clients in the world, the larger the load ntp servers need to shoulder. In environments like home networks an NTP server can be setup locally to help keep the load down on public servers while still providing the proper time to local systems. As an added bonus, private updates will be a lot faster for the local clients! The setup is simple: run a NTP server on the router that synchronizes itself with the public Internet servers while, at the same time, providing the time to the rest of the computers in the network. To get started, simply emerge ntp on the router and edit /etc/conf.d/ntp-client as desired.

root #rc-update add ntp-client default
文件 /etc/ntp.conf
restrict default ignore
restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap

这些将仅允许 IP 地址在 192.168.0.xxx 范围内的 NTP 客户端使用 NTP 服务器。

root #rc-update add ntpd default
root #service ntpd start
附注
Make sure to allow inbound and outbound communication on the NTP port (123/udp) when setting up the server. The client just needs outbound access on port 123 over UDP.

Now, on the clients, run emerge ntp. By running the NTP client setup is a lot simpler.

/etc/conf.d/ntp-client 中,将 NTPCLIENT_OPTS 变量中的 pool.ntp.org 服务器更改为 192.168。 0.1

root #rc-update add ntp-client default
root #service ntp-client start

同步服务器

For those who run multiple Gentoo boxes on the same LAN, it is wise to prevent every machine running emerge --sync with remote servers. By setting up a local rsync, both personal bandwidth and the Gentoo rsync servers' bandwidth is saved. The process is relatively simple.

附注
For a much more in-depth rsync guide, please see the official rsync guide.

Since every Gentoo machine requires rsync, there is no need to emerge it. Edit the default /etc/rsyncd.conf config file, uncomment the [gentoo-portage] section, and make sure to add an address option. All other defaults should be already set correctly.

文件 /etc/rsyncd.conf
pid file = /var/run/rsyncd.pid
use chroot = yes
read only = yes
address = 192.168.0.1
  
[gentoo-portage]
  path = /mnt/space/portage
  comment = Gentoo Linux Repository
  exclude = /distfiles /packages

然后需要启动该服务(同样,默认值是可以的)。

root #service rsyncd start
root #rc-update add rsyncd default

Only thing left is to set tell the clients to sync against the router. Edit the /etc/portage/repos.conf/gentoo.conf config file. If this file does not exist, make a directory under /etc/portage called repos.conf and copy the /usr/share/portage/config/repos.conf file into this directory. Then update this file accordingly - don't forget to set sync-uri to match the address of the server (the home router).

文件 /etc/portage/repos.conf/gentoo.confSetup client to new rsync server
[DEFAULT]
main-repo = gentoo
 
[gentoo]
location = /var/db/repos/gentoo
sync-type = rsync
sync-uri = rsync://192.168.0.1/gentoo-portage
auto-sync = yes

邮件服务器

Sometimes it is nice to run a Simple Mail Transfer Protocol (SMTP) server on the router. Each user may have their own reason for wanting to do so, however one advantage to running SMTP on the router is the users see mail as being sent instantly and the work of retrying/routing is left up to the mail server. Some ISPs do not allow for mail relaying for accounts that are not part of their network (like Verizon). Also, throttling the delivery of mail may be needed so that large attachments will not seriously lag the Internet connection.

root #emerge --ask mail-mta/netqmail

确保 hostname 命令的输出正确:

root #emerge --config netqmail
root #iptables -I INPUT -p tcp --dport smtp ! -i ${LAN} -j REJECT
root #ln -s /var/qmail/supervise/qmail-send /service/qmail-send
root #ln -s /var/qmail/supervise/qmail-smtpd /service/qmail-smtpd
root #cd /etc/tcprules.d

编辑 tcp.qmail-smtp 并在允许部分添加如下条目:

文件 niltcp.qmail-smtp
192.168.0.:allow,RELAYCLIENT=""
root #make
root #rc-update add svscan default
root #service svscan start

When e-mail is setup on the hosts in the network, tell them the SMTP server is 192.168.0.1. Visit the netqmail homepage for more documentation on netqmail usage.

完整的 DHCP 服务器

Earlier dnsmasq was used to provide DHCP service to all DHCP clients. For most people with a simple small LAN, this is perfect, however there may needs something with more features. Thus a full-featured DHCP server is provided by the ISC folks for users who crave the maximum.

root #emerge --ask net-misc/dhcp
文件 /etc/dhcp/dhcpd.conf这是一个简单的配置文件
authoritative;
ddns-update-style interim;
subnet 192.168.0.0 netmask 255.255.255.0 {
    pool {
        range 192.168.0.100 192.168.0.250;
        default-lease-time 259200;
        max-lease-time 518400;
        option subnet-mask 255.255.255.0;
        option broadcast-address 192.168.0.255;
        option routers 192.168.0.1;
        option domain-name-servers 192.168.0.1;
    }
}

/etc/conf.d/dhcpd 中,将 IFACE 设置为“eth0”。

root #rc-update add dhcpd default
root #service dhcpd start

This is the minimal setup required to replace the dnsmasq DHCP functionality used earlier. The DHCP features in dnsmasq should be disabled? If not, comment out the dhcp-range setting in /etc/dnsmasq.conf and restart the service.

连接另一个局域网(或两个或三个)

Sometimes the router must be connected to another LAN. This can be done to hook up a group of friends temporarily or to section off different groups of computers. Whatever the reason, extending the router to other LAN networks should is straightforward. In the following examples, This article presumes that the new network is connected via a third ethernet card, namely eth2.

First configure the interface. Take the instructions in this section and replace config_eth0="192.168.0.1/24" with config_eth2="192.168.1.1/24".

Tweak dnsmasq to service the new interface. Edit the /etc/conf.d/dnsmasq file again and append -i eth2 to DNSMASQ_OPTS; using -i multiple times is OK. Then edit /etc/dnsmasq.conf and add another line like the dhcp-range line in this section, replacing dhcp-range=eth0,192.168.0.100,192.168.0.250,72h with dhcp-range=eth2,192.168.1.100,192.168.1.250,72h and replace interface=eth0 with interface=eth2. Having multiple dhcp-range and interface lines is OK too.

Finally, see the rules in this section and duplicate the rules that have -i ${LAN} in them. Another variable may need to be created, say LAN2, to make things easier.

故障排除

有用的工具

当无法让计算机进行通信时,请尝试以下工具(它们都可以在 Portage 的 net-analyzer 类别中找到):

Utility Description
net-analyzer/wireshark GUI tool to view all raw network data according to filters
net-analyzer/tcpdump Console tool to dump all raw network data according to filters
net-analyzer/iptraf-ng ncurses based IP LAN monitor
net-analyzer/ettercap ncurses based network monitor/control

DHCP 无法启动

When starting the dhcp init.d script for the first time, it may fail to load but neglect to provide any useful information.

root #service dhcp start
 * Setting ownership on dhcp.leases ...          [ ok ]
 * Starting dhcpd ...                            [ !! ]

The trick is used to know where dhcpd is sending its output. Browse to /var/log and read the log files. Since the exact log file depends on the package using a syslog, try running grep -Rl dhcpd /var/log to narrow down the possibilities. Chances are a typo was made in the configuration file. Another command to try running: dhcpd -d -f (short for debug / foreground). This aids in debugging the errors based upon the output.

不正确的 MTU 值

If odd errors are experienced (such as not being able to access some webpages while others load fine), it might be Path MTU Discovery trouble. The quick way to test for this is to run the following iptables command:

root #iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu

This will affect all new connections; refresh the problematic website in order to test the fix. In case it helps, the standard MTU value for 100mbit ethernet connections is 1500; this value also applies to PPPoA. For PPPoE connections it is 1492. For more info, read Chapter 15 of the Linux Advanced Routing & Traffic Control HOWTO.

If the above command does not work, consider putting the rule into the mangle table. Simply add -t mangle to the command.

无法直接连接两台机器

If (for whatever reason) connecting two machines directly together without a hub or switch is required, a regular ethernet cable will likely not work, unless an Auto MDI/MDI-X (also known as "autosensing") capable network adapter is available. A different cable called a crossover cable will be needed for direct NIC to NIC connections. This Wikipedia page explains the low level details.

最后的笔记

There are no other final notes. If any troubles with this guide are experienced either update this article with the correct information or leave a brief message on this article's talk page with a summary of what is broken. Eventually someone should be able to correct any issue(s). It is also possible to file a bug on Gentoo's Bugtracking Website. If there are any other interesting bits that would enhance this guide, by all means include them! The worst that could happen is they could be removed.


This page is based on a document formerly found on our main website gentoo.org.
The following people contributed to the original document: Mike Frysinger (vapier)
They are listed here because wiki history does not allow for any external attribution. If you edit the wiki article, please do not add yourself here; your contributions are recorded on each article's associated history page.