Admin... by accident!

You may have chosen to be an admin. I didn't!

  • Home
  • FreeBSD
  • GNU/Linux
  • Security
  • Network
  • Virtualization
  • Politics
  • Github
  • Donate
  • Me

How to load and unload kernel modules in Linux

May 15, 2023 by Albert Valbuena

Kernel modules permit enabling hardware features on a given system. For example, if we need to read from a particular filesystem from a hard drive, we need to load a particular kernel module. Or use a specific network card, a sound card or sound device, a video display, etc. This is mostly done automatically in many instances but knowing how to load and unload kernel modules in Linux is a good thing anytime you find yourself in trouble.

If you find the articles in Adminbyaccident.com useful to you, please consider making a donation.

Use this link to get $200 credit at DigitalOcean and support Adminbyaccident.com costs.

Get $100 credit for free at Vultr using this link and support Adminbyaccident.com costs.

Mind Vultr supports FreeBSD on their VPS offer.

 

List current modules

First of all, let’s list the loaded modules in a given system, so we can see what is already running. For that we’ll make use of the ‘lsmod’ command.

Let’s imagine we need to load a Linux kernel module so we can read a FAT formatted hard drive. We need to load two kernel modules to acquire that capacity. We’ll use the ‘insmod’ command and we will also have to point to the full path when loading the modules.

Info to load modules

Let’s first look for information related to the module we want to load which is called vfat. To read such information we need to use the ‘modinfo’ command.

Reading carefully we realize there’s a dependency for the vfat module, and that is called simply fat. We can look for that too.

It seems that the ‘fat’ module has no dependencies, so we can now proceed and load the two of them.

Loading the modules

We can load kernel modules with the ‘insmod’ command. And we can do that by simply invoking the command and pointing to the path where the module is sitting on the system. Information which is pointed out in the ‘filename’ parameter after issuing the ‘modinfo’ command.

Let’s check if the module has been loaded correctly by issuing ‘lsmod’ and using grep to pull out the content we are looking for.

And yes, it’s been loaded, otherwise the result of the above command would have resulted in blank.

With the ‘fat’ module loaded we will not be able to read from a modern formatted drive with a FAT filesystem on it. We need to load a second module named ‘vfat’. All we need is to repeat a similar operation as we have done with the ‘fat’ module.

We have first looked for the ‘vfat’ module’s location, and that has been provided by the ‘modinfo’ command, more specifically by the ‘filename’ parameter in the result.

Then we have used the ‘insmod’ command to load the module.

Lastly, we have checked if it was correctly loaded with the ‘lsmod’ command and grep’s assistance.

What if we have finished using that FAT formatted hard drive and we want to unload those modules?

Unloading modules

To unload modules we have another command, called ‘rmmod’. The shortening of ‘remove module’. And as we did with the loading, it’s very easy to use, since we only have to invoke ‘rmmod’ and point out which is the full path of the kernel module we want to unload.

We have unloaded the ‘vfat’ module. Let’s unload the ‘fat’ module.

With both modules unloaded, let’s know check if they have been really unloaded. We’ll use, again, the ‘lsmod’ command working in conjunction with ‘grep’.

The empty output ensures us both modules have been unloaded. Grepping ‘fat’ grants us ‘fat’ as well as ‘vfat’ are covered since the string ‘fat’ is the full one for one of the module’s name and it is also contained in the other’s name.

Loading modules without a hassle.

All of the above is nice but not as nice as it can get.

There’s a catch with using ‘insmod’ and ‘rmmod’. And that is they are not resolving dependencies. They are quite literal in what they load and unload to and from the kernel. And that is problematic, since in order to use VFAT to read a file system formatted in FAT we need to use the ‘fat’ module alongside the ‘vfat’ one. To know that we need to use the ‘modinfo’ command. With more complex dependency models this operation gets trickier and prone to errors. And no one wants to make mistakes when touching kernel infrastructure. So, what solves this?

The ‘modprobe’ command does all the above and it does also resolve dependencies for us. Let’s demonstrate this with the same example as above.

We want to read a FAT formatted drive and all we know is we need to load the ‘vfat’ kernel module.

What has been loaded?

Both have been loaded, the ‘vfat’ module and its dependency ‘fat’ module. With one command and without having to declare the full path.

What if we do not need to read from that filesystem anymore? Let’s unload the ‘vfat’ module.

What is loaded now?

As it happened when they were loaded together, both ‘fat’ and ‘vfat’ modules have been unloaded.

Summary

lsmod: list the currently loaded kernel modules.

insmod: loads specific modules and needs the full path. It doesn’t resolve dependencies.

modinfo: provides information from a specific module.

rmmod: unloads specific modules and needs the full path. It doesn’t resolve dependencies.

modprobe: loads and unloads modules, resolves dependencies and doesn’t need full path declaration.

Conclusion

Knowing how a system works can help us tremendously, specially when automount features don’t work properly or we are given a task to resolve with limited capabilities as it often happens when dealing with servers or challenging scenarios. Knowing how to load and unload kernel modules in Linux can help us out in some of those scenarios.

If you find the articles in Adminbyaccident.com useful to you, please consider making a donation.

Use this link to get $200 credit at DigitalOcean and support Adminbyaccident.com costs.

Get $100 credit for free at Vultr using this link and support Adminbyaccident.com costs.

Mind Vultr supports FreeBSD on their VPS offer.

 

 

 

Filed Under: GNU/Linux, How To's

Recent Posts

  • How to install Redis for WordPress on FreeBSD
  • How to compile cloudflared in FreeBSD 13/14
  • How to configure FreeBSD to use a webcam (version 12 and 13)
  • Symbolic and Hard Links in UNIX and Linux
  • How to import iocage jails to Bastille on FreeBSD 13
  • How to load and unload kernel modules in Linux
  • How to use find in GNU/Linux and FreeBSD
  • How to install Mate on FreeBSD 12/13
  • How to install Nessus 10 on FreeBSD 12
  • How to enable TLS traffic from the origin server on Cloudflare Argo Tunnel
  • How to use Cloudflare’s Argo Tunnel service to publish a website on FreeBSD 12/13
  • How to setup MariaDB master-slave replication on FreeBSD
  • How to upload a FreeBSD custom image on DigitalOcean
  • How to install Drupal 9 on FreeBSD 13.0
  • How to manage site visitors based on IP Geolocation
  • How to enable Geolocation in AWStats on FreeBSD 13.0
  • How to install AWStats on FreeBSD 13.0
  • How to configure Modsecurity 3 for WordPress on FreeBSD
  • How to configure Apache HTTP with a TLS reverse proxy backend on FreeBSD
  • How to detect a WAF – Web Application Firewall

Archives

  • November 2024
  • October 2024
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • April 2022
  • March 2022
  • October 2021
  • September 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • September 2018
  • June 2018
  • May 2018
  • April 2018
  • February 2018
  • January 2018
  • November 2017
  • April 2017

RSS Admin… by accident!

  • How to install Redis for WordPress on FreeBSD
  • How to compile cloudflared in FreeBSD 13/14
  • How to configure FreeBSD to use a webcam (version 12 and 13)
  • Symbolic and Hard Links in UNIX and Linux
  • How to import iocage jails to Bastille on FreeBSD 13
  • How to load and unload kernel modules in Linux
  • How to use find in GNU/Linux and FreeBSD
  • How to install Mate on FreeBSD 12/13
  • How to install Nessus 10 on FreeBSD 12
  • How to enable TLS traffic from the origin server on Cloudflare Argo Tunnel

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in