Linux常用命令-modprobe

命令

modprobe

描述

Add and remove modules from the Linux Kernel
添加或删除内核模块

用法

1
2
3
4
5
6
modprobe [options] [-i] [-b] modulename
modprobe [options] -a [-i] [-b] modulename [modulename...]
modprobe [options] -r [-i] modulename
modprobe [options] -r -a [-i] modulename [modulename...]
modprobe [options] -c
modprobe [options] --dump-modversions filename

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Options:
Management Options:
-a, --all 将每个非参数视为要添加或删除的模块名
-r, --remove 删除模块
--remove-dependencies 同时删除依赖模块
-R, --resolve-alias 显示与别名匹配的文件名
--first-time 如果模块已经添加或删除则报错
-i, --ignore-install 忽略添加命令
-i, --ignore-remove 忽略删除命令
-b, --use-blacklist 将黑名单应用到已解析的别名
-f, --force 强制添加或删除,不推荐使用
--force-modversion 忽略模块版本
--force-vermagic 忽略模块magic

Query Options:
-D, --show-depends 显示模块依赖关系
-c, --show-config 显示已知的配置文件,如模块的alias别名

General Options:
-n, --dry-run 不执行操作,仅仅测试
-C, --config=FILE 使用文件代替默认配置文件
-d, --dirname=DIR 使用DIR作为/lib/modules的文件系统根目录
-S, --set-version=VERSION 使用VERSION代替`uname -r`
-s, --syslog 显示到syslog
-q, --quiet 关闭消息
-v, --verbose 显示详情

注意

