ノート

2020-04-27

[CentOS 8] DHCP サーバの設定

DHCP, Dynamic Host Configuration Protocol は、UDP/IP ネットワークで使用されるネットワーク管理プロトコルで、コンピュータがネットワークに接続する際に必要な設定情報を自動的に割り当てるために使用します。 BOOTP にリース機能を追加して DHCP となっています。

DHCP サーバは、IP アドレス等のネットワーク構成設定をネットワーク上の各デバイスに動的に割り当て、他の IP ネットワークと通信できるようにします。DHCP サーバを使用すると、コンピュータは自動的に IP アドレスとネットワーク設定を要求でき、ネットワーク管理者やエンドユーザが全てのネットワークデバイスに手動で IP アドレスを割り当てる必要がなくなります。

Wikipedia より抜粋、編集

使用している OS(および dhcp-server)は以下の通りです。

CentOS Linux release 8.1.1911 (Core) x86_64
dhcp-server-4.3.6-34.el8.x86_64

ネットワークは下記のような構成になっていて、CentOS 8 をインストールしたミニ PC, cnction の NIC enp2s0 のゾーン internal 側のネットワークにつなげた PC が、IP アドレスなどの情報を取得できるように、cnction に DHCP サーバを設定します。参考書 [1] の「Chapter 10 DHCP サーバ」を参考にして設定しました。

dhcp-server のインストール

dhcp-server のインストールは下記のようにします。

[bitwalk@cnction ~]$ sudo dnf install dhcp-server
...
(途中省略)
...
依存関係が解決しました。
===================================================================================================
 パッケージ              アーキテクチャー   バージョン                    リポジトリー       サイズ
===================================================================================================
インストール:
 dhcp-server             x86_64             12:4.3.6-34.el8               BaseOS             530 k

トランザクションの概要
===================================================================================================
インストール  1 パッケージ

ダウンロードサイズの合計: 530 k
インストール済みのサイズ: 1.4 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
...
(途中省略)
...
完了しました!
[bitwalk@cnction ~]$

dhcp-server の設定

DHCP の設定は ipv4 場合、/etc/dhcp/dhcpd.conf を編集しますが、/etc/dhcp/ ディレクトリ内はユーザー権限ではアクセスできませんので、sudo ではなく、 su でスーパーユーザーになって作業をすることにします。

[bitwalk@cnction ~]$ su
パスワード:
[root@cnction bitwalk]# cd /etc/dhcp
[root@cnction dhcp]# ls
dhclient.conf  dhclient.d  dhcpd.conf  dhcpd6.conf
[root@cnction dhcp]# vi dhcpd.conf

dhcpd.conf を下記のように編集します。

/etc/dhcp/dhcpd.conf
# dhcpd.conf

option domain-name "example.org";
option domain-name-servers 192.168.0.25;

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

log-facility local7;

subnet 192.168.2.0 netmask 255.255.255.0 {
  option routers 192.168.2.1;
  option subnet-mask 255.255.255.0;
  range dynamic-bootp 192.168.2.100 192.168.2.200;
}

編集した dhcpd.conf に対して、次のようにして書式チェックをします。

[root@cnction dhcp]# dhcpd -t -cf /etc/dhcp/dhcpd.conf
Internet Systems Consortium DHCP Server 4.3.6
Copyright 2004-2017 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
ldap_gssapi_principal is not set,GSSAPI Authentication for LDAP will not be used
Not searching LDAP since ldap-server, ldap-port and ldap-base-dn were not specified in the config file
Config file: /etc/dhcp/dhcpd.conf
Database file: /var/lib/dhcpd/dhcpd.leases
PID file: /var/run/dhcpd.pid
Source compiled to use binary-leases
[root@cnction dhcp]#

ファイアウォールの設定

NIC enp2s0 側のゾーン internal に対して DHCP のサービスをファイアウォールに追加します。

[root@cnction dhcp]# firewall-cmd --permanent --zone=internal --add-service=dhcp
success
[root@cnction dhcp]# firewall-cmd --reload
Warning: ZONE_ALREADY_SET: 'enp2s0' already bound to 'internal'
success
[root@cnction dhcp]#

