如何安装和配置DHCP服务器?

如何在Ubuntu 12.04上安装和配置DHCP服务器?
如果有人有一份逐步教程,请提供给我。

这个链接可能不是最用户友好的,但它提供了关于dhcp3-server的帮助。 - adempewolff
1这可能是一样的,但是... http://comtech247.net/2012/05/01/how-to-set-up-a-dhcp-server-on-ubuntu-server-12-04-lts/ - Mitch
2请注意,在Ubuntu 12.04中,ISC DHCP服务器是dhcp3-server的过渡软件包。 - user66009
1我更倾向于使用dnsmasq,这是一个具有简单配置文件语法的DNS和DHCP服务器。 - Reinier Post
3个回答

首先,您需要安装我们所需的软件包。
打开终端并输入:
sudo apt-get install isc-dhcp-server

有两个主要文件/etc/default/isc-dhcp-server/etc/dhcp/dhcpd.conf,我们需要进行配置,所以让我们先看第一个。
打开终端并使用您喜欢的文本编辑器输入:
sudo vim /etc/default/isc-dhcp-server

你应该得到以下内容:
#Defaults for dhcp initscript
#sourced by /etc/init.d/dhcp
#installed at /etc/default/isc-dhcp-server by the maintainer scripts
#
#This is a POSIX shell fragment
#
#On what interfaces should the DHCP server (dhcpd) serve DHCP requests"
#Separate multiple interfaces with spaces, e.g. “eth0 eth1".
INTERFACES="eth0"

将上面的eth0替换为您希望服务器租用地址的网络接口名称。继续下一个文件。
打开终端并输入:
sudo vim /etc/dhcp/dhcpd.conf

应该会给你以下的输出结果。
#
#Sample configuration file for ISC dhcpd for Debian
#
#Attention: If /etc/ltsp/dhcpd.conf exists, that will be used as
#configuration file instead of this file.
#
#
....
option domain-name “example.org”;
option domain-name-servers ns1.example.org, ns2.example.org;
option domain-name “comtech.com”;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.150 10.0.0.253;
option routers 10.0.0.2;
option subnet-mask 255.255.255.0;
option broadcast-address 10.0.0.254;
option domain-name-servers 10.0.0.1, 10.0.0.2;
option ntp-servers 10.0.0.1;
option netbios-name-servers 10.0.0.1;
option netbios-node-type 8;
 ......
}

这需要一点解释。

  1. 根据您的网络要求调整设置。
  2. 选项域名是您的 DNS 区域名称。例如,我的设置为 comtech.com。
  3. 范围应该是您希望服务器提供给客户端的 IP 地址范围。

现在通过输入以下内容重新启动 dhcp 服务:

sudo service isc-dhcp-server restart

这就是了!你的 DHCP 服务器应该正在运行,不过最好检查一下。打开终端并输入:
sudo netstat -uap

以下是一个示例,会显示类似下面的内容(查找 dhcpdnmbdnamed):
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address State PID/Program name
udp        0      0 10.0.0.255:netbios-dgm  *:*                   1016/nmbd
udp        0      0 10.0.0.255:netbios-ns   *:*                   1016/nmbd
udp        0      0 *:bootps                *:*                   4525/dhcpd
udp        0      0 *:netbios-dgm           *:*                   1016/nmbd
udp        0      0 *:netbios-ns            *:*                   1016/nmbd
udp        0      0 chris-desktop:domain    *:*                   1273/named
udp        0      0 chris-desktop.lo:domain *:*                   1273/named
udp        0      0 chris-deskt:netbios-dgm *:*                   1016/nmbd
udp        0      0 chris-deskto:netbios-ns *:*                   1016/nmbd
udp6       0      0 [::]:domain             [::]:*                1273/named

1提醒一下,务必使用/etc/dhcp/dhcpd.conf而不是/etc/dhcp3/dhcpd.conf - puk
1当我重新启动服务时,会显示以下消息:“stop: Unknown instance: isc-dhcp-server start/running, process 3027”。我无法启动该服务。我使用的是Ubuntu 13.10。 - JJD
1好的。但在启动DHCP服务之前,请确保已经分配了静态IP。 - krizna
这并不难,但如果你问我,使用 dnsmasq 更容易。 - Reinier Post

可以在接受的答案中包括其他内容。
  1. 在启动 isc-dhcp-server 服务之前,先分配静态 IP。

  2. 您可以通过在特定子网中按以下格式添加 MAC 地址来为打印机和 Linux 设备保留 IP。

    ------
    host bla1 { 
            hardware ethernet DD:GH:DF:E5:F7:D7;
            fixed-address 10.0.0.10;
    }
    ----
    

    Dhcp3-server 社区维基

    Ubuntu DHCP 服务器


