In the following the loading and removing of kernel modules at runtime is covered and which commands are used for this purpose, these files usually contain the driver code.

Table of Contents

Load Kernel Module

With the command insmod the module can be loaded into the running kernel. In addition to that, required options can be passed to the program. At the beginning of the loading process dependencies on other modules are checked, but not resolved. Error messages are displayed, but not success messages. In case of an error message, the required modules must be installed first and then the desired module.

Explanation of the output
  • Line 1 – With cd (change directory) and the path you change to the directory
  • Line 3 – The command ls (list) displays the content of the directory
  • Lines 4 and 5 – Both of them show the module files compressed with MODULE_COMPRESS_XZ
  • Line 7lsmod prints the loaded kernel modules, the module cpufreq_stats.ko.xz is not loaded yet
  • Line 15 – insmod with the module specification loads the module into the kernel
  • Line 18 – Note: insmod with path specification loads the module into the kernel, if you are in another directory and want to load the module from there
  • Line 22 – The module cpufreq_stats is loaded into the kernel

Remove Kernel Module

The removal of a module is done with the command rmmod and the name of the module. Success messages are not displayed, but error messages are shown.

Explanation of the output
  • Line 1 – lsmod outputs the loaded kernel modules
  • Line 3 – The module cpufreq_stats is loaded
  • Line 11 – rmmod with the module specification removes the module from the system kernel, a path specification is not necessary for removing a module
  • Line 13 – lsmod shows the loaded kernel modules again and the module cpufreq_stats is not listed
  • Line 19 – snd is a module which is used by other modules
  • Line 22rmmod snd is used to remove the module
  • Line 23 – An error message is displayed because it is used by other modules

Swiss Knife of Module Management

The program modprobe offers more functions than insmod and rmmod. It considers the module dependencies which are defined in the file /usr/lib/modules/KERNEL-VERSION/modules.dep when loading and removing modules. The program also loads the modules required by a specific module. Furthermore, it considers the module parameters listed in the directory /etc/modprobe.d/*. The path specification like insmod is not necessary for the modprobe command. With the command modprobe -r module_name the desired module is removed.

Extract from the modules.dep file
Structure of the Definition

In the /usr/lib/modules/KERNEL-VERSION/modules.dep file the modules are defined including the path specifications. After the colon, the required modules with the path specifications are listed one after the other, a space is used as a separator.