个人工具

“Quick HOWTO : Ch08 : Configuring the DHCP Server/zh”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
总结
 
(未显示2个用户的16个中间版本)
第1行: 第1行:
{{Translation}}
+
{{Copyedit translation}}
  
 
{{From|http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch08_:_Configuring_the_DHCP_Server}}
 
{{From|http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch08_:_Configuring_the_DHCP_Server}}
 +
 +
{{Translator|Dbzhang800 name Vitoking}}
  
 
{{Languages|Quick HOWTO : Ch08 : Configuring the DHCP Server}}
 
{{Languages|Quick HOWTO : Ch08 : Configuring the DHCP Server}}
第10行: 第12行:
 
这一章仅包含对一个可以提供IP地址的DHCP服务器的配置指南。关于Linux系统中,DHCP客户端从DHCP服务器端如何获取IP地址的配置可以参考第三章,“[[Quick HOWTO : Ch03 : Linux Networking/zh|Linux Networking]]”中的Linux网络互联部分。
 
这一章仅包含对一个可以提供IP地址的DHCP服务器的配置指南。关于Linux系统中,DHCP客户端从DHCP服务器端如何获取IP地址的配置可以参考第三章,“[[Quick HOWTO : Ch03 : Linux Networking/zh|Linux Networking]]”中的Linux网络互联部分。
  
= Download and Install the DHCP Package =
+
= 下载与安装 DHCP =
  
Most RedHat and Fedora Linux software products are available in the RPM format. Downloading and installing RPMs aren't hard. If you need a refresher, Chapter 6, "[[Quick HOWTO : Ch06 : Installing Linux Software|Installing Linux Software]]", covers how to do this in detail.  
+
许多 RedHat 以及 Fedora Linux 软件都是RPM格式的. 下载安装这些RPM软件很容易. 如果你想再回顾下如何安装, Chapter 6, "[[Quick HOWTO : Ch06 : Installing Linux Software|Installing Linux Software]]", 详细讲解了这部分内容.  
  
When searching for the file, remember that the DHCP server RPM's filename usually starts with the word dhcp followed by a version number like this: dhcp-3.0.1rc14-1.i386.rpm.
+
找该软件包的时候,请注意DHCP服务器的RPM包的命名往往以dhcp开始,后跟一个版本号,比如这样:dhcp-3.0.1rc14-1.i386.rpm
  
'''Debian Note:''' With Debian / Ubuntu the package name may include a version number. Use the <code>dpkg --list | grep dhcp</code> command to get a list of all your dhcp packages and use the output to infer what the DHCP server package name would be. In this case we can guess that the package name should be <code>dhcp3-server</code>. If you need a DEB package installation refresher you can take a look at Chapter 6, "[[Quick HOWTO : Ch06 : Installing Linux Software|Installing Linux Software]]".
+
'''Debian Note:''' Debian / Ubuntu 来说软件包名称可能也包含版本号. <code>dpkg --list | grep dhcp</code> 来的到一个所有dhcp软件包的列表,从而找到dhcp服务区的软件包名称. 这里我们不妨认为这个软件包是 <code>dhcp3-server</code>.如果想回顾一下DEB包的安装请参见 Chapter 6, "[[Quick HOWTO : Ch06 : Installing Linux Software|Installing Linux Software]]".
  
 
  root@u-bigboy:/tmp# dpkg --list | grep dhcp
 
  root@u-bigboy:/tmp# dpkg --list | grep dhcp
第23行: 第25行:
 
  root@u-bigboy:/tmp#
 
  root@u-bigboy:/tmp#
  
= The /etc/dhcpd.conf File =
+
= /etc/dhcpd.conf 文件 =
  
  
When DHCP starts, it reads the file /etc/dhcpd.conf. It uses the commands here to configure your network. The standard DHCP RPM package doesn't automatically install a /etc/dhcpd.conf file, but you can find a sample copy of dhcpd.conf in the following directory which you can always use as a guide.
+
DHCP 服务器启动时,它读取 /etc/dhcpd.conf文件,并使用里面的命令来配置你的网络。标准的DHCP RPM软件包不会安装这个文件,但是你可以在下面的目录里找到一个配置文件示例作为参照。
  
 
  /usr/share/doc/dhcp-<version-number>/dhcpd.conf.sample
 
  /usr/share/doc/dhcp-<version-number>/dhcpd.conf.sample
  
You have to copy the sample dhcpd.conf file to the /etc directory and then you'll have to edit it. Here is the command to do the copying for the version 3.0p11 RPM file:
+
你需要拷贝这个文件 dhcpd.conf /etc 目录下面然后进行编辑。对于3.0p11版本的RPM包,拷贝文件命令如下:
  
 
  [root@bigboy tmp]# cp /usr/share/doc/dhcp-3.0pl1/dhcpd.conf.sample /etc/dhcpd.conf
 
  [root@bigboy tmp]# cp /usr/share/doc/dhcp-3.0pl1/dhcpd.conf.sample /etc/dhcpd.conf
  