从Ubuntu社区页面dhcp3-serverUbuntu文档维基的贡献者提供:

Dynamic Host Configuration Protocol (DHCP)

The Dynamic Host Configuration Protocol (DHCP) is a network service that enables host computers to be automatically assigned settings from a server as opposed to manually configuring each network host. Computers configured to be DHCP clients have no control over the settings they receive from the DHCP server, and the configuration is transparent to the computer's user.

Installation

At a terminal prompt, enter the following command to install dhcpd:

sudo apt-get install dhcp3-server

You will probably need to change the default configuration by editing /etc/dhcp3/dhcpd.conf to suit your needs and particular configuration.

You also need to edit /etc/default/dhcp3-server to specify the interfaces dhcpd should listen to. By default it listens to eth0.

Configuration

The error message the installation ends with might be a little confusing, but the following steps will help you configure the service:

Most commonly, what you want to do is assign an IP address randomly. This can be done with settings as follows:

nano -w /etc/dhcp3/dhcpd.conf
# Sample /etc/dhcpd.conf
# (add your comments here) 
default-lease-time 600;
max-lease-time 7200;
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.1.255;
option routers 192.168.1.254;
option domain-name-servers 192.168.1.1, 192.168.1.2;
option domain-name "mydomain.example";

subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.10 192.168.1.100;
range 192.168.1.150 192.168.1.200;
} 

This will result in the DHCP server giving a client an IP address from the range 192.168.1.10-192.168.1.100 or 192.168.1.150-192.168.1.200. It will lease an IP address for 600 seconds if the client doesn't ask for a specific time frame. Otherwise the maximum (allowed) lease will be 7200 seconds. The server will also "advise" the client that it should use 255.255.255.0 as its subnet mask, 192.168.1.255 as its broadcast address, 192.168.1.254 as the router/gateway and 192.168.1.1 and 192.168.1.2 as its DNS servers.

If you need to specify a WINS server for your Windows clients, you will need to include the netbios-name-servers option, e.g.

nano -w /etc/default/dhcp3-server
option netbios-name-servers 192.168.1.1; 

dhcp3-server and multiple interfaces

multiple interfaces example

Interface

nano -w /etc/network/interfaces
auto lo
iface lo inet loopback

mapping hotplug
        script grep
        map eth1

iface eth1 inet dhcp

auto eth0
iface eth0 inet static
    address 10.152.187.1
    netmask 255.255.255.0

auto wlan0
  iface wlan0 inet static
    address 192.168.1.1
    netmask 255.255.255.0
    up     /sbin/iwconfig wlan0 mode TTTTTT && /sbin/iwconfig wlan0 enc
restricted && /sbin/iwconfig wlan0 key [Y] XXXXXXXX && /sbin/iwconfig
wlan0 essid SSSSSSSS

auto eth1

Select Interface card

nano -w /etc/default/dhcp3-server
INTERFACES="wlan0 eth0"

Configure Subnet

nano -w /etc/dhcp3/dhcpd.conf
ddns-update-style none;
log-facility local7;

subnet 192.168.1.0 netmask 255.255.255.0 {

        option routers                  192.168.1.1;
        option subnet-mask              255.255.255.0;
        option broadcast-address        192.168.1.255;
        option domain-name-servers      194.168.4.100;
        option ntp-servers              192.168.1.1;
        option netbios-name-servers     192.168.1.1;
        option netbios-node-type 2;
        default-lease-time 86400;
        max-lease-time 86400;

        host bla1 {
                hardware ethernet DD:GH:DF:E5:F7:D7;
                fixed-address 192.168.1.2;
        }
        host bla2 {
                hardware ethernet 00:JJ:YU:38:AC:45;
                fixed-address 192.168.1.20;
        }
}

subnet  10.152.187.0 netmask 255.255.255.0 {

        option routers                  10.152.187.1;
        option subnet-mask              255.255.255.0;
        option broadcast-address        10.152.187.255;
        option domain-name-servers      194.168.4.100;
        option ntp-servers              10.152.187.1;
        option netbios-name-servers     10.152.187.1;
        option netbios-node-type 2;

        default-lease-time 86400;
        max-lease-time 86400;

        host bla3 {
                hardware ethernet 00:KK:HD:66:55:9B;
                fixed-address 10.152.187.2;
        }
}

Check Route

ip route
192.168.1.0/24 dev wlan0  scope link
82.16.TT.0/24 dev eth1  scope link
10.152.187.0/24 dev eth0  scope link
default via 82.16.TT.UU dev eth1

  • 相关问题