如何将内核模块列入黑名单?

Rin*_*ail 50 kernel

如何禁用加载不必要的内核模块。内核 3.2.4

Lnx*_*lck 48

Note: blacklisting will not work for modules which are built into the kernel image (i.e. not loaded via a separate .ko file. The only way to disable such modules is via a kernel parameter (if available) or by recompiling the kernel.

Just open your /etc/modprobe.d/blacklist file and add drivername using following syntax:

blacklist driver-name
Run Code Online (Sandbox Code Playgroud)

EDIT: In later versions since 12.10 (12.04?) the file is /etc/modprobe.d/blacklist.conf

Reboot your box and use lsmod command to show the status of modules in the Linux Kernel

Note: here driver-name is the name of your desired blacklist driver. For example, If you wanted to disable the NIC card driver, you can find the name of kernel driver for your LAN card by using the command lspci -v command in a terminal.
For Example my output was :

........
........ 
6:00.0 Ethernet controller: Broadcom Corporation NetLink BCM5906M Fast Ethernet PCI Express (rev 02)
    Subsystem: Lenovo Device 3861
    Flags: bus master, fast devsel, latency 0, IRQ 46
    Memory at b8000000 (64-bit, non-prefetchable) [size=64K]
    Expansion ROM at  [disabled]
    Capabilities: 
    Kernel driver in use: tg3
    Kernel modules: tg3
........
........

Here, I see the driver is tg3. so you need to write tg3(or your driver) in the place of driver-name.

Plenty of info can be found here.

  • 就我而言(`Lubuntu 12.10`),没有 `/etc/modprobe.d/blacklist` 文件。有一个`/etc/modprobe.d/blacklist.conf`文件 (8认同)
  • @LnxSlck 我正在解决之前评论中表达的困惑。我不需要帮助。痴迷于不发布旧内容是怎么回事?就让它腐烂吗?人们仍然会发现这一点,其他人也会有我提出的相同问题。 (5认同)
  • `blacklist.conf` 文件不需要存在。你可以在那里放一个名为“my-mom-is-awesome”的文件,它会起作用。如果你想制作一个特定的文件只是为了将一个特定的东西列入黑名单,你可以随意起一个你喜欢的名字,比如 `blacklist-nouveau` 或其他什么。 (3认同)
  • 我必须使用“sudo update-initramfs -u”更新 initramfs 才能使更改生效。 (3认同)
  • 最好将用户列入黑名单的模块保存在单独的文件中,以避免升级期间发生冲突(请参阅[serverfault 上的此评论](https://serverfault.com/questions/162007/how-to-permanently-disable- a-kernel-module-kvm-intel-in-ubuntu-10-04#comment139673_162010))。 (2认同)

Pan*_*her 29

您还可以在使用语法启动时在 grub 命令行(linux 行)上临时将它们列入黑名单

module_to_blacklist.blacklist=yes
Run Code Online (Sandbox Code Playgroud)

  • 这和`modprobe.blacklist=module_to_blacklist`有什么区别? (5认同)
  • 或者使用内核参数`modprobe.blacklist=module_to_blacklist`(详见`man modprobe`) (5认同)
  • 这个“临时”有多长?直到下次启动? (2认同)

小智 16

在至少 Ubuntu 16.04 LTS 中将模块列入黑名单的另一种方法是将以下行添加到内核命令行:

modprobe.blacklist=MODULE_NAME
Run Code Online (Sandbox Code Playgroud)

使用 /etc/modprobe 系统是最好的方法,但这是一种替代方法,可以通过在启动时编辑 GRUB 命令行来紧急使用。

这也可以通过编辑 /etc/default/grub 并添加到GRUB_CMDLINE_LINUX_DEFAULT变量来永久化。例如,在我的 /etc/default/grub 中,我有:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash modprobe.blacklist=nouveau"
Run Code Online (Sandbox Code Playgroud)

然后我跑update-grub2,然后update-initramfs -u。重新启动后,您将摆脱该模块,只要启动后没有加载它。

此方法也适用于 EL 变体(RHEL、CentOS、SciLinux),但您必须使用该发行版的方法来更新 grub 和 initrd。

(注意那些试图将 nouveau 列入黑名单的人:确保不要通过运行加载 X systemctl set-default multi-user.target,否则当 X 启动时它会再次加载 nouveau!)


小智 5

在最近的版本中,您需要在黑名单文件中使用 install 指令

install modulename /bin/false
Run Code Online (Sandbox Code Playgroud)

将上面的“modulename”替换为模块的名称。这将强制阻止其加载。

您可以在 modprobe.conf 的手册中找到有关安装指令的更多信息

man modprobe.conf
Run Code Online (Sandbox Code Playgroud)

  • 这非常有帮助。事实证明,即使在 `/etc/modprobe/blacklist.conf` 中有一个黑名单条目,模块仍然可以通过 `modprobe <module_name>` 手动加载。使用 `install <module_name> /bin/false` 方法可以根据需要失败。 (2认同)