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。
root@test:~# 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
root@test:~# 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 0 0 localhost:domain 0.0.0.0:* 820/systemd-resolve
udp 0 0 0.0.0.0:bootps 0.0.0.0:* 3848/dhcpd
udp 0 0 0.0.0.0:ipp 0.0.0.0:* 1075/cups-browsed
udp 0 0 0.0.0.0:53979 0.0.0.0:* 3848/dhcpd
udp 8448 0 test:mdns 0.0.0.0:* 1675/nxserver.bin
udp 0 0 0.0.0.0:mdns 0.0.0.0:* 1675/nxserver.bin
udp 0 0 0.0.0.0:mdns 0.0.0.0:* 940/avahi-daemon: r
udp 0 0 0.0.0.0:40539 0.0.0.0:* 940/avahi-daemon: r
udp6 0 0 [::]:24554 [::]:* 3848/dhcpd
udp6 0 0 [::]:37176 [::]:* 940/avahi-daemon: r
udp6 0 0 [::]:mdns [::]:* 940/avahi-daemon: r root@test:/home/test/Documents/
配置過程中的調試
第一次配置過程中很有可能出現配置失敗無法啟動dhcp的情況,筆者第一次配置時也是遇到了很多問題,后來發現查看日志是最可靠的方法。
查看系統日志:
# vim /var/log/syslog
上面記錄了失敗的具體原因,定位到行,能比較有效地減少 花在配置錯誤上面的分析時間。
另外,官方的guidebook和Q&A也是比較有效工具。
以上內容來自 hey_leo 的CSDN 博客(修改圖片內容為文字) ,全文地址請點擊:https://blog.csdn.net/u010177634/article/details/53044777?utm_source=copy
其他相關配置:
雙網卡靜態地址配置:
# vim /etc/network/interfaces
示例:
# 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
gateway 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
# systemctl disable NetworkManager.service
# systemctl enable NetworkManager.service
強制刷新網卡IP:
# ip addr flush dev eno1
# ifdown eno1
# ifup eno1
增加默認網關:
# route add default gw 10.11.12.1
重啟網絡服務:
# sudo /etc/init.d/networking restart