📝

Kea DHCP のセットアップ - AlmaLinux 10

に公開

AlmaLinux 10 の Minimal Install 直後の状態から Kea DHCP を最小構成で起動する手順。

インストール

AlmaLinux 10 (EL10) では標準リポジトリで kea パッケージが提供されており、AlmaLinux 10.0 時点のバージョンは Kea 2.6.3 です。

# dnf install kea

設定

  1. DHCPv4 の設定ファイル /etc/kea/kea-dhcp4.conf を編集

    設定例 (最小構成):

    /etc/kea/kea-dhcp4.conf
    {
      "Dhcp4": {
        "interfaces-config": {
          "interfaces": [ "enp7s0" ]
        },
        "subnet4": [
          {
            "id": 1,
            "subnet": "10.0.0.0/16",
            "pools": [
              { "pool": "10.0.1.0  - 10.0.1.255" }
            ]
          }
        ]
      }
    }
    

    上記例では enp7s0 で DHCP メッセージを受け付け、10.0.1.0 ~ 10.0.1.255 の IP アドレスを払い出します。
    その他の設定項目は DHCPv4 Server Configuration から確認可能です。

  2. 設定ファイルの検証

    # kea-dhcp4 -t /etc/kea/kea-dhcp4.conf
    

    INFO や WARN で複数メッセージが出力されますが、ERROR がなければ問題ありません。

  3. ファイアウォールの許可

    # firewall-cmd --permanent --add-service=dhcp
    # firewall-cmd --reload
    

    なお、Kea DHCPv4 はデフォルトで raw ソケット (AF_PACKET, SOCK_RAW) を使用するため、Linux カーネルのファイアウォールを介さない通信が可能です。
    つまり、ファイアウォールで許可しなくても動作します。

    The DHCPv4 server uses raw sockets to achieve this, and builds the entire IP/UDP stack for the outgoing packets. The downside of raw socket use, however, is that incoming and outgoing packets bypass the firewalls (e.g. iptables).
    Interface Configuration

  4. 自身のインターフェースに IP アドレスを設定

    # nmcli con mod enp7s0 ipv4.method manual ipv4.address 10.0.0.2/16
    # nmcli con up enp7s0
    

    当前ですが自分自身の IP アドレスは事前に設定しておく必要があります。

起動

# systemctl enable --now kea-dhcp4

サービス起動後、ネットワーク上に DHCPDISCOVER が送信されると以下の様に Kea DHCP が IP アドレスを払い出します。

# tshark -i enp7s0 -f udp
 1 0.000000000  0.0.0.0 → 255.255.255.255 DHCP 330 DHCP Discover
 2 0.001478488 10.0.0.2 → 10.0.1.0        DHCP 319 DHCP Offer
 3 0.002175600  0.0.0.0 → 255.255.255.255 DHCP 336 DHCP Request
 4 0.003385384 10.0.0.2 → 10.0.1.0        DHCP 319 DHCP ACK

Ansible で構築

以下のロールで前述の最小構成を Ansible で構築可能です。

tasks/main.yml
---
- name: Install required packages
  ansible.builtin.dnf:
    name: kea

- name: Copy Kea DHCPv4 configuration file
  ansible.builtin.template:
    src: kea-dhcp4.conf.j2
    dest: /etc/kea/kea-dhcp4.conf
    owner: root
    group: kea
    mode: "640"
  notify: Restart kea-dhcp4

- name: Validate the configuration file
  ansible.builtin.command:
    cmd: kea-dhcp4 -t /etc/kea/kea-dhcp4.conf
  changed_when: false

- name: Allow dhcp service through firewalld
  ansible.posix.firewalld:
    service: dhcp
    state: enabled
    permanent: true
    immediate: true

- name: Add a connection with static IP address
  community.general.nmcli:
    conn_name: "{{ kea_interface }}"
    ifname: "{{ kea_interface }}"
    type: ethernet
    ip4: "{{ kea_address }}"
    state: present
  register: kea_con

- name: Reload the connection
  community.general.nmcli:
    conn_name: "{{ kea_interface }}"
    conn_reload: true
    state: up
  when: kea_con.changed # noqa: no-handler

- name: Start kea-dhcp4 service
  ansible.builtin.systemd_service:
    name: kea-dhcp4
    state: started
    enabled: true
handlers/main.yml
---
- name: Restart kea-dhcp4
  ansible.builtin.systemd_service:
    name: kea-dhcp4
    state: restarted
    enabled: true
defaults/main.yml
---
kea_interface: enp7s0
kea_address: 10.0.0.2/16
kea_subnet: 10.0.0.0/16
kea_pool: 10.0.1.0 - 10.0.1.255
templates/kea-dhcp4.conf.j2
{
  "Dhcp4": {
    "interfaces-config": {
      "interfaces": [ "{{ kea_interface }}" ]
    },
    "subnet4": [
      {
        "id": 1,
        "subnet": "{{ kea_subnet }}",
        "pools": [
          { "pool": "{{ kea_pool }}" }
        ]
      }
    ]
  }
}

Discussion