'''Debian Note''': With Debian / Ubuntu the configuration file name is <code>/etc/dhcp*/dhcpd.conf</code> and has the same syntax as that used by Redhat / Fedora.
+
'''Debian Note''': Debian / Ubuntu 来说配置文件是 <code>/etc/dhcp*/dhcpd.conf</code> ,语法跟 Redhat / Fedora一样.
  
Here is a quick explanation of the dhcpd.conf file: Most importantly, there must be a subnet section for each interface on your Linux box.
+
下面是对dhcpd.conf文件的简单解释:最重要的是,你的Linux主机的每个网络接口都必须有一个子网段
  
 
  ddns-update-style interim
 
  ddns-update-style interim
第43行: 第45行:
 
  subnet 192.168.1.0 netmask 255.255.255.0 {
 
  subnet 192.168.1.0 netmask 255.255.255.0 {
 
    
 
    
     # The range of IP addresses the server
+
     # 当DHCP客户端主机启动网络时
     # will issue to DHCP enabled PC clients
+
     # 服务器能够分配给他的IP地址
     # booting up on the network
+
     # 的范围
 
    
 
    
 
     range 192.168.1.201 192.168.1.220;
 
     range 192.168.1.201 192.168.1.220;
 
    
 
    
     # Set the amount of time in seconds that
+
     # 客户端能使用该IP地址的时间
     # a client may keep the IP address
+
     # 以秒计算
 
   
 
   
 
   default-lease-time 86400;
 
   default-lease-time 86400;
 
   max-lease-time 86400;
 
   max-lease-time 86400;
 
    
 
    
     # Set the default gateway to be used by
+
     # 客户端默认网关
    # the PC clients
+
 
    
 
    
 
     option routers 192.168.1.1;
 
     option routers 192.168.1.1;
    # Don't forward DHCP requests from this
+
 
     # NIC interface to any other NIC
+
     # 不从一个网口向另一个网口转发
     # interfaces
+
     # DHCP请求
 
    
 
    
 
     option ip-forwarding off;
 
     option ip-forwarding off;
 
    
 
    
     # Set the broadcast address and subnet mask
+
     # 设置客户端广播地址和子网掩码
    # to be used by the DHCP clients
+
 
   
 
   
 
   option broadcast-address 192.168.1.255;
 
   option broadcast-address 192.168.1.255;
 
   option subnet-mask 255.255.255.0;
 
   option subnet-mask 255.255.255.0;
 
    
 
    
     # Set the DNS server to be used by the
+
     # 设置客户端DNS服务器
    # DHCP clients
+
 
   
 
   
 
   option domain-name-servers 192.168.1.100;
 
   option domain-name-servers 192.168.1.100;
 
    
 
    
     # Set the NTP server to be used by the
+
     # 设置客户端NTP服务器
    # DHCP clients
+
 
    
 
    
 
     option nntp-server 192.168.1.100;
 
     option nntp-server 192.168.1.100;
 
    
 
    
     # If you specify a WINS server for your Windows clients,
+
     # 如果你为Windows客户端指定了一个WINS服务器,
     # you need to include the following option in the dhcpd.conf file:
+
     # 你必须在 dhcpd.conf中加入以下选项
 
   
 
   
 
   option netbios-name-servers 192.168.1.100;
 
   option netbios-name-servers 192.168.1.100;
 
    
 
    
     # You can also assign specific IP addresses based on the clients'
+
     # 你也可以根据客户端MAC地址分配给他静态IP
     # ethernet MAC address as follows (Host's name is "laser-printer":
+
     # (主机名是 "laser-printer"):
 
   
 
   
 
   host laser-printer {
 
   host laser-printer {
第95行: 第93行:
 
  }
 
  }
 
  #
 
  #
  # List an unused interface here
+
  # 未使用网络接口
 
  #
 
  #
 
  subnet 192.168.2.0 netmask 255.255.255.0 {
 
  subnet 192.168.2.0 netmask 255.255.255.0 {
 
  }
 
  }
  
There are many more options statements you can use to configure DHCP. These include telling the DHCP clients where to go for services such as finger and IRC. Check the dhcp-options man page after you do your install:
+
还有许多其他选项来配置 DHCP, 包括告诉 DHCP客户端哪里提供 finger 以及 IRC服务。在安装后请查看 dhcp-options man page :
  
 
  [root@bigboy tmp]# man dhcp-options
 
  [root@bigboy tmp]# man dhcp-options
  
'''Note:''' The host statement seen in the sample dhcpd.conf file can be very useful. Some devices such as network printers default to getting their IP addresses using DHCP, but users need to access them by a fixed IP address to print their documents. This statement can be used to always provide specific IP address to DHCP queries from a predefined a NIC MAC address. This can help to reduce systems administration overhead.
+
'''Note:''' 示例文件dhcpd.conf文件里面的host指令非常有用. 像网络上打印机这样一些设备默认是通过DHCP获得IP地址的, 但是用户可以用固定IP地址区访问. 这个指令可以用来为一个已经预先知道网卡MAC地址的DHCP客户端提供静态IP地址。这可以降低系统管理开销
  
 
= 启动DHCP: =
 
= 启动DHCP: =
第110行: 第108行:
 
1) DHCP的一些旧版本的Fedora/RedHat将无法使用除非已有dhcpd.leases文件。如果没有的话可以用命令 touch /var/lib/dhcp/dhcpd.leases&nbsp;去创建一个。&nbsp;
 
1) DHCP的一些旧版本的Fedora/RedHat将无法使用除非已有dhcpd.leases文件。如果没有的话可以用命令 touch /var/lib/dhcp/dhcpd.leases&nbsp;去创建一个。&nbsp;
  