警告が出たので、念のためゾーン internal の設定内容を確認しました。

[root@cnction dhcp]# sudo firewall-cmd --list-all --zone=internal
internal (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp2s0
  sources:
  services: cockpit dhcp dhcpv6-client mdns samba-client ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

[root@cnction dhcp]#

サービス提供インターフェイスの設定

参考書 [1] によると、システムに複数の NIC があって、特定の NIC に DHCP のサービスを提供する場合は /etc/sysconfig/dhcpd にインターフェイス名を指定するとありますが、少なくとも現バージョンの dhcp-server では設定する必要はなさそうです。

/etc/sysconfig/dhcpd
# WARNING: This file is NOT used anymore.

# If you are here to restrict what interfaces should dhcpd listen on,
# be aware that dhcpd listens *only* on interfaces for which it finds subnet
# declaration in dhcpd.conf. It means that explicitly enumerating interfaces
# also on command line should not be required in most cases.

# If you still insist on adding some command line options,
# copy dhcpd.service from /lib/systemd/system to /etc/systemd/system and modify
# it there.
# https://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F

# example:
# $ cp /usr/lib/systemd/system/dhcpd.service /etc/systemd/system/
# $ vi /etc/systemd/system/dhcpd.service
# $ ExecStart=/usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid 
# $ systemctl --system daemon-reload
# $ systemctl restart dhcpd.service

サービスの起動

dhcpd のサービスを起動します。同時に自動起動するようにも設定します。

[root@cnction dhcp]# systemctl start dhcpd
[root@cnction dhcp]# systemctl enable dhcpd
Created symlink /etc/systemd/system/multi-user.target.wants/dhcpd.service → /usr/lib/systemd/system/dhcpd.service.
[root@cnction dhcp]# systemctl status dhcpd
 dhcpd.service - DHCPv4 Server Daemon
   Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2020-04-26 12:37:31 JST; 52s ago
     Docs: man:dhcpd(8)
           man:dhcpd.conf(5)
 Main PID: 25156 (dhcpd)
   Status: "Dispatching packets..."
    Tasks: 1 (limit: 20121)
   Memory: 4.9M
   CGroup: /system.slice/dhcpd.service
           └─25156 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid

 4月 26 12:37:31 cnction dhcpd[25156]:
 4月 26 12:37:31 cnction dhcpd[25156]: No subnet declaration for enp1s0 (192.168.0.31).
 4月 26 12:37:31 cnction dhcpd[25156]: ** Ignoring requests on enp1s0.  If this is not what
 4月 26 12:37:31 cnction dhcpd[25156]:    you want, please write a subnet declaration
 4月 26 12:37:31 cnction dhcpd[25156]:    in your dhcpd.conf file for the network segment
 4月 26 12:37:31 cnction dhcpd[25156]:    to which interface enp1s0 is attached. **
 4月 26 12:37:31 cnction dhcpd[25156]:
 4月 26 12:37:31 cnction dhcpd[25156]: Sending on   Socket/fallback/fallback-net
 4月 26 12:37:31 cnction dhcpd[25156]: Server starting service.
 4月 26 12:37:31 cnction systemd[1]: Started DHCPv4 Server Daemon.
[root@cnction dhcp]#

動作確認

ゾーン internal 側に、Ubuntu 18.04 のノート PC を繋げてネットワークの設定を確認しました。

ゾーン internal 側に繋げた PC のネットワーク設定

実は、最初にサービスを起動した時には、ネットワークの設定を確認後に cnction のネットワークが固まってしまいました。cnction の再起動後は特に問題なく使えています。Windows PC も繋げてみましたが問題なさそうです。しばらく使ってみて、通っていないポートなど不具合がないか確認します。

参考サイト

  1. TECHNICAL MASTER はじめてのCentOS 8 Linuxサーバエンジニア入門編 - 秀和システム あなたの学びをサポート

ブログランキング・にほんブログ村へ
にほんブログ村

0 件のコメント:

コメントを投稿