I had problems with my distribution's kernel, so I built a kernel (same version) to build my modules. My modules would be compatible. However, if I install the modules, the script creates a new directory in /lib/modules/ instead of writing into the module directory of the kernel. This looks somehow like this:


Is there a way to write into a fixed path like /lib/modules/old_kernel/TARGET1. So far overiding the install pathes only led to very weird behavior. E.g.: /lib/modules/old_kernel/lib/modules/new_kernel/TARGET1

obj-m += rcio_core.o
obj-m += rcio_spi.o
obj-m += rcio_adc.o
obj-m += rcio_pwm.o
obj-m += rcio_rcin.o
obj-m += rcio_status.o

ccflags-y := -std=gnu99

KVERSION ?= $(shell uname -r)
KERNEL_SOURCE ?= /lib/modules/$(KVERSION)/build

  $(MAKE) -C $(KERNEL_SOURCE) M=$(PWD) modules
  /usr/local/bin/dtc -@ -I dts -O dtb rcio-overlay.dts -o rcio-overlay.dtb
  cp rcio-overlay.dtb /boot/overlays

$(MAKE) -C $(KERNEL_SOURCE) M=$(PWD) modules_install

  $(MAKE) -C $(KERNEL_SOURCE) M=$(PWD) clean
  $(RM) rcio-overlay.dtb
  • I re-tagged your question with the linux-kernel tag, though you didn't make it explicit; if my assumption is incorrect, please feel free to re-tag it.
    – Jeff Schaller
    Jun 9 '16 at 11:07

I assume that you are using "make modules_install" in order to install these modules. The destination under /lib/modules is determined according to against which kernel headers you are building the kernel modules source files. In Fedora, these headers are provided by kernel-devel. So, if "rpm -q kernel-devel" shows you the kernel version under /lib/modules which is the one that you want, than everything is fine. If not, you can remove this rpm and install one which has the version you need and than build and install the modules. In other distros the same principle holds, though the name of that package may vary.

Rami Rosen


The makefile you are going to use is pointed out by the following line:

KVERSION ?= $(shell uname -r)

Specifically in /lib/modules/$(KVERSION)/build/Makefile, all the targets you are executing with the $(MAKE) -C are going to refer to this.

Beware, this must be constant and you cannot change it manually in the targets. Although targets like clean might work pretty much the same across versions, if you build a kernel module with a specific kernel version (it's Makefile), you will not be able to install on any other version as the magic number would differ. Likewise it doesn't make sense to manually change the directory where modules_install is pointing to.

In short, if you want to target a different version, change the KVERSION variable in a way the directory pointed by KERNEL_SOURCE exists.

This is usually true for installed kernel, check @Rami Rosen 's answer for more details on this, however since you are mentioning you did compile your kernel rather than installing the kernel-devel package, you might want to check twice on this (how exactly the kernel version is spelled out, for instance).

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.