How to Load and Unload Kernel Modules in Linux

A kernel module is a program which can loaded into or unloaded from the kernel upon demand, without necessarily recompiling it (the kernel) or rebooting the system, and is intended to enhance the functionality of the kernel.

In general software terms, modules are more or less like plugins to a software such as WordPress. Plugins provide means to extend software functionality, without them, developers would have to build a single massive software with all functionalities integrated in a package. If new functionalities are needed, they would have to be added in new versions of a software.

Likewise without modules, the kernel would have to be built with all functionalities integrated directly into the kernel image. This would mean having bigger kernels, and system administrators would need to recompile the kernel every time a new functionality is needed.

A simple example of a module is a device driver – which enables the kernel to access a hardware component/device connected to the system.

Suggested Read: How to Find Out What Version of Linux You Are Running

List All Loaded Kernel Modules in Linux

In Linux, all modules end with the .ko extension, and they are normally loaded automatically as the hardware is detected at system boot. However a system administrator can manage the modules using certain commands.

To list all currently loaded modules in Linux, we can use the lsmod (list modules) command which reads the contents of /proc/modules like this.

# lsmod
List Kernel Modules in Linux
Module                  Size  Used by
rfcomm                 69632  2
pci_stub               16384  1
vboxpci                24576  0
vboxnetadp             28672  0
vboxnetflt             28672  0
vboxdrv               454656  3 vboxnetadp,vboxnetflt,vboxpci
bnep                   20480  2
rtsx_usb_ms            20480  0
memstick               20480  1 rtsx_usb_ms
btusb                  45056  0
uvcvideo               90112  0
btrtl                  16384  1 btusb
btbcm                  16384  1 btusb
videobuf2_vmalloc      16384  1 uvcvideo
btintel                16384  1 btusb
videobuf2_memops       16384  1 videobuf2_vmalloc
bluetooth             520192  29 bnep,btbcm,btrtl,btusb,rfcomm,btintel
videobuf2_v4l2         28672  1 uvcvideo
videobuf2_core         36864  2 uvcvideo,videobuf2_v4l2
v4l2_common            16384  1 videobuf2_v4l2
videodev              176128  4 uvcvideo,v4l2_common,videobuf2_core,videobuf2_v4l2
intel_rapl             20480  0
x86_pkg_temp_thermal    16384  0
media                  24576  2 uvcvideo,videodev
....

How to Load and Unload (Remove) Kernel Modules in Linux

To load a kernel module, we can use the insmod (insert module) command. Here, we have to specify the full path of the module. The command below will insert the speedstep-lib.ko module.

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

To unload a kernel module, we use the rmmod (remove module) command. The following example will unload or remove the speedstep-lib.ko module.

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

How to Manage Kernel Modules Using modprobe Command

modprobe is an intelligent command for listing, inserting as well as removing modules from the kernel. It searches in the module directory /lib/modules/$(uname -r) for all the modules and related files, but excludes alternative configuration files in the /etc/modprobe.d directory.

Here, you don’t need the absolute path of a module; this is the advantage of using modprobe over the previous commands.

To insert a module, simply provide its name as follows.

# modprobe speedstep-lib

To remove a module, use the -r flag like this.

# modprobe -r speedstep-lib

Note: Under modprobe, automatic underscore conversion is performed, so there is no difference between _ and while entering module names.

For more usage info and options, read through the modprobe man page.

# man modprobe

Do not forget to check out:

  1. How to Change Kernel Runtime Parameters in a Persistent and Non-Persistent Way
  2. How to Install or Upgrade to Latest Kernel Version in CentOS 7
  3. How to Upgrade Kernel to Latest Version in Ubuntu

That’s all for now! Do you have any useful ideas, that you wanted us to add to this guide or queries, use the feedback form below to drop them to us.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

6 Comments

Leave a Reply
  1. What happens if we try to load an already loaded module?

    Eg –

    # modprobe speedstep-lib
    

    then again,

    # modprobe speedstep-lib
    

    Is there any downside to this? How does the command work internally?

    Reply
  2. This sounds like a great way to cause kernel panics when people start using this without knowing what they are doing (unloading network drivers for instance).

    Reply
    • @Arlow

      Yes, one must know what they are doing before trying this. All we are trying to show is how kernel modules can be loaded and unloaded.

      Reply
    • @Sham

      Thanks, hope you liked it. To view info about a module, use the modinfo command like this:
      modinfo speedstep-lib

      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.