Linux: How to load a kernel module automatically at boot time

How do I Load a Linux kernel module automatically at boot time so that my hardware automatically recognized during system boot sequence?

Linux kernel follows modular kernel design. Loadable Kernel Modules (LKM) are object files that contain code to extend the running kernel or so-called base kernel. LKM’s are typically used to add support for new hardware, filesystems, NICs and more.

Config file

Loading a kernel module is an essential task. You need to edit the file named /etc/modules or put a new config file in /etc/modules-load.d/ directory. Use any one of the methods for loading kernel modules. The configuration file consists of a set of lines. All empty lines, and all text on a line after a #, will be ignored. The file /etc/modules (or other files in /etc/modules-load.d/) is used if new hardware is added after installation and the hardware requires a kernel module, the system must be configured to load the proper kernel module for the new hardware.

Examples

For example, if a system included an IDE CD-ROM, the module configuration file contains the following 3 lines:
# vi /etc/modules
Append following lines:
ide-cd
ide-core
cdrom

Save and close the file. Reboot the system for testing purpose:
# reboot
Another option is to load drivers without rebooting the system. Use the modprobe command:
# modprobe {driver-name}
# modprobe ide-cd
# modprobe ide-cd cdrom

Please note that if you are using Debian Linux or Ubuntu Linux use the file /etc/modules file instead of /etc/modules.conf (which works on on an older version of Red Hat/Fedora/CentOS Linux. These days it is better to use the directory /etc/modules-load.d/ on all Linux distros.

A note about modern Linux kernels

These days udev used for for automatic module handling. There is no need to put modules in any configuration file as udev takes care of it. However, sometimes you still need add an extra module during the boot process, or blacklist another one for your Linux laptop or server to function correctly. For example, kernel modules can be loaded during boot a boot in files under /etc/modules-load.d/. For example:
# cat /etc/modules-load.d/kvm.conf
kvm
kvm_intel

Task: See what kernel modules are currently loaded

Type the lsmod command:
$ lsmod
OR
$ lsmod | more
Sample outputs:

Fig.01: Obtaining information about currently loaded modules/drivers

To get information about a module, run modinfo command

$ modinfo {module_name}
For example get info about a module named igb:
$ modinfo igb
Sample outputs:

Fig.02: Find information about a module

To unload/remove a module

The syntax is as follows for the rmmod command
$ sudo rmmod {module_name}
OR
$ sudo modprobe -r {module_name}

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.