[root@bigboy tmp]# touch /var/lib/dhcp/dhcpd.leases<br>2) Use the chkconfig command to get DHCP configured to start at boot:
+
[root@bigboy tmp]# touch /var/lib/dhcp/dhcpd.leases<br>2) 使用chkconfig命令配置DHCP在电脑启动时自动运行;
  
 
  [root@bigboy tmp]# chkconfig dhcpd on
 
  [root@bigboy tmp]# chkconfig dhcpd on
  
With Debian / Ubuntu the equivalent command for the <code>dhcp3-server</code> package would be:
+
Debian / Ubuntu,<code>dhcp3-server</code> 软件包相应的命令为:
  
 
  root@u-bigboy:/tmp# sysv-rc-conf dhcp3-server on
 
  root@u-bigboy:/tmp# sysv-rc-conf dhcp3-server on
  
  
3) Use the service command to instruct the /etc/init.d/dhcpd script to start/stop/restart DHCP after
+
3) 然后用 service 命令执行 /etc/init.d/dhcpd 脚本来 start/stop/restart (启动/停止/重新起动) DHCP
  
  
booting
+
启动
  
 
  [root@bigboy tmp]# service dhcpd start
 
  [root@bigboy tmp]# service dhcpd start
第128行: 第126行:
 
[root@bigboy tmp]# service dhcpd restart
 
[root@bigboy tmp]# service dhcpd restart
  
With Debian / Ubuntu the equivalent commands would be:
+
Debian / Ubuntu 相应命令是:
  
 
  root@u-bigboy:/tmp# /etc/init.d/dhcp*-server start
 
  root@u-bigboy:/tmp# /etc/init.d/dhcp*-server start
第134行: 第132行:
 
root@u-bigboy:/tmp# /etc/init.d/dhcp*-server restart
 
root@u-bigboy:/tmp# /etc/init.d/dhcp*-server restart
  
4) Remember to restart the DHCP process every time you make a change to the conf file for the changes to take effect on the running process. You also can test whether the DHCP process is running with the following command; you should get a response of plain old process ID numbers:
+
4)每次更改配置文件后,要重启DHCP服务以使更改生效。你也可以用下面的命令测试DHCP是否在运行,如果是则返回DHCP进程的ID;
  
 
  [root@bigboy tmp]# pgrep dhcpd
 
  [root@bigboy tmp]# pgrep dhcpd
第144行: 第142行:
 
address via DHCP.
 
address via DHCP.
  
= DHCP Servers with Multiple NICs =
+
= 有多个网卡的DHCP 服务器 =
  
  
When a DHCP configured PC boots, it requests its IP address from the DHCP server. It does this by sending a standardized DHCP broadcast request packet to the DHCP server with a source IP address of 255.255.255.255.
+
当一台被配置为动态获得IP(DHCP)的电脑启动时,他向DHCP服务器申请一个IP地址。这是通过向DHCP服务器发送一个标准的DHCP申请实现的。这个申请是一个广播桢,源IP地址为255.255.255.255
  
If your DHCP server has more than one interface, you have to add a route for this 255.255.255.255 address so that it knows the interface on which to send the reply; if not, it sends it to the default gateway. (In both of the next two examples, we assume that DHCP requests will be coming in on interface eth0).
+
如果你的DHCP服务器有多个网络接口你必须为这个255.255.255.255添加一条路由表项,让服务器进程知道往哪个接口发送回应;否则,回应会被发往默认网关(下面两个例子中,我们假设DHCP请求来自接口eth0)
  
'''Note:''' More information on adding Linux routes and routing may be found in Chapter 3, "[[Quick HOWTO : Ch03 : Linux Networking|Linux Networking]]".
+
'''Note:''' 更多关于路由以及Linux添加路由表项的信息参见 Chapter 3, "[[Quick HOWTO : Ch03 : Linux Networking|Linux Networking]]".
  
