22.2. 创建运行 dhclient exit hooks 的 NetworkManager 分配程序脚本
当 DHCP 服务器分配或更新 IPv4 地址时,NetworkManager 可以运行存储在 /etc/dhcp/dhclient-exit-hooks.d/ 目录中的分配程序脚本。然后,该分配程序脚本可以运行 dhclient 退出钩子。
先决条件
-
dhclientexit hooks 存储在/etc/dhcp/dhclient-exit-hooks.d/目录下。
流程
使用以下内容创建
/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将
root用户设为文件的所有者:# chown root:root /etc/NetworkManager/dispatcher.d/12-dhclient-down设置权限,以便只有 root 用户才能执行它:
# chmod 0700 /etc/NetworkManager/dispatcher.d/12-dhclient-down恢复 SELinux 上下文:
# restorecon /etc/NetworkManager/dispatcher.d/12-dhclient-down