I am writing a simple Makefile for an external kernel module.

Building it with:

obj-m += usbtherm.o

    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

compiles only the external module, which is nice.

But installing it with:

    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules_install

installs all modules in the kernel source tree, and I don't know how I can have the module installed to drivers/usb/misc.

So I am installing the module like this:

    cp $(shell pwd)/usbtherm.ko /lib/modules/$(shell uname -r)/kernel/drivers/usb/misc
    depmod -a

which will install only the external module.

But it doesn't look too elegant to me - what am I missing?

  • Are you using su or sudo when you try the install? I ran into this when I was installling my own modules and using the environment preservation flags with sudo fixed the behavior to be what I expected (IE. Installing my module and running depmod).
    – dudebrobro
    Feb 20, 2017 at 2:49
  • Indeed I run make as user but make install as root with sudo. That worked fine so far since every time I get a kernel update I need to reinstall the module. Feb 20, 2017 at 20:30

2 Answers 2


When I installed with sudo

sudo -E make install

Installed just my external module and reran depmod.

Running with out the environment preservation flags resulted in reinstalling all of the modules built in the kernel source tree I used to update my kernel. Seems like using sudo may not have PWD set correctly by the time make changes into the kernel tree possibly, but maybe that's will help someone else that runs into this.


I had another, closer look at modules_install. In the Linux Makefile:

# Target to install modules
PHONY += modules_install
modules_install: _modinst_ _modinst_post

PHONY += _modinst_
        @rm -rf $(MODLIB)/kernel
        @rm -f $(MODLIB)/source
        @mkdir -p $(MODLIB)/kernel
        @ln -s `cd $(srctree) && /bin/pwd` $(MODLIB)/source
        @if [ ! $(objtree) -ef  $(MODLIB)/build ]; then \
                rm -f $(MODLIB)/build ; \
                ln -s $(CURDIR) $(MODLIB)/build ; \
        @cp -f $(objtree)/modules.order $(MODLIB)/
        @cp -f $(objtree)/modules.builtin $(MODLIB)/
        $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modinst

What I understand from it is that it expects a source tree, uninstalls existing modules, does some cleanup and then runs scripts/Makefile.modinst to install the modules - which installs external modules to extra:

# Modules built outside the kernel source tree go into extra by default

And actually, when building an external module on a system with no kernel sources but the headers installed, the module is built in /usr/src/linux-headers-$(uname -r) which is symlinked from /lib/modules/$(uname -r)/build, and while the all and clean targets succeed, modules_install fails because i.e. modules.order doesn't exist if only the headers are installed.

But installing the external module by copying the *.ko and running depmod -a works fine.

You must log in to answer this question.

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