Linux内核模块管理命令: kmod是主程序,其他命令如lsmod,insmod,rmmod,modinfo,modprobe,depmod均为软链接
推荐使用lsmod来显示内核模块,使用modprobe来添加或删除内核模块

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# kmod是主要程序,其他命令均为软链接
$ ll /usr/sbin/*mod
lrwxrwxrwx. 1 root root 11 Nov 27 05:42 /usr/sbin/depmod -> ../bin/kmod
lrwxrwxrwx. 1 root root 11 Nov 27 05:42 /usr/sbin/insmod -> ../bin/kmod
lrwxrwxrwx. 1 root root 11 Nov 27 05:42 /usr/sbin/lsmod -> ../bin/kmod
lrwxrwxrwx. 1 root root 11 Nov 27 05:42 /usr/sbin/rmmod -> ../bin/kmod
$ ll /usr/sbin/mod*
lrwxrwxrwx. 1 root root 11 Nov 27 05:42 /usr/sbin/modinfo -> ../bin/kmod
lrwxrwxrwx. 1 root root 11 Nov 27 05:42 /usr/sbin/modprobe -> ../bin/kmod
$ ll /usr/bin/kmod
-rwxr-xr-x. 1 root root 146680 Apr 1 2020 /usr/bin/kmod
# 查看模块
$ lsmod | head
Module Size Used by
ext4 584133 0
mbcache 14958 1 ext4
jbd2 107486 1 ext4
vmw_vsock_vmci_transport 30577 1
vsock 36367 2 vmw_vsock_vmci_transport
iosf_mbi 15582 0
crc32_pclmul 13133 0
ghash_clmulni_intel 13273 0
ppdev 17671 0
$ lsmod | grep xfs
xfs 997727 2
libcrc32c 12644 1 xfs
# 系统模块目录/lib/modules/$(uname -r)
$ ll /lib/modules/$(uname -r)/
total 3300
lrwxrwxrwx. 1 root root 44 Jan 7 22:48 build -> /usr/src/kernels/3.10.0-1160.11.1.el7.x86_64
drwxr-xr-x. 2 root root 6 Dec 19 00:39 extra/
drwxr-xr-x. 12 root root 128 Jan 7 22:48 kernel/
-rw-r--r--. 1 root root 860326 Jan 7 22:48 modules.alias
-rw-r--r--. 1 root root 819744 Jan 7 22:48 modules.alias.bin
-rw-r--r--. 1 root root 1333 Dec 19 00:39 modules.block
-rw-r--r--. 1 root root 7391 Dec 19 00:38 modules.builtin
-rw-r--r--. 1 root root 9440 Jan 7 22:48 modules.builtin.bin
-rw-r--r--. 1 root root 273209 Jan 7 22:48 modules.dep
-rw-r--r--. 1 root root 382108 Jan 7 22:48 modules.dep.bin
-rw-r--r--. 1 root root 361 Jan 7 22:48 modules.devname
-rw-r--r--. 1 root root 140 Dec 19 00:39 modules.drm
-rw-r--r--. 1 root root 69 Dec 19 00:39 modules.modesetting
-rw-r--r--. 1 root root 1810 Dec 19 00:39 modules.networking
-rw-r--r--. 1 root root 97935 Dec 19 00:38 modules.order
-rw-r--r--. 1 root root 569 Jan 7 22:48 modules.softdep
-rw-r--r--. 1 root root 397513 Jan 7 22:48 modules.symbols
-rw-r--r--. 1 root root 486211 Jan 7 22:48 modules.symbols.bin
lrwxrwxrwx. 1 root root 5 Jan 7 22:48 source -> build
drwxr-xr-x. 2 root root 6 Dec 19 00:39 updates/
drwxr-xr-x. 2 root root 95 Jan 7 22:48 vdso/
drwxr-xr-x. 2 root root 6 Jan 7 22:48 weak-updates/
# 查看模块基本信息
$ ll /lib/modules/3.10.0-1160.el7.x86_64/kernel/fs/xfs/xfs.ko.xz
-rw-r--r--. 1 root root 335716 Oct 20 00:30 /lib/modules/3.10.0-1160.el7.x86_64/kernel/fs/xfs/xfs.ko.xz
$ modinfo /lib/modules/3.10.0-1160.el7.x86_64/kernel/fs/xfs/xfs.ko.xz
filename: /lib/modules/3.10.0-1160.el7.x86_64/kernel/fs/xfs/xfs.ko.xz
license: GPL
description: SGI XFS with ACLs, security attributes, no debug enabled
author: Silicon Graphics, Inc.
alias: fs-xfs
retpoline: Y
rhelversion: 7.9
srcversion: ACB9CF86C248DD09DC2B428
depends: libcrc32c
intree: Y
vermagic: 3.10.0-1160.el7.x86_64 SMP mod_unload modversions
signer: CentOS Linux kernel signing key
sig_key: E1:FD:B0:E2:A7:E8:61:A1:D1:CA:80:A2:3D:CF:0D:BA:3A:A4:AD:F5
sig_hashalgo: sha256
# 查看iptables相关模块,可以看到主模块是ip_tables,其他iptables模块均依赖该模块
$ lsmod | grep iptable
iptable_security 12705 0
iptable_mangle 12695 0
iptable_raw 12678 0
iptable_nat 12875 0
nf_nat_ipv4 14115 1 iptable_nat
iptable_filter 12810 1
ip_tables 27126 5 iptable_security,iptable_filter,iptable_mangle,iptable_nat,iptable_raw
$ modinfo iptable_filter
filename: /lib/modules/3.10.0-1160.el7.x86_64/kernel/net/ipv4/netfilter/iptable_filter.ko.xz
description: iptables filter table
author: Netfilter Core Team <coreteam@netfilter.org>
license: GPL
retpoline: Y
rhelversion: 7.9
srcversion: DAB2F67E766E485B0779D45
depends: ip_tables
intree: Y
vermagic: 3.10.0-1160.el7.x86_64 SMP mod_unload modversions
signer: CentOS Linux kernel signing key
sig_key: E1:FD:B0:E2:A7:E8:61:A1:D1:CA:80:A2:3D:CF:0D:BA:3A:A4:AD:F5
sig_hashalgo: sha256
parm: forward:bool

# 查询发现没有加载ext4模块
$ lsmod |grep ext4
# 添加ext4模块
$ modprobe ext4
$ lsmod |grep ext4
ext4 584133 0
mbcache 14958 1 ext4
jbd2 107486 1 ext4
# 删除模块
$ modprobe -r ext4
$ lsmod |grep ext4
# modules.dep.bin该文件记录了模块位置及依赖关系
$ ll /lib/modules/3.10.0-1160.11.1.el7.x86_64/ | grep dep
-rw-r--r--. 1 root root 273209 Jan 7 22:48 modules.dep
-rw-r--r--. 1 root root 382108 Jan 7 22:48 modules.dep.bin
-rw-r--r--. 1 root root 569 Jan 7 22:48 modules.softdep
$ cd /lib/modules/3.10.0-1160.11.1.el7.x86_64/
$ mv modules.dep.bin modules.dep.bin.old
# 重新生成新的模块包文件,如果是新加模块包,要将模块放到kernel对应目录下,然后重新生成modules文件
$ depmod -a
$ ll modules.dep*
-rw-r--r-- 1 root root 273209 Feb 26 20:43 modules.dep
-rw-r--r-- 1 root root 382108 Feb 26 20:43 modules.dep.bin
-rw-r--r--. 1 root root 382108 Jan 7 22:48 modules.dep.bin.old
$ lsmod |grep ext4
$ modprobe ext4
$ lsmod |grep ext4
ext4 584133 0
mbcache 14958 1 ext4
jbd2 107486 1 ext4