'''Note:''' You can't run your DHCP sever on multiple interfaces because you can only have one route to network 255.255.255.255. If you try to do it, you'll discover that DHCP serving working on only one interface.
+
'''Note:''' 你不能在多个网口上运行多个DHCP服务器,因为网络255.255.255.255只能有一个路由表项。如果你一定要这样,你会发现DHCP服务器只在一个网口上工作。
  
  
== Temporary Solution ==
+
== 临时解决方案 ==
  
  
You can temporarily add a route to 255.255.255.255 using the route add command as seen below.
+
你可以使用route add 命令临时为 255.255.255.255添加一个路由项,如下。
  
 
  [root@bigboy tmp]# route add -host 255.255.255.255 dev eth0
 
  [root@bigboy tmp]# route add -host 255.255.255.255 dev eth0
  
If you want this routing state to be maintained after a reboot, then use the permanent solution that's discussed next.
+
如果你想让该路由项重启后仍能使用,那用下面的永久解决方案
  
  
== Permanent Solution ==
+
== 永久解决方案 ==
  
  
The new Fedora Linux method of adding static routes doesn't seem to support sending traffic out an interface that's not destined for a specific gateway IP address. The DHCP packet destined for address 255.255.255.255 isn't intended to be relayed to a gateway, but it should be sent using the MAC address of the DHCP client in the Ethernet frame.
+
新的Fedora Linux添加静态路由的方法不支持将数据发往一个IP地址不是某一网关IP的接口。对地址为255.255.255.255的DHCP数据包的响应不应该发送给网关,而应该使用以太网帧中的MAC地址发送。
  
You have one of two choices. Add the route add command to your /etc/rc.local script, or add an entry like this to your /etc/sysconfig/static-routes file.
+
你有两个选择。往 /etc/rc.local 中加入一条命令, 或者往 /etc/sysconfig/static-routes 中添加一项,如下:
 
   
 
   
 
  #
 
  #
第178行: 第176行:
 
  eth0 host 255.255.255.255
 
  eth0 host 255.255.255.255
  
'''Note:''' The /etc/sysconfig/static-routes file is a deprecated feature and Fedora support for it will eventually be removed.
+
'''Note:''' /etc/sysconfig/static-routes 是被声明为不赞成的(deprecated),最终会被Fedora去除
  
Now that you have configured your server, it's time to take a look at the DHCP clients.
+
现在已经配置好服务器了,谈谈客户端吧。
  
= Configuring Linux Clients to Use DHCP =
+
= 配置客户端使用DHCP =
  
  
A Linux NIC interface can be configured to obtain its IP address using DHCP with the examples outlined in , "[[Quick HOWTO : Ch03 : Linux Networking|Linux Networking]]". Please refer to this chapter if you need a quick refresher on how to configure a Linux DHCP client.
+
Linux网络接口可以配置为通过DHCP获取IP地址,参考"[[Quick HOWTO : Ch03 : Linux Networking|Linux Networking]]"。如果你需要回顾DHCP客户端的配置,请查阅这一章。
  
= Configuring Windows Clients to Use DHCP =
 
  
Fortunately Windows defaults to using DHCP for all its NIC cards so you don't have to worry about doing any reconfiguration.
+
= 配置Windows客户端使用 DHCP =
  
= Using a Single DHCP Server to Serve Multiple Networks =
+
Windows默认对所有网口使用DHCP,所以你不用担心需要重新配置
  
As stated before, DHCP clients send their requests for IP addresses to a broadcast address which is limited to the local LAN. This would imply that a DHCP server is required on each subnet. Not so.
+
= 使用一个DHCP服务器为多个网络服务 =
It is possible to configure routers to forward DHCP requests to a DHCP server many hops away. This is done by inserting the IP address of the router's interface on the DHCP client's network into the forwarded packet. To the DHCP server, the non-blank router IP address field takes precedence over the broadcast address and it uses this value to provide a DHCP address that is meaningful to the client. The DHCP server replies with a broadcast packet, and the router, which has kept track of the initial forwarded request, forwards it back towards the client.
+
 
You can configure this feature on Cisco devices by using the ip helper-address command on all the interfaces on which DHCP clients reside. Here is a configuration sample that points to a DHCP server with the IP address 192.168.36.25:
+
如前所述,DHCP客户端发送一个请求IP地址的广播桢,而广播桢是被限制在局域网里面的。这本来意味着每个子网都需要一个DHCP服务器。但是,可以通过配置路由器向许多跳之外的DHCP服务器转发DHCP请求。这是通过将路由器在DHCP客户端接口的IP地址插入到转发的桢里面。对于DHCP服务器,非空的路由器的IP地址优先于广播地址考虑,并向该地址提供对DHCP客户端很重要的IP地址。DHCP服务器用一个广播桢响应(''译者注:怎么是广播桢呢?应该发给当初转发DHCP请求的路由器才对,原文如此:The DHCP server replies with a broadcast packet''), 而记录了原先的DHCP请求的路由器再把这个响应桢转发给DHCP客户端。
 +
