guidugli.kernel_config

Ansible 角色:kernel_config

一个用于在 RHEL/CentOS、Fedora 和 Debian/Ubuntu 上安装和配置 sysctl 的 Ansible 角色。该角色还可以禁用、黑名单或设置内核模块自启动。此外,还可以设置与电源管理相关的 UDEV 规则。

注意: 禁用内核模块或将设备设置为休眠(udev 设置)可能会影响系统工作,可能需要系统恢复(单用户登录或甚至实时启动)。

要求

操作系统必须运行在裸机或虚拟化环境中。根据设置,该角色可能在容器化系统上无法正常工作。

角色变量

可用变量如下,包含默认值(请查看 defaults/main.yml):

kernel_disable_modules: ['cramfs', 'freevxfs', 'jjfs2', 'hfs', 'hfsplus', 'udf', 'squashfs',
                         'dccp', 'sctp', 'rds', 'tipc']

使用此变量列出应禁用的内核模块,你可以将 usb-storage 添加到列表中,但这会导致任何 USB 存储设备无法工作。建议使用 USBGuard。默认值适用于大多数系统。如果系统需要使用其中某些模块,请用所需配置覆盖此变量。

kernel_blacklist_modules: ['cramfs', 'freevxfs', 'jjfs2', 'hfs', 'hfsplus', 'udf', 'squashfs',
                           'dccp', 'sctp', 'rds', 'tipc']

需要被列入黑名单的模块。默认值适用于大多数系统。如果系统需要使用其中某些模块,请用所需配置覆盖此变量。

#kernel_autostart_modules: ['br_netfilter']

需要自启动的模块。

kernel_sysctl:
  • { name: fs.suid_dumpable, value: "0" }
  • { name: fs.protected_hardlinks, value: "1" }
  • { name: fs.protected_symlinks, value: "1" }
  • { name: fs.inotify.max_user_instances, value: "1024" }
  • { name: kernel.dmesg_restrict, value: "1" }
  • { name: kernel.yama.ptrace_scope, value: "1" }
  • { name: kernel.randomize_va_space, value: "2" }
  • { name: kernel.kptr_restrict, value: "1" }
  • { name: kernel.nmi_watchdog, value: "0" }
  • { name: net.ipv4.ip_forward, value: "0" }
  • { name: net.ipv4.conf.all.forwarding, value: "0" }
  • { name: net.ipv4.conf.all.send_redirects, value: "0" }
  • { name: net.ipv4.conf.default.send_redirects, value: "0" }
  • { name: net.ipv4.conf.all.accept_source_route, value: "0" }
  • { name: net.ipv4.conf.default.accept_source_route, value: "0" }
  • { name: net.ipv4.conf.all.accept_redirects, value: "0" }
  • { name: net.ipv4.conf.default.accept_redirects, value: "0" }
  • { name: net.ipv4.conf.all.secure_redirects, value: "0" }
  • { name: net.ipv4.conf.default.secure_redirects, value: "0" }
  • { name: net.ipv4.conf.all.log_martians, value: "1" }
  • { name: net.ipv4.conf.default.log_martians, value: "1" }
  • { name: net.ipv4.icmp_echo_ignore_broadcasts, value: "1" }
  • { name: net.ipv4.icmp_ignore_bogus_error_responses, value: "1" }
  • { name: net.ipv4.conf.all.rp_filter, value: "1" }
  • { name: net.ipv4.conf.default.rp_filter, value: "1" }
  • { name: net.ipv4.tcp_syncookies, value: "1" }
  • { name: net.ipv6.conf.all.disable_ipv6, value: "1" }
  • { name: net.ipv6.conf.default.disable_ipv6, value: "1" }

指定要在系统上配置的 sysctl 参数。默认配置适用于不作为路由器且不使用 IPv6 的系统。请检查规则,并在需要时覆盖此变量。

kernel_sysctl_flush_network_routes: yes

如果设置为 yes,将设置 net.ipv4.route.flush 和 net.ipv6.route.flush 为 1 并重新加载。

#kernel_udev_sata_link_power_mgmt: med_power_with_dipm

SATA 链接电源管理策略。有效值为 min_power、max_performance、medium_power、med_power_with_dipm。

#kernel_udev_autosuspend_ahci_devices: yes

自动挂起 AHCI 控制器和 ATA 设备。

#kernel_udev_autosuspend_scsi_devices: yes

自动挂起 SCSI 设备,驱动程序为 sd。

#kernel_udev_disable_bluetooth: no

禁用蓝牙。

#kernel_udev_disable_wake_on_lan: yes

禁用远程唤醒。

#kernel_udev_usb_autosuspend_devices:
#  - { vendor: '13fe', product: '5500' }   # Silicon power flash 驱动器
#  - { vendor: '1532', product: '0f13', autosuspend: 120 }    # 键盘

自动挂起的设备列表。 通常 Linux 已经为 USB 集线器和其他设备设置了自动挂起,所以不需要为这些设备使用此选项。请检查哪些设备被设置为自动或未设置,查看 /sys/bus/usb/devices/*/power/control。

#kernel_udev_pci_autosuspend_devices:
#  - { vendor: '0x8086', device: '0x0c00' }
#  - { vendor: '0x8086', device: '0x0412' }

自动挂起的 PCI 设备列表。

#kernel_udev_enable_wlan_powersave: yes

启用 WLAN 节能?

下面列出的变量对于目标系统不需要更改(请查看 vars/main.yml):

kernel_udev_reload_cmd: "udevadm control --reload-rules && udevadm trigger"

重新加载 udev 规则的命令。

依赖关系

无依赖。

示例剧本

- hosts: servers
  vars:
    kernel_disable_modules: ['cramfs', 'freevxfs', 'jjfs2', 'hfs', 'hfsplus', 'udf', 'vfat', 'squashfs']
    kernel_blacklist_modules: ['radeon', 'amdgpu']
    kernel_sysctl:
      - { name: net.ipv4.conf.all.forwarding, value: "0" }
      - { name: net.ipv4.conf.all.send_redirects, value: "0" }
      - { name: net.ipv4.conf.default.send_redirects, value: "0" }
      - { name: net.ipv4.conf.all.accept_source_route, value: "0" }
      - { name: net.ipv4.conf.default.accept_source_route, value: "0" }
      - { name: net.ipv4.conf.all.accept_redirects, value: "0" }
      - { name: net.ipv4.conf.default.accept_redirects, value: "0" }
      - { name: net.ipv4.conf.all.secure_redirects, value: "0" }
      - { name: net.ipv4.conf.default.secure_redirects, value: "0" }
      - { name: net.ipv4.conf.all.log_martians, value: "1" }
      - { name: net.ipv4.conf.default.log_martians, value: "1" }
      - { name: net.ipv4.icmp_echo_ignore_broadcasts, value: "1" }
      - { name: net.ipv4.icmp_ignore_bogus_error_responses, value: "1" }
      - { name: net.ipv4.conf.all.rp_filter, value: "1" }
      - { name: net.ipv4.conf.default.rp_filter, value: "1" }
      - { name: net.ipv4.tcp_syncookies, value: "1" }
    kernel_sysctl_flush_network_routes: yes

  roles:
     - { role: guidugli.kernel_config }

许可证

MIT / BSD

作者信息

该角色由 Carlos Guidugli 于 2020 年创建。

关于项目

Disable kernel modules and configure sysctl settings on linux

安装
ansible-galaxy install guidugli.kernel_config
许可证
mit
下载
102
拥有者