动态主机配置协议 (DHCP)

动态主机配置协议 (DHCP)

如何在 Ubuntu 12.04 中安装和配置 DHCP 服务器?

如果有人有分步教程的话我需要一个。

答案1

您需要做的第一件事是安装我们需要的软件包。

打开终端并输入:

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

答案2

接受的答案中可以包含其他内容

  1. 在启动 isc-dhcp-server 服务之前分配静态 IP。

  2. 您可以通过在特定子网内以以下格式添加 MAC ID 来为打印机和 Linux 机器等设备保留 IP。

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

    Dhcp3-server 社区 Wiki

    Ubuntu DHCP 服务器

答案3

来自 Ubuntu 社区页面dhcp3 服务器经过Ubuntu 文档 wiki 的贡献者

动态主机配置协议 (DHCP)

动态主机配置协议 (DHCP) 是一种网络服务,它允许主机自动从服务器分配设置,而无需手动配置每个网络主机。配置为 DHCP 客户端的计算机无法控制从 DHCP 服务器接收的设置,并且配置对计算机用户是透明的。

安装

在终端提示符下,输入以下命令来安装 dhcpd:

sudo apt-get install dhcp3-server

您可能需要通过编辑 /etc/dhcp3/dhcpd.conf 来更改默认配置以满足您的需求和特定配置。

您还需要编辑/etc/default/dhcp3-server以指定 dhcpd 应监听的接口。默认情况下,它监听 eth0。

配置

安装结束时的错误消息可能有点令人困惑,但以下步骤将帮助您配置服务:

最常见的情况是,您想要随机分配一个 IP 地址。这可以通过以下设置完成:

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;
} 

这将导致 DHCP 服务器为客户端提供 192.168.1.10-192.168.1.100 或 192.168.1.150-192.168.1.200 范围内的 IP 地址。如果客户端未要求特定时间范围,它将租用 IP 地址 600 秒。否则,最大(允许)租用时间为 7200 秒。服务器还将“建议”客户端应使用 255.255.255.0 作为其子网掩码,192.168.1.255 作为其广播地址,192.168.1.254 作为路由器/网关,192.168.1.1 和 192.168.1.2 作为其 DNS 服务器。

如果需要为 Windows 客户端指定 WINS 服务器,则需要包含 netbios-name-servers 选项,例如

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

dhcp3-server 和多个接口

多个接口示例

界面

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

选择接口卡

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

配置子网

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;
        }
}

查看路线

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

相关内容