你可以用 ip helper-address 命令让Cisco路由器在所有有DHCP客户端的接口中启用该功能。下面是一个配置实例,DHCP服务器的地址为192.168.36.25:
  
 
  interface FastEthernet 2/1
 
  interface FastEthernet 2/1
第201行: 第199行:
 
   ip helper-address 192.168.36.25
 
   ip helper-address 192.168.36.25
  
= Simple DHCP Troubleshooting =
+
= 简单的 DHCP 故障排解 =
 +
 
 +
 
 +
DHCP最常见的问题常跟服务器无关;在服务器端被正确配置之后就没必要作任何更改,服务器会运行的很好。问题常常由客户端各种各样的原因导致。接下来部分讲述了简单的故障排除步骤。执行这些步骤使DHCP在你的网络上正常工作。
 +
 
 +
 
 +
== DHCP 客户端包含 169.254.0.0 地址 ==
 +
 
  
 +
当Microsoft DHCP 客户端找不到DHCP服务器,他们就会默认从169.254.0.0网络中选择一个IP地址,直到找到DHCP服务器为止,这常被称为Automatic Private IP Addressing(APIPA)。你可以采取一下步骤解决该问题:
  
The most common problems with DHCP usually aren't related to the server; after the server is configured correctly there is no need to change any settings and it therefore runs reliably. The problems usually occur at the DHCP client's end for a variety of reasons. The following sections present simple troubleshooting steps that you can go through to ensure that DHCP is working correctly on your network.
+
* 确定DHCP服务器配置正确并使用先前讲到的 pgrep 命令来确定DHCP服务器进程在运行。特别注意 255.255.255.255 路由项,尤其当你有多个网口时。
 +
* 从DHCP服务器能够分配的IP地址里面选一个给DHCP客户端,然后看客户端能不能ping通DHCP服务器。如果不能,再次检查线缆与网卡。
 +
* DHCP使用 BOOTP 协议在客户端与服务器间交换信息。确定防火墙没有阻止通信。DHCP服务器在UDP端口67接收请求,DHCP客户端在UDP端口68等待响应,在服务器网卡上使用 <code>tcpdump</code> 检查通信数据流是否正确。
  
 +
==
 +
==
 +
==
 +
== DHCP的其它错误 ==
  
== DHCP Clients Obtaining 169.254.0.0 Addresses ==
+
如果你的DHCP服务器无法启动,可以使用第四章所描述的查错纠错技术,“常见网络错误及其解决方法”,来帮助纠正错误。 以下原因常导致初始化时的一些错误:
  
 +
* Incorrect settings in the /etc/dhcpd.conf file such as not defining the networks for which the DHCP server is responsible;
 +
*  /etc/dhcpd.conf 文件配置错误,例如没有定义DHCP服务器负责的网络。
  
Whenever Microsoft DHCP clients are unable to contact their DHCP server they default to selecting their own IP address from the 169.254.0.0 network until the DHCP server becomes available again. This is frequently referred to as Automatic Private IP Addressing (APIPA). Here are some steps you can go through to resolve the problem:
+
* Firewall rules that block the DHCP bootp protocol on UDP ports 67 and 68;
 +
* 防火墙阻止了依赖于UDP端口67和68的 DHCP bootp 协议;
  
* Ensure that your DHCP server is configured correctly and use the pgrep command discussed earlier to make sure the DHCP process is running. Pay special attention to your 255.255.255.255 route, especially if your DHCP server has multiple interfaces.
+
* Routers failing to forward the bootp packets to the DHCP server when the clients reside on a separate network.
* Give your DHCP client a static IP address from the same range that the DHCP server is supposed to provide. See whether you can ping the DHCP server. If you cannot, double-check your cabling and your NIC cards.
+
* 当客户端位于另外一个网络时,路由器没有转发DHCP服务器的bootp数据包
* DHCP uses the BOOTP protocol for its communication between the client and server. Make sure there are no firewalls blocking this traffic. DHCP servers expect requests on UDP port 67 and the DHCP clients expect responses on UDP port 68. Use <code>tcpdump</code> on the server's NIC to verify the correct traffic flows.
+
  
==Other DHCP Failures==
+
经常地查看/var/logs/messages文件以确定是否有DHCP错误发生,同时记得当你升级操作系统的时候寄存在配置文件里的关键字可能改变。经常检阅版本发布通知以确保关键字没有改变。
If the DHCP server fails to start then use your regular troubleshooting techniques outlined in Chapter 4, "[[Quick HOWTO : Ch04 : Simple Network Troubleshooting|Simple Network Troubleshooting]]", to help rectify your problems.
+
Most problems with an initial setup are often due to:
+
*Incorrect settings in the /etc/dhcpd.conf file such as not defining the networks for which the DHCP server is responsible;
+
*Firewall rules that block the DHCP bootp protocol on UDP ports 67 and 68;
+
*Routers failing to forward the bootp packets to the DHCP server when the clients reside on a separate network.
+
Always check your /var/logs/messages file for dhcpd errors and remember that mandatory keywords in your configuration file may change when you upgrade your operating system. Always read the release notes to be sure.
+
  
