DHCP 使用

July 6, 2018

Description

Environment

DHCP Server - Ubuntu 14.04

DHCP Clients - Ubuntu 14.04(test1) and Ubuntu 14.04(test2)

Installing DHCP in DHCP Server

[itachi@ubuntu:~] sudo apt install isc-dhcp-server -y

DHCP Server Setting

DHCP 接口
[itachi@ubuntu:~] vi /etc/default/isc-dhcp-server
	INTERFACES="eth0" # 接口上提供DHCP請求,可以多個。
網路設定

DHCP Server 設定 static Network

[itachi@ubuntu:~] vi /etc/network/interfaces
		iface eth0 inet static # 虛擬機預設是 DHCP ,更改成 Static IP
		address 192.168.8.XX # 必須是 Host-only 網段上的 IP
		netmask 255.255.255.0 # 遮罩
		gateway 192.168.8.1 # 閘道
		dns-nameservers 8.8.8.8 # dns

Configuring DHCP in DHCP Server

DHCP configuration file

/etc/dhcp/dhcpd.conf # 設定一些資訊給DHCP Client 端

[itachi@ubuntu:~] vi /etc/dhcp/dhcpd.conf
	# 全域設定
	ddns-update-style none;	# 支援 ddns (Dynamic DNS) 更新,IP
				# 這個選項一定要存在才不會有錯誤。

	option domain-name "com.tw"; 	# 域名
	option domain-name-servers ns1.com.tw, ns2.com.tw; # DNS
	default-lease-time 3600; 	# 預設租期時間長度(秒)
	max-lease-time 7200;		# 租期到可續租,但最大租期時間(秒)
	authoritative; 			# DHCP Server 為本地網路,註解需拿掉

	# 定義子網路
	subnet 192.168.8.0 netmask 255.255.255.0 {  	# network
		range 192.168.8.200 192.168.8.254; 	# Range
		option domain-name-servers 192.168.8.1, 8.8.8.8; # Pri DNS , Sec DNS
		option domain-name "com.tw"; 		# Domain name
		option routers 192.168.8.1; 		# Gateway
		option broadcast-address 192.168.8.255; # Broadcast
	}
	
	# 針對特定服務或裝置給定 IP
	host ubuntu { #名稱隨意,最好是對方hostname
		hardware ethernet 00:f0:m4:6y:89:0g; 	# 對方的 MAC
		fixed-address 192.168.8.210; 		# 固定從 range 給定此 IP
	}

Service start

  sudo service isc-dhcp-server start  
  sudo service isc-dhcp-server stop  
  sudo service isc-dhcp-server restart

test

Client

test1:192.168.8.200
test1:192.168.8.201

Server

DHCP Server:192.168.8.60 DHCP Server 檢查是否分配

[itachi@ubuntu:~] sudo tail /var/lib/dhcp/dhcpd.leases