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

Posted on in Categories , , , , last updated August 3, 2017

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
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
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: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

21 comment

  1. Hello,
    I use Ubuntu 11.4 (Linux 2.6.38)
    I tried both the files modules.conf and rc.modules.
    Unfortunately the modules is not loaded.
    Do you have any idea?
    Thanks,
    Yacob.

  2. My understanding is that modprobe.conf and modules.conf are only configuration files. They do not invoke modprobe. All they do is provide modprobe with information about what it should do when it is invoked.

    Scripts that are run immediately after the boot (in the start up sequence) are called rc scripts. Many systems have a script called rc.local. This script is arranged in such a way (normally with symlinks) that it is executed as the last script in the start up sequence. This is a good place to put additional commands that are required and which have not been invoked already. The normal location of this script is /etc/rc.d/rc.local.

    Therefore if the modprobe command is added to that script it will be executed at the end, and before a login shell prompt is provided. Determine the location of modprobe. If, for example, it is /sbin/modprobe, then the end of the rc.local file should look something like this:
    # Put your required modprobe command here:
    /sbin/modprobe name-of-module

    Note that if the module in question requires options, then a place to put these is in /etc/modprobe.conf, because when modprobe runs it will read that configuration file and pick up any required options from there.

    1. Thanks Steve,

      Your steps helped me. It works.
      I wanted to bring 8021q after every reboot.
      I added it in /etc/rc.local file.
      For me /etc/rc.d/rc.local doesn’t exist, I have Ubuntu 14.04 platform.

      1. Hi Rohan,

        I am glad that helped you.

        I use “linux from scratch”. That is a system in which the operating system is built step by step, not simply installed from some disk or web site. It takes a long time but can give some improvements in understanding and control.

        Different distributions tend to store files (including configuration files) in different places. It appears that with Ubuntu there is a configuration file called /etc/modules which lists the names of modules to be loaded at boot time. If so then that might be a better place to put the 8021q module name. rc.local tends to be used for “local” after thoughts – anything unusual that needs to be done for that one host. There is a web page that describes Ubuntu’s /etc/modules here:
        https://help.ubuntu.com/community/Loadable_Modules

        The use of the 8021q module for vlan’s in Ubuntu is described here:
        https://wiki.ubuntu.com/vlan
        The section “Making it permanent”, which comes at the end of the article, seems to be relevant in your case. If that’s right then it might be tidier to do it that way than to use rc.local (even though it does work).

  3. Hi all,
    i am new buddy to Linux module programming.I am facing same problem in loading a module at boot time in Ubuntu 11.04. Can any one tell me the perfect steps to load the module at the boot time.

    Thanks.

  4. same problem here. I did change /etc/modules, but where do I put the module object itself so moprob can find it?

    To put the question in another way, what are the default lcoation that modprobe look for modules?

    thanks

  5. i i tried to put the .KO file with the all drivers .KO file.But that doesn’t worked.
    The other way to do this is configuring the Kconfig file and recompile the kernel.
    But i am not getting proper documentation.

  6. I figured that out.

    add a line to /etc/modules for your module (without .ko)
    copy the module file to /lib/modules/
    do

    sudo depmod -a
    reboot and it worked for me on Ubuntu 12.04

  7. Atul,

    quite often there is no need for a modprobe configuration file. As I said before (see previous remark from November, 2011), modprobe is not invoked by that configuration file. Its just that if you need to provide it with additional information, then that can be done from the configuration file. The name used for the configuration file has changed a bit between different versions, and sometimes it is given as a file in a directory, modprobe.d. On my workstation (built from scratch) there is no modprobe configuration file. Its not needed.

    The critical thing usually is making the kernel aware of modules. To do this you have to put the module in the right place, and run depmod to update the module dependencies.

    Perhaps if you explained what you were trying to do, then it would be possible to provide a more specific explanation.

Leave a Comment