= Conclusion =
+
= 总结 =
  
 +
大多数的家庭网络,一个CHCP 服务器是没有必要的,因为DSL 路由器/防火墙(router / firewall)一般都有DHCP的功能,但是出于兴趣也可以尝试一下。务必确保一个网络的所有DHCP 服务器&nbsp;提供的IP地址不要重复,否则将有可能导致不可预料的错误。 要想在你的Linux 服务器上尝试提供DHCP服务,最好关闭路由器/防火墙所提供的DHCP服务。
  
In most home-based networks, a DHCP server isn't necessary because the DSL router / firewall usually has DHCP capabilities, but it is an interesting project to try. Just remember to make sure that the range of IP addresses issued by all DHCP servers on a network doesn't overlap because it could possibly cause unexpected errors. You might want to disable the router/firewall's DHCP server capabilities to experiment with your new Linux server.
+
在办公环境中,如果让一个网络工程师配置网络的时间以及资金投入都不大的话,让一个Linux系统管理员自己做这些事情会更简单,这时DHCP服务器也就不再需要了.
  
A DHCP server may be invaluable in an office environment where the time and cost of getting a network engineer to get the work done may make it simpler for Linux systems administrators to do it by themselves.
+
<br>配置一台DHCP服务器很简单,也涉及到了前面章节讲到的所有重要话题. 现在是时候尝试一些比较有难度的操作了,不过在开始之前,我们将快速地复习一下怎样创建将使用本书馀下部分介绍的许多功能的用户的方法。
  
Creating a Linux DHCP server is straightforward and touches all the major themes in the previous chapters. Now it's time to try something harder, but before we do, we'll do a quick refresher on how to create the Linux users who'll be using many of the applications outlined in the rest of the book.
+
<br>&nbsp;
 +
[[Category:Quick_HOWTO]]

2010年7月18日 (日) 16:07的最新版本


简介

一般来讲,如果你有一个有线调制解调器或者数字用户线路,你就可以使用家里的电脑获取由你的服务提供商动态分配的IP地址。如果在调制解调器和本地网络之间安装了有线/数字用户线路的路由器,你的电脑很有可能是在启动过程中从路由器获取了IP地址。你也可以选择禁用本地路由器中DHCP服务器的功能,而设置一个Linux机器作为DHCP服务器。

这一章仅包含对一个可以提供IP地址的DHCP服务器的配置指南。关于Linux系统中,DHCP客户端从DHCP服务器端如何获取IP地址的配置可以参考第三章,“Linux Networking”中的Linux网络互联部分。

下载与安装 DHCP 包

许多 RedHat 以及 Fedora Linux 软件都是RPM格式的. 下载安装这些RPM软件很容易. 如果你想再回顾下如何安装, Chapter 6, "Installing Linux Software", 详细讲解了这部分内容.

找该软件包的时候,请注意DHCP服务器的RPM包的命名往往以dhcp开始,后跟一个版本号,比如这样:dhcp-3.0.1rc14-1.i386.rpm

Debian Note: 对 Debian / Ubuntu 来说软件包名称可能也包含版本号. 用 dpkg --list | grep dhcp 来的到一个所有dhcp软件包的列表,从而找到dhcp服务区的软件包名称. 这里我们不妨认为这个软件包是 dhcp3-server.如果想回顾一下DEB包的安装请参见 Chapter 6, "Installing Linux Software".

root@u-bigboy:/tmp# dpkg --list | grep dhcp
ii  dhcp3-client   3.0.3-6ubuntu7  DHCP Client
ii  dhcp3-common   3.0.3-6ubuntu7  Files used by all the dhcp3* packages
root@u-bigboy:/tmp#

/etc/dhcpd.conf 文件

DHCP 服务器启动时,它读取 /etc/dhcpd.conf文件,并使用里面的命令来配置你的网络。标准的DHCP RPM软件包不会安装这个文件,但是你可以在下面的目录里找到一个配置文件示例作为参照。

/usr/share/doc/dhcp-<version-number>/dhcpd.conf.sample

你需要拷贝这个文件 dhcpd.conf 到 /etc 目录下面然后进行编辑。对于3.0p11版本的RPM包,拷贝文件命令如下:

[root@bigboy tmp]# cp /usr/share/doc/dhcp-3.0pl1/dhcpd.conf.sample /etc/dhcpd.conf

Debian Note: 对 Debian / Ubuntu 来说配置文件是 /etc/dhcp*/dhcpd.conf ,语法跟 Redhat / Fedora一样.

下面是对dhcpd.conf文件的简单解释:最重要的是,你的Linux主机的每个网络接口都必须有一个子网段

ddns-update-style interim
ignore client-updates
 
