How to load and unload kernel modules in Linux

In this tutorial, you will learn how to load and unload kernel modules in Linux. When you install a Linux-like operating system, the kernel automatically installs the majority of device driver modules. It also allows you to install additional device drivers as modules using the commands modprobe and insmod once the installation is complete.

How to load and unload kernel modules in Linux

Normally, kernel modules are loaded automatically, but you may need to manually install new modules on occasion.

From time to time, you may want to unload/uninstall some modules are well.

The two major commands that can be used to load and unload kernel modules in Linux include:

While the two commands are used to achieve the same thing, most users will want to use modprobe instead, which is more clever and can handle module dependencies.

Modprobe can also be used to unload/remove loaded kernel modules.

How to load kernel modules in Linux

Load Kernel Modules using INSMOD command

The insmod (insert module) command can be used to load a kernel module. The whole path of the module must be specified here.

Kernel module files usually have .ko extensions, for example you can use the command bellow to insert bluetooth.ko module.

insmod /lib/modules/4.4.0-21-generic/kernel/drivers/cpufreq/bluetooth.ko

Load Kernel Modules using MODPROBE command

Using the modprobe command and the module name, you can add the module to the Linux kernel.

sudo modprobe bluetooth

List Loaded Kernel Modules

Using the lsmod command, you can see what kernel modules are currently loaded.

sudo lsmod | grep bluetooth

Except for the additional configuration file in /etc/modprobe.d/, Linux maintains a kernel module directory and configuration files under /lib/modules/'uname -r' /kernel/drivers/.

To list kernel drivers run the following command

ls /lib/modules/'uname -r'/kernel/drivers/

For my case when I run uname -r I get the following sample output;

5.11.0-38-generic

Now I can run the full command with value of the command uname -r used;

ls /lib/modules/5.11.0-38-generic/kernel/drivers/
accessibility  crypto    hwmon       md        parport    scsi         vdpa
acpi           dax       hwtracing   media     pci        siox         vfio
android        dca       i2c         memstick  pcmcia     slimbus      vhost
ata            dma       i3c         message   phy        soc          video
atm            edac      iio         mfd       pinctrl    soundwire    virt
auxdisplay     extcon    infiniband  misc      platform   spi          virtio
base           firewire  input       mmc       power      spmi         visorbus
bcma           firmware  iommu       most      powercap   ssb          vme
block          fpga      ipack       mtd       pps        staging      w1
bluetooth      gnss      irqchip     mux       ptp        target       watchdog
bus            gpio      isdn        net       pwm        tee          xen
char           gpu       leds        nfc       rapidio    thermal
clk            greybus   lightnvm    ntb       regulator  thunderbolt
counter        hid       macintosh   nvdimm    reset      tty
cpufreq        hsi       mailbox     nvme      rpmsg      uio
cpuidle        hv        mcb         nvmem     rtc        usb

You may encounter problems loading modules at times, or modules that are not loaded properly.

You can aggressively install or load modules to avoid these issues by using the '–force' option (-f) in the modprobe command.

modprobe -f floppy

If you continue to have issues or difficulties while loading the modules, you will need to debug this time.

You can determine the exact error or issue before or after installing the modules by activating debugging. To put it another way, debugging is the same as a dry-run of loading modules.

This form of debugging is enabled by the '-n' option in the modprobe command. This option forces the modprobe command to complete all module loading steps except the last.

modprobe -vn module_name

The ‘--show-depends‘ option in the modprobe command can also be used to display the module’s dependencies.

An example is shown below.

modprobe --show-depends e1000
insmod /lib/modules/5.11.0-38-generic/kernel/drivers/net/ethernet/intel/e1000/e1000.ko

How to unload kernel modules in Linux

You can unload loaded kernel modules in Linux using the commands:

Just like insmod does not a module with its dependencies, rmmod does not remove a module with its dependencies. You may want to use modprobe with -r option instead.

Unload kernel modules using rmmod command

The rmmod (remove module) command is used to unload a kernel module. The bluetooth.ko module will be unloaded or removed using the following example.

rmmod /lib/modules/4.4.0-21-generic/kernel/drivers/cpufreq/bluetooth.ko

Unload kernel modules using modprobe command

Use the modprobe command with the -r option and the module name to remove a module. e.g.

sudo modprobe -r bluetooth

To remove a kernel module, use the -r option in the modprobe command. Assume we wish to get rid of the floppy module.

modprobe -r floppy

After removing the floppy module from the kernel, type

lsmod | grep floppy

You should not be able to see anything. If you want to re-insert this module, refer to loading kernel module section above.

That concludes our guide on how to load and unload kernel modules in Linux.

Further Reading

man modprobe

man insmod

man rmmod

Other Tutorials

How to set and unset environment variables in Linux

How to list hardware information in Linux

How to Use uname Command on Linux

System administrator | Software Developer | DevOps

Leave a Comment