Linux modprobe Command Examples to View, Install, Remove Modules

本文深入介绍了Linux内核模块管理的相关命令与操作,包括如何列出所有可用模块、查看已加载模块、安装新模块、使用不同名称避免冲突以及卸载已加载模块。通过实例演示了在Ubuntu环境下利用modprobe命令进行各项操作。

modprobe utility is used to add loadable modules to the Linux kernel. You can also view and remove modules using modprobe command.

Linux maintains /lib/modules/$(uname-r) directory for modules and its configuration files (except /etc/modprobe.conf and /etc/modprobe.d).

In Linux kernel 2.6, the .ko modules are used instead of .o files since that has additional information that the kernel uses to load the modules. The example in this article are done with using modprobe on Ubuntu.

1. List Available Kernel Modules

modprobe -l will display all available modules as shown below.

$ modprobe -l | less
kernel/arch/x86/kernel/cpu/mcheck/mce-inject.ko
kernel/arch/x86/kernel/cpu/cpufreq/e_powersaver.ko
kernel/arch/x86/kernel/cpu/cpufreq/p4-clockmod.ko
kernel/arch/x86/kernel/msr.ko
kernel/arch/x86/kernel/cpuid.ko
kernel/arch/x86/kernel/apm.ko
kernel/arch/x86/kernel/scx200.ko
kernel/arch/x86/kernel/microcode.ko
kernel/arch/x86/crypto/aes-i586.ko
kernel/arch/x86/crypto/twofish-i586.ko

2. List Currently Loaded Modules

While the above modprobe command shows all available modules, lsmod command will display all modules that are currently loaded in the Linux kernel.

$ lsmod | less
soundcore          7264   1 snd
ppdev                6688   0
snd_page_alloc   9156    1 snd_pcm
psmouse            56180  0
lp                     8964    0

3. Install New modules into Linux Kernel

In order to insert a new module into the kernel, execute the modprobe command with the module name.

Following example loads vmhgfs module to Linux kernel on Ubuntu.

$ sudo modprobe vmhgfs

Once a module is loaded, verify it using lsmod command as shown below.

$ lsmod | grep vmhgfs
vmhgfs                 50772  0

The module files are with .ko extension. If you like to know the full file location of a specific Linux kernel module, use modprobe command and do a grep of the module name as shown below.

$ modprobe | grep vmhgfs
misc/vmhgfs.ko

$ cd /lib/modules/2.6.31-14-generic/misc

$ ls vmhgfs*
vmhgfs.ko

Note: You can also use insmod for installing new modules into the Linux kernel.

4. Load New Modules with the Different Name to Avoid Conflicts

Consider, in some cases you are supposed to load a new module but with the same module name another module got already loaded for different purposes.

If for some strange reasons, the module name you are trying to load into the kernel is getting used (with the same name) by a different module, then you can load the new module using a different name.

To load a module with a different name, use the modprobe option -o as shown below.

$ sudo modprobe vmhgfs -o vm_hgfs

$ lsmod  | grep vm_hgfs
vm_hgfs                   50772  0

5. Remove the Currently Loaded Module

If you’ve loaded a module to Linux kernel for some testing purpose, you might want to unload (remove) it from the kernel.

Use modprobe -r option to unload a module from the kernel as shown below.

modprobe -r vmhgfs
### 解决方案 #### 检查模块是否存在 如果执行 `modprobe vmmon` 时提示未找到模块,可能是模块未正确安装或编译。可以通过以下命令检查模块是否存在于系统中: ```bash ls /lib/modules/$(uname -r)/kernel/drivers/misc/vmware/ ``` 如果目录为空或不存在相关文件,则说明模块未正确安装[^1]。 #### 安装 VMware Tools 确保 VMware Tools 已正确安装。安装完成后,重新编译模块以适配当前内核版本: ```bash sudo vmware-modconfig --console --install-all ``` 此命令会根据当前运行的内核重新配置并编译 `vmmon` 和 `vmnet` 模块。如果安装成功,通常会显示类似以下消息: ``` The configuration of VMware Tools 10.0.5 build-3228253 for Linux for this running kernel completed successfully. ``` [^3] #### 手动加载模块 尝试手动加载模块以验证其可用性: ```bash sudo modprobe vmmon sudo modprobe vmnet ``` 如果加载失败,可能是因为模块未正确编译或内核头文件缺失。此时需要确保已安装正确的内核头文件和开发工具链: ```bash sudo yum groupinstall "Development Tools" sudo yum install kernel-devel-$(uname -r) ``` #### 编译模块 如果模块未正确编译,可以手动进入 VMware 提供的模块源代码目录进行编译: ```bash cd /usr/lib/vmware/modules/source/ tar xf vmmon.tar cd vmmon-only/ make sudo make install ``` 重复上述步骤以编译和安装 `vmnet` 模块[^1]。 #### 签名模块(适用于启用了 Secure Boot 的系统) 如果系统启用了 Secure Boot,可能需要对模块进行签名。以下是具体步骤: 1. 生成自签名证书: ```bash openssl req -new -x509 -newkey rsa:2048 -keyout VMWARE.priv -outform DER -out VMWARE.der -nodes -days 36500 -subj "/CN=VMware/" ``` 2. 签名模块: ```bash sudo /usr/src/linux-headers-$(uname -r)/scripts/sign-file sha256 ./VMWARE.priv ./VMWARE.der $(modinfo -n vmmon) sudo /usr/src/linux-headers-$(uname -r)/scripts/sign-file sha256 ./VMWARE.priv ./VMWARE.der $(modinfo -n vmnet) ``` 3. 导入证书到 MOK 数据库: ```bash sudo mokutil --import VMWARE.der ``` 重启后根据提示完成证书注册[^4]。 #### 验证模块加载状态 最后,验证模块是否成功加载: ```bash lsmod | grep -e vmmon -e vmnet ``` 如果模块已加载,应显示相关信息。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值