subnet 192.168.1.0 netmask 255.255.255.0 {
 
   # 当DHCP客户端主机启动网络时
   # 服务器能够分配给他的IP地址
   # 的范围
 
   range 192.168.1.201 192.168.1.220;
 
   # 客户端能使用该IP地址的时间
   # 以秒计算

  default-lease-time 86400;
  max-lease-time 86400;
 
   # 客户端默认网关
 
   option routers 192.168.1.1;
   # 不从一个网口向另一个网口转发
   # DHCP请求
 
   option ip-forwarding off;
 
   # 设置客户端广播地址和子网掩码

  option broadcast-address 192.168.1.255;
  option subnet-mask 255.255.255.0;
 
   # 设置客户端DNS服务器

  option domain-name-servers 192.168.1.100;
 
   # 设置客户端NTP服务器
 
   option nntp-server 192.168.1.100;
 
   # 如果你为Windows客户端指定了一个WINS服务器,
   # 你必须在 dhcpd.conf中加入以下选项 

  option netbios-name-servers 192.168.1.100;
 
   # 你也可以根据客户端MAC地址分配给他静态IP
   # (主机名是 "laser-printer"):

  host laser-printer {
      hardware ethernet 08:00:2b:4c:59:23;
     fixed-address 192.168.1.222;
   }
}
#
# 未使用网络接口
#
subnet 192.168.2.0 netmask 255.255.255.0 {
}

还有许多其他选项来配置 DHCP, 包括告诉 DHCP客户端哪里提供 finger 以及 IRC服务。在安装后请查看 dhcp-options man page :

[root@bigboy tmp]# man dhcp-options

Note: 示例文件dhcpd.conf文件里面的host指令非常有用. 像网络上打印机这样一些设备默认是通过DHCP获得IP地址的, 但是用户可以用固定IP地址区访问. 这个指令可以用来为一个已经预先知道网卡MAC地址的DHCP客户端提供静态IP地址。这可以降低系统管理开销

启动DHCP:

1) DHCP的一些旧版本的Fedora/RedHat将无法使用除非已有dhcpd.leases文件。如果没有的话可以用命令 touch /var/lib/dhcp/dhcpd.leases 去创建一个。 

[root@bigboy tmp]# touch /var/lib/dhcp/dhcpd.leases
2) 使用chkconfig命令配置DHCP在电脑启动时自动运行;

[root@bigboy tmp]# chkconfig dhcpd on

对 Debian / Ubuntu,dhcp3-server 软件包相应的命令为:

root@u-bigboy:/tmp# sysv-rc-conf dhcp3-server on


3) 然后用 service 命令执行 /etc/init.d/dhcpd 脚本来 start/stop/restart (启动/停止/重新起动) DHCP


启动

[root@bigboy tmp]# service dhcpd start

[root@bigboy tmp]# service dhcpd stop [root@bigboy tmp]# service dhcpd restart

对 Debian / Ubuntu 相应命令是:

root@u-bigboy:/tmp# /etc/init.d/dhcp*-server start

root@u-bigboy:/tmp# /etc/init.d/dhcp*-server stop root@u-bigboy:/tmp# /etc/init.d/dhcp*-server restart

4)每次更改配置文件后,要重启DHCP服务以使更改生效。你也可以用下面的命令测试DHCP是否在运行,如果是则返回DHCP进程的ID;

[root@bigboy tmp]# pgrep dhcpd


5) 最后,一定要记得设置你的电脑以使其可以通过DHCP来获得它的IP地址。


address via DHCP.

有多个网卡的DHCP 服务器

当一台被配置为动态获得IP(DHCP)的电脑启动时,他向DHCP服务器申请一个IP地址。这是通过向DHCP服务器发送一个标准的DHCP申请实现的。这个申请是一个广播桢,源IP地址为255.255.255.255

如果你的DHCP服务器有多个网络接口你必须为这个255.255.255.255添加一条路由表项,让服务器进程知道往哪个接口发送回应;否则,回应会被发往默认网关(下面两个例子中,我们假设DHCP请求来自接口eth0)

Note: 更多关于路由以及Linux添加路由表项的信息参见 Chapter 3, "Linux Networking".

Note: 你不能在多个网口上运行多个DHCP服务器,因为网络255.255.255.255只能有一个路由表项。如果你一定要这样,你会发现DHCP服务器只在一个网口上工作。


临时解决方案

你可以使用route add 命令临时为 255.255.255.255添加一个路由项,如下。

[root@bigboy tmp]# route add -host 255.255.255.255 dev eth0

如果你想让该路由项重启后仍能使用,那用下面的永久解决方案


永久解决方案

新的Fedora Linux添加静态路由的方法不支持将数据发往一个IP地址不是某一网关IP的接口。对地址为255.255.255.255的DHCP数据包的响应不应该发送给网关,而应该使用以太网帧中的MAC地址发送。

你有两个选择。往 /etc/rc.local 中加入一条命令, 或者往 /etc/sysconfig/static-routes 中添加一项,如下:

