I'm wondering if I can use the directory listing of /sys/module instead of lsmod to get a list of currently loaded modules.

Is that the list of loaded modules only? Or maybe that combined with /sys/module/*/initstate?


Each loaded module has an entry in /sys/module. But there are also kernel components with an entry in /sys/module that are not loaded as modules. Each kernel component¹ that can be built as a module has an entry in /sys/module, whether it is compiled and loaded as a module or compiled as part of the main kernel image.

lsmod gets the list of loaded modules from /proc/modules.

I think that only loaded modules have an initstate file in their /sys/module directory, so you can use that too.

¹ That's each component of the loaded kernel. The kernel doesn't know or care what modules you may have in files on your hard disk. The kernel doesn't care what modules were built at the same time of the kernel image, either; it may show that via /proc/config but it doesn't use that information for anything.

  • A fun side effect is that you can't insmod modules that are named exactly like built-in modules and you become totally clueless why: unix.stackexchange.com/questions/364956/… – Ciro Santilli 新疆改造中心996ICU六四事件 Apr 16 '18 at 14:21
  • @Gilles I was wondering if you could clarify "Each kernel component that can be built as a module has an entry in /sys/module". On my system: lsmod | wc -l => 116. ls /sys/module | wc -l = >201. grep '=m' /boot/config-$(uname -r) => 4934. – catanman Feb 25 at 17:30
  • @catanman That's the loaded modules. Usually you would only load a small number of modules compared to the number that were built, and you may load modules that were built separately. – Gilles Feb 25 at 17:58
  • 1
    No. You have 2389 options set to y, not all of which are components (some are yes/no settings but not a distinct component). You have 4934 components available as modules. You have 116 loaded modules, and 201-116=85 components that could be loaded as modules, but are built in. – Gilles Feb 25 at 18:41
  • 1
    @catanman 1) Look in the Kconfig files in the kernel source (either directly or via a kernel configuration tool). 2) Yes. 3) Yes (but you'd need to have a compatible .ko file, which is hard to ensure unless the file was built from the same kernel version with the same options). – Gilles Feb 25 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.