How to Load and Unload Kernel Modules in Linux
When we install Linux Like operating system at that time Linux kernel install most of device driver’s modules and after the installation it also allows us to install new device drivers as modules using the commands modprobe and insmod.
Normally kernel modules are loaded automatically but sometimes you need to install the additional modules as being manual . For instance you want to install device drivers of storage device and etc. For this there are some commands some of them are listed below.
lsmod stand for ‘list module‘. As the name suggests this command will list currently loaded kernel modules on your system.
[[email protected] ~]# lsmod
If you want to find a specific module. This can be network driver module (e1000) then you can do via grep command.
[[email protected] ~]# lsmod | grep e1000
modinfo stand for ‘module information‘. This command will show the information about a kernel module. For instance you want to view the information regarding network driver module:
[[email protected] ~]# modinfo e1000
Output of modinfo command clearly show version of this module, description which is showing the manufacture factory, license is GPL and other important information.
modprobe command is used to add and remove module from the kernel. Linux maintains kernel module directory under ‘/lib/modules/’uname -r’/kernel/drivers/‘ and configuration files(except for additional configuration file in /etc/modprobe.d/). If we want to look at kernel drivers then run the beneath command.
[[email protected] ~]# ls /lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/
If we need to add i8k kernel module which this module is using for “accessing SMM BIOS on Dell laptops”
[[email protected]nuxtechi ~]# modprobe i8k
if output of this command is being any error, then you can use ‘–quite‘ option, with this flag or option modprobe will not print error messages.
Note : Most of the time we use modprobe command to install kernel module though insmod is also used for loading the kernel module in linux.
Remove a Kernel Module
-r option in modprobe command is used to remove a kernel module. Let’s assume we want to remove the floppy module.
[[email protected] ~]# modprobe -r floppy [[email protected] ~]#
Here we remove floppy module from kernel after that you type
[[email protected] ~]# lsmod | grep floppy
and you should see nothing. If you want to add this module again you can type
[[email protected] ~]# modprobe floppy
At some point of time we may get issues while loading the modules or modules not loaded properly. To overcome these errors we can add or load modules forcefully using ‘–force’ option ( -f) in the modprobe command.
[[email protected] ~]# modprobe -f floppy
If we still face problems or errors while loading the modules, then this time we must do debugging.By enabling debugging we can find exact error or issue before or after installing the modules. In other words debugging is equivalent of dry-run of loading modules.
‘-n’ option in the modprobe command can enable this type of debugging. This option will force modprobe command to perform all module loading steps except the final one.
[[email protected] ~]# modprobe -vn 'module_name'
We can also see dependency of the module with using ‘–show-depends‘ in the modprobe command, example is shown below
[[email protected] ~]# modprobe --show-depends e1000 insmod /lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/net/ethernet/intel/e1000/e1000.ko [[email protected] ~]#
That’s all for this article. I hope you got an idea how to list, install and remove kernel module in Linux.