#
# File /etc/sysconfig/static-routes
#
eth0 host 255.255.255.255

Note: /etc/sysconfig/static-routes 是被声明为不赞成的(deprecated),最终会被Fedora去除

现在已经配置好服务器了,谈谈客户端吧。

配置客户端使用DHCP

Linux网络接口可以配置为通过DHCP获取IP地址,参考"Linux Networking"。如果你需要回顾DHCP客户端的配置,请查阅这一章。


配置Windows客户端使用 DHCP

Windows默认对所有网口使用DHCP,所以你不用担心需要重新配置

使用一个DHCP服务器为多个网络服务

如前所述,DHCP客户端发送一个请求IP地址的广播桢,而广播桢是被限制在局域网里面的。这本来意味着每个子网都需要一个DHCP服务器。但是,可以通过配置路由器向许多跳之外的DHCP服务器转发DHCP请求。这是通过将路由器在DHCP客户端接口的IP地址插入到转发的桢里面。对于DHCP服务器,非空的路由器的IP地址优先于广播地址考虑,并向该地址提供对DHCP客户端很重要的IP地址。DHCP服务器用一个广播桢响应(译者注:怎么是广播桢呢?应该发给当初转发DHCP请求的路由器才对,原文如此:The DHCP server replies with a broadcast packet), 而记录了原先的DHCP请求的路由器再把这个响应桢转发给DHCP客户端。 你可以用 ip helper-address 命令让Cisco路由器在所有有DHCP客户端的接口中启用该功能。下面是一个配置实例,DHCP服务器的地址为192.168.36.25:

interface FastEthernet 2/1
  ip address 192.168.1.30 255.255.255.0
  ip helper-address 192.168.36.25

简单的 DHCP 故障排解

DHCP最常见的问题常跟服务器无关;在服务器端被正确配置之后就没必要作任何更改,服务器会运行的很好。问题常常由客户端各种各样的原因导致。接下来部分讲述了简单的故障排除步骤。执行这些步骤使DHCP在你的网络上正常工作。


DHCP 客户端包含 169.254.0.0 地址

当Microsoft DHCP 客户端找不到DHCP服务器,他们就会默认从169.254.0.0网络中选择一个IP地址,直到找到DHCP服务器为止,这常被称为Automatic Private IP Addressing(APIPA)。你可以采取一下步骤解决该问题:

  • 确定DHCP服务器配置正确并使用先前讲到的 pgrep 命令来确定DHCP服务器进程在运行。特别注意 255.255.255.255 路由项,尤其当你有多个网口时。
  • 从DHCP服务器能够分配的IP地址里面选一个给DHCP客户端,然后看客户端能不能ping通DHCP服务器。如果不能,再次检查线缆与网卡。
  • DHCP使用 BOOTP 协议在客户端与服务器间交换信息。确定防火墙没有阻止通信。DHCP服务器在UDP端口67接收请求,DHCP客户端在UDP端口68等待响应,在服务器网卡上使用 tcpdump 检查通信数据流是否正确。

== == ==

DHCP的其它错误

如果你的DHCP服务器无法启动,可以使用第四章所描述的查错纠错技术,“常见网络错误及其解决方法”,来帮助纠正错误。 以下原因常导致初始化时的一些错误:

  • Incorrect settings in the /etc/dhcpd.conf file such as not defining the networks for which the DHCP server is responsible;
  • /etc/dhcpd.conf 文件配置错误,例如没有定义DHCP服务器负责的网络。
  • Firewall rules that block the DHCP bootp protocol on UDP ports 67 and 68;
  • 防火墙阻止了依赖于UDP端口67和68的 DHCP bootp 协议;
  • Routers failing to forward the bootp packets to the DHCP server when the clients reside on a separate network.
  • 当客户端位于另外一个网络时,路由器没有转发DHCP服务器的bootp数据包

经常地查看/var/logs/messages文件以确定是否有DHCP错误发生,同时记得当你升级操作系统的时候寄存在配置文件里的关键字可能改变。经常检阅版本发布通知以确保关键字没有改变。

总结

大多数的家庭网络,一个CHCP 服务器是没有必要的,因为DSL 路由器/防火墙(router / firewall)一般都有DHCP的功能,但是出于兴趣也可以尝试一下。务必确保一个网络的所有DHCP 服务器 提供的IP地址不要重复,否则将有可能导致不可预料的错误。 要想在你的Linux 服务器上尝试提供DHCP服务,最好关闭路由器/防火墙所提供的DHCP服务。

在办公环境中,如果让一个网络工程师配置网络的时间以及资金投入都不大的话,让一个Linux系统管理员自己做这些事情会更简单,这时DHCP服务器也就不再需要了.


配置一台DHCP服务器很简单,也涉及到了前面章节讲到的所有重要话题. 现在是时候尝试一些比较有难度的操作了,不过在开始之前,我们将快速地复习一下怎样创建将使用本书馀下部分介绍的许多功能的用户的方法。