はじめに
CentOS7で2つのサーバを用意し、DHCPサーバ・クライアントとして動作させてみます。
- testvm1: DHCPサーバ
- eth1 192.168.99.1/24
- eth2 192.168.100.1/24
- testvm2: DHCPクライアント
- eth1 192.168.99.2/24
- eth2 192.168.100.2/24 ←このeth2をDHCPでIPアドレス取得するように変更します
DHCPサーバの設定・起動
-
dhcpパッケージのインストール
[root@testvm1 ~]# yum install -y dhcp
```
-
dhcpd.confの編集
[root@testvm1 ~]# cat /etc/dhcp/dhcpd.conf
ddns-update-style none; Dynamic DNSのON/OFF
default-lease-time 28800; 割り当てIPアドレスの有効期間
max-lease-time 86400; 割り当てる最大有効期間
subnet 192.168.100.0 netmask 255.255.255.0 { サブネット192.168.100.0/24に関する設定
authoritative; 異なるサブネットのIPアドレスを要求してきたときの動作(DHCP-NAKを返すか、無視するか)
range 192.168.100.101 192.168.100.199; 割り当てるIPアドレス範囲
option subnet-mask 255.255.255.0; 配布するサブネット情報
option routers 192.168.100.254; 配布するデフォルトゲートウェイ情報
option domain-name-servers 192.168.100.1; 配布するDNSサーバ情報
option domain-name "test.com"; 配布するドメイン名情報
}
```
-
dhcpdの起動
[root@testvm1 ~]# systemctl start dhcpd
```
起動すると`/var/log/messages`に以下のようなメッセージが出力される
```
eth2(192.168.100.1/24)でリッスンされる
Jan 13 01:48:49 testvm1 dhcpd: Listening on LPF/eth2/08:00:27:43:9b:ce/192.168.100.0/24
Jan 13 01:48:49 testvm1 dhcpd: Sending on LPF/eth2/08:00:27:43:9b:ce/192.168.100.0/24
Jan 13 01:48:49 testvm1 dhcpd:
eth1(192.168.99.1/24)は設定していないため以下のように出力される
Jan 13 01:48:49 testvm1 dhcpd: No subnet declaration for eth1 (192.168.99.1).
Jan 13 01:48:49 testvm1 dhcpd: ** Ignoring requests on eth1. If this is not what
```
DHCPクライアントからの接続確認
-
DGCPクライアントのeth2の設定ファイルを変更して、NICを再起動
[root@testvm2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth2
NM_CONTROLLED=yes
BOOTPROTO=dhcp
ONBOOT=yes
DEVICE=eth2
[root@testvm2 network-scripts]# ifdown eth2
Device 'eth2' successfully disconnected.
[root@testvm2 network-scripts]# ifup eth2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
```
-
IPアドレスを確認
192.168.100.101が設定されているのが確認できる
[root@testvm2 ~]# ip a
4: eth2: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:98e1 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.101/24 brd 192.168.100.255 scope global noprefixroute dynamic eth2
valid_lft 28074sec preferred_lft 28074sec
inet6 fe80::a00:27ff:fe98:ace1/64 scope link
valid_lft forever preferred_lft forever
```
-
DHCPサーバ側でtcpdumpによる通信を確認していると以下のやり取りが確認できる
[root@testvm1 ~]# tcpdump -nlSvi eth2 dst port 67 or dst port 68
tcpdump: listening on eth2, link-type EN10MB (Ethernet), capture size 262144 bytes
----DHCPクライアントのeth2(MACアドレス:08:00:27:98:ac:e1)からDHCPディスカバー(ブロードキャストでDHCPサーバを探す)----
02:20:29.173395 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
0.0.0.0.bootpc > 255.255.255.255.bootps: BOOTP/DHCP, Request from 08:00:27:98e1, length 300, xid 0xcab0ba6d, Flags [none]
Client-Ethernet-Address 08:00:27:98e1
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message Option 53, length 1: Discover ←★DHCPディスカバー
Hostname Option 12, length 7: "testvm2"
Parameter-Request Option 55, length 19:
Subnet-Mask, BR, Time-Zone, Classless-Static-Route
Domain-Name, Domain-Name-Server, Hostname, YD
YS, NTP, MTU, Option 119
Default-Gateway, Classless-Static-Route, Classless-Static-Route-Microsoft, Static-Route
Option 252, NTP, RP
----DHCPサーバからDHCPクライアントへDNCオファー(IPアドレス192.168.100.101やDNSサーバなどを指定)----
02:20:30.175255 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
192.168.100.1.bootps > 192.168.100.101.bootpc: BOOTP/DHCP, Reply, length 300, xid 0xcab0ba6d, Flags [none]
Your-IP 192.168.100.101
Client-Ethernet-Address 08:00:27:98e1
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message Option 53, length 1: Offer ←★DHCPオファー
Server-ID Option 54, length 4: 192.168.100.1
Lease-Time Option 51, length 4: 28800
Subnet-Mask Option 1, length 4: 255.255.255.0
Domain-Name Option 15, length 8: "test.com"
Domain-Name-Server Option 6, length 4: 192.168.100.1
Default-Gateway Option 3, length 4: 192.168.100.254
----DHCPクライアントへDHCPサーバへDHCPリクエスト(IPアドレス192.168.100.101を指定)----
02:20:30.176446 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
0.0.0.0.bootpc > 255.255.255.255.bootps: BOOTP/DHCP, Request from 08:00:27:98e1, length 300, xid 0xcab0ba6d, Flags [none]
Client-Ethernet-Address 08:00:27:98e1
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message Option 53, length 1: Request ←★DHCPリクエスト
Server-ID Option 54, length 4: 192.168.100.1
Requested-IP Option 50, length 4: 192.168.100.101
Hostname Option 12, length 7: "testvm2"
Parameter-Request Option 55, length 19:
Subnet-Mask, BR, Time-Zone, Classless-Static-Route
Domain-Name, Domain-Name-Server, Hostname, YD
YS, NTP, MTU, Option 119
Default-Gateway, Classless-Static-Route, Classless-Static-Route-Microsoft, Static-Route
Option 252, NTP, RP
----DHCPサーバからDHCPクライアントへDNC-ACK(IPアドレス192.168.100.101やDNSサーバなどを指定)----
02:20:30.181847 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
192.168.100.1.bootps > 192.168.100.101.bootpc: BOOTP/DHCP, Reply, length 300, xid 0xcab0ba6d, Flags [none]
Your-IP 192.168.100.101
Client-Ethernet-Address 08:00:27:98e1
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message Option 53, length 1: ACK ←★DHCP-ACK
Server-ID Option 54, length 4: 192.168.100.1
Lease-Time Option 51, length 4: 28800
Subnet-Mask Option 1, length 4: 255.255.255.0
Domain-Name Option 15, length 8: "test.com"
Domain-Name-Server Option 6, length 4: 192.168.100.1
Default-Gateway Option 3, length 4: 192.168.100.254
```
-
DHCPサーバで上で、割り当て中のIPアドレス確認
DHCPサーバ上では以下のファイルから割り当て中のIPアドレスの情報が確認できます
[root@testvm1 ~]# cat /var/lib/dhcpd/dhcpd.leases
lease 192.168.100.101 {
starts 3 2020/01/15 12:16:41;
ends 3 2020/01/15 20:16:41;
cltt 3 2020/01/15 12:16:41;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 08:00:27:98e1;
client-hostname "testvm2";
}
server-duid "\000\001\000\001%\256\206g\010\000'C\233\316";
```