Red Hat Training

A Red Hat training course is available for RHEL 8

42.2. 创建运行 dhclient exit hooks 的 NetworkManager 分配程序脚本

当 DHCP 服务器分配或更新 IPv4 地址时,NetworkManager 可以运行存储在 /etc/dhcp/dhclient-exit-hooks.d/ 目录中的分配程序脚本。然后,分配程序脚本可以运行 dhclient exit hooks。

先决条件

  • dhclient exit hooks 存储在 /etc/dhcp/dhclient-exit-hooks.d/ 目录下。

步骤

  1. 使用以下内容创建 /etc/NetworkManager/dispatcher.d/12-dhclient-down 文件:

    #!/bin/bash
    # Run dhclient.exit-hooks.d scripts
    
    if [ -n "$DHCP4_DHCP_LEASE_TIME" ] ; then
      if [ "$2" = "dhcp4-change" ] || [ "$2" = "up" ] ; then
        if [ -d /etc/dhcp/dhclient-exit-hooks.d ] ; then
          for f in /etc/dhcp/dhclient-exit-hooks.d/*.sh ; do
            if [ -x "${f}" ]; then
              . "${f}"
            fi
          done
        fi
      fi
    fi
  2. root 用户设为文件的所有者:

    # chown root:root /etc/NetworkManager/dispatcher.d/12-dhclient-down
  3. 设置权限,以便只有 root 用户才能执行它:

    # chmod 0700 /etc/NetworkManager/dispatcher.d/12-dhclient-down
  4. 恢复 SELinux 上下文:

    # restorecon /etc/NetworkManager/dispatcher.d/12-dhclient-down

其他资源

  • NetworkManager(8) 手册页