ubuntu16.04上安装配置DHCP服务的详细过程

Posted 网络漫步

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ubuntu16.04上安装配置DHCP服务的详细过程相关的知识,希望对你有一定的参考价值。

DHCP服务器是为客户端机器分配IP地址的,所有分配的IP地址都保存在DHCP服务器的数据库中。为了在子网中实现DHCP分配IP地址,需要在目标主机上安装配置DHCP服务。

安装DHCP服务

安装isc-dhcp-server:

$ sudo apt install isc-dhcp-server

配置DHCP服务

首先需要知道目标主机的网卡名称:

$ ifconfig

得到以下信息:

eno1      Link encap:Ethernet  HWaddr e8:39:35:46:10:f5
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:37752 errors:0 dropped:1 overruns:0 frame:0
          TX packets:2202 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2975927 (2.9 MB)  TX bytes:226745 (226.7 KB)
          Interrupt:20 Memory:fe400000-fe420000

enx00e04c6802a0 Link encap:Ethernet  HWaddr 00:e0:4c:68:02:a0
          inet addr:10.11.12.13  Bcast:10.11.12.255  Mask:255.255.255.0
          inet6 addr: fe80::ae12:d219:10b4:71b4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7478 errors:0 dropped:0 overruns:0 frame:0
          TX packets:423 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:753496 (753.4 KB)  TX bytes:51662 (51.6 KB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:316 errors:0 dropped:0 overruns:0 frame:0
          TX packets:316 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:23353 (23.3 KB)  TX bytes:23353 (23.3 KB)

可以看到,目标主机的两张网卡名称分别为eno1和enx00e04c6802a0,其中eno1可用。
接下来配置 /etc/default/isc-dhcp-server 文件。

$ sudo vim /etc/default/isc-dhcp-server

将INTERFACES的内容补充为目标主机的网卡名称eno1。

[email protected]:~# sudo vim /etc/default/isc-dhcp-server
# Defaults for isc-dhcp-server initscript
# sourced by /etc/init.d/isc-dhcp-server
# installed at /etc/default/isc-dhcp-server by the maintainer scripts

#
# This is a POSIX shell fragment
#

# Path to dhcpd‘s config file (default: /etc/dhcp/dhcpd.conf).
#DHCPD_CONF=/etc/dhcp/dhcpd.conf

# Path to dhcpd‘s PID file (default: /var/run/dhcpd.pid).
#DHCPD_PID=/var/run/dhcpd.pid

# Additional options to start dhcpd with.
#       Don‘t use options -cf or -pf here; use DHCPD_CONF/ DHCPD_PID instead
#OPTIONS=""

# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
#       Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACES="eno1"

配置 /etc/dhcp/dhcpd.conf 文件:

$ sudo vim /etc/dhcp/dhcpd.conf

文件内容中需要配置的是域名和子网IP等信息,对于第一文件片段中的
option domain-name “example.org” 不用修改,下一行的domain-name-server需要注释掉在第二文件片段中补充。

目标主机的子网IP为192.168.100.1,因此:
subnet为192.168.100.0
DHCP分配范围设置为10-254,其余地址留给广播和静态IP
网关和DNS均设置为192.168.100.1
广播地址为192.168.100.255
ntp-servers和netbios-name-servers设置与DNS一致
netbios-node-type默认为8

[email protected]:~# cat /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 definitions common to all supported networks...
option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;

default-lease-time 600;
max-lease-time 7200;


# This is a very basic subnet declaration.

subnet 192.168.100.0 netmask 255.255.255.0 {
    range 192.168.100.10 192.168.100.254;
    option routers 192.168.100.1;
    option subnet-mask 255.255.255.0;
    option broadcast-address 192.168.100.255;
    option domain-name-servers 192.168.100.1;
    option ntp-servers 192.168.100.1;
    option netbios-name-servers 192.168.100.1;
    option netbios-node-type 8;
}


#subnet 10.254.239.0 netmask 255.255.255.224 {
#  range 10.254.239.10 10.254.239.20;
#  option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
#}

配置完成之后重启DHCP服务:

$ sudo service isc-dhcp-server restart

查看dhcp是否正常运行:

$ sudo netstat -uap

当显示dhcpd的program name时表示dhcp服务安装配置启动成功

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
udp    44544      0 acrn-HP-Compaq-8:domain *:*                                 1005/dnsmasq
udp    38080      0 *:bootpc                *:*                                 1625/dhclient
udp        0      0 *:33033                 *:*                                 764/avahi-daemon: r
udp    13376      0 *:37416                 *:*                                 1005/dnsmasq
udp        0      0 *:ipp                   *:*                                 847/cups-browsed
udp    45696      0 *:mdns                  *:*                                 764/avahi-daemon: r
udp6       0      0 [::]:43169              [::]:*                              764/avahi-daemon: r
udp6   28800      0 [::]:mdns               [::]:*                              764/avahi-daemon: r

配置过程中的调试

第一次配置过程中很有可能出现配置失败无法启动dhcp的情况,笔者第一次配置时也是遇到了很多问题,后来发现查看日志是最可靠的方法。

查看系统日志:

$ vim /var/log/syslog

上面记录了失败的具体原因,定位到行,能比较有效地减少 花在配置错误上面的分析时间。
另外,官方的guidebook和Q&A也是比较有效工具。


本文来自 hey_leo 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u010177634/article/details/53044777?utm_source=copy

其他相关配置:

双网卡静态地址配置:

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

auto eno1
iface eno1 inet static
address 10.11.12.13
netmask 255.255.255.0
network 10.11.12.0
getway 10.11.12.1

auto enx00e04c6802a0
iface enx00e04c6802a0 inet static
address 192.168.100.1
netmask 255.255.255.0
network 192.168.100.0
broadcast 192.168.100.255

关闭/开启ubuntu network-manager:

service network-manager stop
service network-manager start
service network-manager restart

强制刷新网卡IP:

ip addr flush dev eno1
ifdown eno1
ifup eno1

增加默认网关:

route add default gw 10.11.12.1

重启网络服务:

sudo /etc/init.d/networking restart









以上是关于ubuntu16.04上安装配置DHCP服务的详细过程的主要内容,如果未能解决你的问题,请参考以下文章

ubuntu16.04怎么修改DNS

ubuntu16.04新服务器上配置selenium+firefox

Ubuntu 16.04 上安装 CUDA 9.0 详细教程

在 Ubuntu 16.04 上重新安装 MySQL 5.7 服务器的问题

Win7 U盘安装Ubuntu16.04 双系统详细教程

Ubuntu16.04设置静态IP或动态ip(DHCP)