I have a Linux kernel module which I compiled dynamically. How is it added to startup? There are lot of .ko files in /lib/modules. How is priority set for loading these modules?

2 Answers 2


They are not loaded automatically at start-up or any other time, although a lot of them do end up being loaded during boot. There are three different mechanisms by which this happens:

  • Userspace request: Which covers everything from init services to udev to the command-line. Init or udev are probably the most straightforward means if you want to load a specific module at boot time.

  • Hotplugged device: When you connect something to, e.g., USB or PCI, the kernel detects this and requests an appropriate module based on how the device identifies itself.

  • Needed protocol or other implementation: When the kernel needs to do something, such as read a filesystem, and discovers it lacks the knowledge to do so, it will request a module.

Notice that for the last two I used the phrase "request a module" -- this is because the kernel actually loads via a userspace daemon, kmod which executes /sbin/modprobe. According to Wolfgang Mauerer in Linux Kernel Architecture, there are only ~100 different points in the 2.6 kernel where it calls an internal request_module() function.

modprobe uses a database of installed MODULE_ALIAS's. These are specified in the module source code explicitly, or derived from it's MODULE_DEVICE_TABLE, which is a list of OEM device IDs that the module services.


Many systems are set up to use an initrd or initramfs. These are filesystem images that are loaded by the bootloader and made available to the kernel before it mounts the root partition. This allows drivers that are necessary to mount the root partition (disk drivers, filesystem drivers, device mapper or logical volume drivers, …) to be compiled as modules and loaded from the initrd/initramfs.

The startup scripts on the initrd (/linuxrc) or initramfs (/init) typically loads some modules and locates the root filesystem. Each distribution has its own setup. Ubuntu uses an initramfs which is assembled from components in the initramfs-tools package and regenerated for each kernel based on the necessary drivers to mount the root filesystem.

After the root filesystem is mounted, during the system boot, modules listed in /etc/modules (Debian, …) or /etc/modules.conf (Red Hat, Arch, …) are loaded. This file usually lists few modules if any. Most modules are loaded on demand.

When the kernel detects some hardware for which it lacks a driver, or certain other components such as network protocols or cryptographic algorithms, it calls /sbin/modprobe to load the module. For hardware drivers, the kernel passes names that encode the PCI id, the USB id, or other systematic designation of the hardware. There is a table in /lib/modules/$VERSION/modules.alias that maps these systematic designations to module names. This table is generated by depmod and read by modprobe.

If you have an extra kernel module that you compiled manually for a hardware device, drop it into /lib/modules/$VERSION/local (create the local subdirectory if it doesn't exist) and run depmod -a to regenerate the alias database. If the module is for some unusual feature that the kernel is unable to detect automatically, drop it into /lib/modules/$VERSION/local, run depmod -a to analyze its dependencies, and add the module name to /etc/modules.

  • I can't find a /var/lib/modules nor a /lib/modules/*/local, nor can I find any mention of this on Google or in the depmod man page. Looked in CentOS 6.4, 7.4 and Ubuntu 19.04.
    – itsadok
    Sep 4, 2019 at 8:00
  • @itsadok I'm pretty sure I meant to write /lib/modules/…. Just create the local subdirectory (depmod traverses /lib/module/$VERSION recursively). Sep 4, 2019 at 9:44
  • Do you know how to force kernel to pick my custom module instead of default one ?
    – EdiD
    Aug 28, 2021 at 13:38
  • @EdiD The kernel loads the code it's told to load. The tool that maps a module name to a file containing code is modprobe. I guess it chooses the file to load based on modules.order but I don't know what generates this. You should ask this as a new question on this site. Aug 28, 2021 at 19:46
  • @Gilles'SO-stopbeingevil' Ok thanks question asked.
    – EdiD
    Aug 29, 2021 at 15:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .