How to: Compile Linux kernel modules

by on August 22, 2005 · 69 comments· LAST UPDATED October 26, 2007

in , ,

This is one the essential and important task. Many time we upgrade our kernel and some precompiled drivers won't work with Linux. Especially if you have weird hardware; then vendor may send you driver code aka C files to compile. Or even you can write your own Linux kernel driver. Compiling kernel driver is easy. Kernel 2.6.xx makes it even much more easier. Following steps are required to compile driver as module:

1) You need running kernel source code; if you don't have a source code download it from Untar kernel source code (tar ball) in /usr/src using tar command:
$ tar -zxvf kernel* -C /usr/src

To be frank kernel headers are more than sufficient to compile kernel modules / drivers. See how to install kernel headers under Debian / Ubuntu Linux or RHEL / CentOS / Fedora Linux.

2) Next go to your kernel module source code directory and simply create the Makefile file as follows (assuming your kernel module name is foo):
$ vi Makefile

3) Add following text to it:

obj-m = foo.o
KVERSION = $(shell uname -r)
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

4) Compile module using make command (module build can be done by any user) :
$ make
It will finally creates the foo.ko module in current directory. You can see all actual compile command stored in .foo* files in same directory.

5) Once module compiled successfully, load it using insmod or modprobe command. You need to be root user or privileged user to run insmod:
# insmod foo.ko

Example: hello.c module

1) hello.c C source code. Copy following code and save to hello.c
$ mkdir demo; cd demo
$ vi hello.c

2)Add following c source code to it:

#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */
#include <linux/init.h>         /* Needed for the macros */
static int __init hello_start(void)
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
static void __exit hello_end(void)
printk(KERN_INFO "Goodbye Mr.\n");

This is an example modified from original source for demonstration purpose.

3) Save the file. Create new Makefile as follows:
$ vi Makefile
Append following make commands:

obj-m = hello.o
KVERSION = $(shell uname -r)
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

4) Save and close the file.

5) Compile hello.c module:
$ make

6) Become a root user (use su or sudo) and load the module:
$ su -
# insmod hello.ko

Note you can see message on screen if you are logged in as root under run level 3.

7) Verify that module loaded:
# lsmod | less

8) See message in /var/log/message file:
# tail -f /var/log/message

9) Unload the module:
# rmmod hello

10) Load module when Linux system comes up. File /etc/modules use to load kernel boot time. This file should contain the names of kernel modules that are to be loaded at boot time, one per line. First copy your module to /lib/modules/$(uname -r)/kernel/drivers. Following are suggested steps:

(a) Create directory for hello module:
# mkdir -p /lib/modules/$(uname -r)/kernel/drivers/hello
(b) Copy module:
# cp hello.ko /lib/modules/$(uname -r)/kernel/drivers/hello/
(c) Edit /etc/modules file under Debian Linux:
# vi /etc/modules
(d) Add following line to it:
(e) Reboot to see changes. Use lsmod or dmesg command to verify module loaded or not.
# cat /proc/modules
# lsmod | less

See also:

  • Read man pages of lsmod, rmmod, modprobe, modules
  • Documentation located in your kernel source directory (for example /usr/src/linux-2.6.xx.xx/Documentation/) and README file located under kernel source code tree /usr/src/linux-2.6.xx.xx/README
  • Read tutorial online.
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 69 comments… read them below or add one }

1 Anonymous August 23, 2005 at 10:33 am

Aha … intresting gone try this.


2 swift May 4, 2007 at 3:28 am

hi when i compile this by running make file this type of erro occcur …what to do

make -f make
make -C /lib/modules/2.6.18-1.2798.fc6/build M=/home/mclamna modules
make[1]: Entering directory `/usr/src/kernels/2.6.18-1.2798.fc6-i686′
scripts/ /home/mclamna/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/mclamna/Makefile’. Stop.
make[1]: *** [_module_/home/mclamna] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-1.2798.fc6-i686′
make: *** [all] Error 2


3 shan January 18, 2008 at 6:23 am

dis s wat i encountered wen using

CHK include/linux/version.h
CHK include/linux/utsrelease.h
HOSTCC scripts/basic/fixdep
In file included from /usr/include/bits/posix1_lim.h:153,
from /usr/include/limits.h:145,
from /usr/lib/gcc/i486-linux-gnu/4.1.2/include/limits.h:122,
from /usr/lib/gcc/i486-linux-gnu/4.1.2/include/syslimits.h:7,
from /usr/lib/gcc/i486-linux-gnu/4.1.2/include/limits.h:11,
from scripts/basic/fixdep.c:113:
/usr/include/bits/local_lim.h:36:26: error: linux/limits.h: No such file or directory
In file included from /usr/include/sys/socket.h:35,
from /usr/include/netinet/in.h:24,
from /usr/include/arpa/inet.h:23,
from scripts/basic/fixdep.c:115:
/usr/include/bits/socket.h:310:24: error: asm/socket.h: No such file or directory
scripts/basic/fixdep.c: In function ‘use_config’:
scripts/basic/fixdep.c:204: error: ‘PATH_MAX’ undeclared (first use in this function)
scripts/basic/fixdep.c:204: error: (Each undeclared identifier is reported only once
scripts/basic/fixdep.c:204: error: for each function it appears in.)
scripts/basic/fixdep.c:204: warning: unused variable ‘s’
scripts/basic/fixdep.c: In function ‘parse_dep_file’:
scripts/basic/fixdep.c:300: error: ‘PATH_MAX’ undeclared (first use in this function)
scripts/basic/fixdep.c:300: warning: unused variable ‘s’
make[1]: *** [scripts/basic/fixdep] Error 1
make: *** [scripts_basic] Error 2


4 Nitin May 24, 2008 at 4:56 pm

Hi swift, I guess u have not created Makefile in ur present directory, plz make sure that makefile name is correct and its case sensitive, it should be ‘Makefile’.


5 Nitin June 3, 2008 at 2:22 pm

Hi, This was very much helpful but now I’m stuck because i didn’t understand how this particular makefile is working, so i’m not able to write it for multiple files. Please could anyone explain how this makefile works with a example for multiple files.


6 john June 6, 2008 at 7:27 pm

how to cross compile lkm for arm?


7 Mohammad August 11, 2008 at 4:21 pm

Dear Vivek ,
Thanks alots for yr teaching.
i compile a new module on fedora core 6 and make it very good. but in fedora i dont know how to submit for when linux start up , can load automaticly. i didnot find /etc/modules in fedora.


8 touristguy87 November 4, 2008 at 6:19 am

I followed the instructions as written even copying and pasting the code, I got nada.

I was able to install the headers using
apt-get install linux-headers-$(uname -r)

then I copied and pasted the hello.c text and the Makefile from this page but I got nothing.
make: Nothing to be done for ‘all’.

I must be missing something

this is in Ubuntu 8.04


9 touristguy87 November 8, 2008 at 6:56 am

ok I got it somehow, honestly I am not sure how but I patched my install (now running kernel-headers 2.6.24-21 generic) and rebooted, sudo -i, just tried running make and it worked.

before I was getting “can’t find target ‘make’ error that seems so common. It now is working!!!


10 mangesh November 20, 2008 at 10:49 am

how do I cross compile a module using kbuild. Please help


11 mix January 7, 2009 at 2:53 am

The reason that make: Nothing to be done for ‘all’ is you should modify the Makefile, replace the space in each make command with tab.


12 s singh February 7, 2011 at 2:27 pm

i am getting the same error and replacing spaces with tab is not working.
some other solution..??


13 kernelmodule January 22, 2009 at 11:48 am

After executing the insmod the message is not being displayed…Can u pls guide me on whats goin wrong?


14 Anjan January 13, 2011 at 8:09 am

try looking for messages in dmesg i.e., after #insmod hello.ko
try #dmesg
u will be able to see the messages


15 tahi August 28, 2014 at 5:17 am

hello: disagrees about version of symbol module_layout
i can’t install module to my system
uname –all
Linux debian-vir1 3.2.0-4-486 #1 Debian 3.2.60-1+deb7u3 i686 GNU/Linux
OS is installed on virtualbox.
what’s wrong in here?


16 scrat January 23, 2009 at 8:25 pm

Oh, big thanks, source of Makefile is very helpful.


17 Victor May 18, 2009 at 4:36 pm


I’m trying to compile your hello.c module in a Fedora Core 9 with a kernel. My gcc compiler is 4.3.0 20080428 (Red Hat 4.3.0-8) (GCC). My kernel-devel package is installed with my current kernel version.

When I try to compile I get the following:

root@leela:[~/pruebas-modulos]$ make
make -C /lib/modules/ M=/root/pruebas-modulos modules
make[1]: se ingresa al directorio `/usr/src/kernels/′
Building modules, stage 2.
MODPOST 0 modules
make[1]: se sale del directorio `/usr/src/kernels/′

root@leela:[~/pruebas-modulos]$ ls
hello.c hello.o Makefile Module.markers modules.order Module.symvers

As you can see I do not obtain the .ko file. I have searched across the web in order to understand why I am not getting my .ko file, but I am not able to find anything useful. I would appreciate if you could help me to find why the makefile is not working

Thanks in advance,


18 June 5, 2009 at 4:25 am

Hey, check that you makefile if you make a mistake with “obj-m” with “obj_m”, I have the same problem as you do.
When I write “obj-m” instead of “obj_m”, everything goes good.


19 reveil July 27, 2009 at 10:53 am

Be sure you have tabs instead of spaces in the Makefile. Obvious and common mistake but took me a few minutes to figure this out after copy-pasting.


20 shreyas karmahe August 24, 2009 at 5:42 am

could u please tell me how can i install the kernel updates as I despirately need linux source code.


21 blaine September 23, 2009 at 5:30 pm

Thank you for posting this. It is very hard to figure out how to just make a single module manually. This post should be ranked higher when searching “How to compile a single kernel module”. Self fulfilling prophecy, perhaps?


22 Adam October 14, 2009 at 3:24 pm

Thanks, this ended a few hours of frustration.


23 Alok November 4, 2009 at 1:41 pm

I have tried load module when Linux system comes up but it’s not displaying in
#cat /proc/modules
I have done these things successfully.
# mkdir -p /lib/modules/$(uname -r)/kernel/drivers/hello
# cp hello.ko /lib/modules/$(uname -r)/kernel/drivers/hello/
# vi /etc/modules

and Added “hello” word in next line.
any suggestion would be welcome

with regards


24 Devang April 23, 2013 at 5:13 am

Hi Alok,

I am also facing the same problem. Have your query been solved ?
If solved, tell me the procedure, it will be a great help to me.

Thanks & Regards,


25 abhi January 10, 2010 at 1:03 pm

Thanks a lot. Found this article v. useful


26 Arnab Chakraborty January 17, 2010 at 12:07 pm


I found the article very helpful. Unlike many other linux tutorials, this one worked (with me) within the first few attempts! Just one minor point:

I had to change
tail -f /var/log/message
tail -f /var/log/messages



27 mario March 23, 2010 at 10:09 am

Be sure , tabs instead of spaces in the Makefile!!!!!!!!


28 Alvaro March 25, 2010 at 10:19 pm

Please can anybody help me out, I need to add the pktgen module, but I’m finding it difficult
Thanks in advance


29 Martin April 3, 2010 at 1:07 pm

thanks for that article. I try to compile a helloworld.c against the sources of my running kernel now quite a while without success. First the distro-kernel, and then also the kernel. It all looks good to me. make enters the right directories. the error is _always_ “No Rule for target ….. ” like in the first comment above.

I don’t really understand all about the makefile. make is called from inside the kernel tree through the makefile right? Still, it really should be fine, but it doesnt work. Does anybody have a hint?


30 Zed April 4, 2010 at 9:50 pm

Hi guys… I’m getting really desperate, I need to program a network device drivers for one of my projects, and I can’t even get the hello world done properly !

I followed the protocol above, and when it comes to the make command, here is what I get:

root@ubuntu:/usr/src/linux-source-2.6.31/include# make

make -C /lib/modules/2.6.31-20-generic/build M=/usr/src/linux-source-2.6.31/include modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.31-20-generic’
Building modules, stage 2.
MODPOST 0 modules
make[1]: Leaving directory `/usr/src/linux-headers-2.6.31-20-generic’

So no hello.ko file generated ! I already spent so many hours on this, checked that I have tabs instead of spaces in the makefile, and nothing changed…

Thanks for your help guys !



31 Vlad June 21, 2010 at 6:44 pm

I found an error in Makefile you have:

$(PWD) should be $(shell pwd)

Since PWD is not defined, I think it attempts to compile the entire kernel source.
It solved this error I got:
“make[2]: *** No rule to make target `kernel/bounds.c’, needed by `kernel/bounds.s’. Stop.”


32 smash July 21, 2010 at 12:00 am

Thanks a lot to mix and reveil for pointing out the tabs in place of spaces. I spent like 3 hrs trying to build a module from another site, and had no clue what was wrong. It all boiled down to tabs.
Also thanks to whoever made this tut. :)


33 touristguy87 July 21, 2010 at 10:27 pm

One thing I learned from this exercise is to always start from a working example.


34 Tapas Mishrae August 21, 2010 at 1:42 am

Checked it.
Following is my program
extern void *sys_table[];
asmlinkage int(*main_sys_exit)(int);
asmlinkage int alt_exit_function(int err_code)
printk(“Sys_exit called with err_code=%d\n”,err_code);
return main_sys_exit(err_code);

int init_module()
void cleanup_module()

and following is make file
obj-m = sample2.o
make -C /lib/modules/$(uname -r)/build/M=$(PWD) modules
make -C /lib/modules/$(uname -r)/build/M=$(PWD) clean

but I got error

make -C /lib/modules//build/M=/home/tapas/LKP/pandora/temp/sample2 modules
make: *** /lib/modules//build/M=/home/tapas/LKP/pandora/temp/sample2: No such file or directory. Stop.
make: *** [all] Error 2

where as I had installed Kernel headers.


35 touristguy87 August 21, 2010 at 6:26 am

If you cannot get that to work that is one thing.
If you can get THAT to work but you cannot get YOUR code to work then
for chrissakes
figure out what you have done different that doesn’t work and fix it l!!!!


36 Lucas November 18, 2010 at 6:05 pm

my system is ubuntu 10.04 kernel 2.6.32-25-generic, i did everything as described in article and.. when i do(as root: sudo -i ):
# insmod hello.ko
i get answer :
# insmod: error inserting ‘hello.ko': -1 Invalid module format

And I’ve no idea what can be wrong.. or what I do wrong… would be gratefull for help


37 PAgore November 19, 2010 at 9:51 am

Cool! :-)
I compiled my first module in Linux.

Thanks for your instructions!!!


38 Prashant November 21, 2010 at 5:00 pm

I made a hello-1.c and Makefile in the same directory.
when is issue “make” command, it gives error:
make −C /lib/modules/2.6.32-25-generic/build M=/home/prashant modules
make[1]: Entering directory `/home/prashant’
make[1]: *** No rule to make target `−C’. Stop.
make[1]: Leaving directory `/home/prashant’
make: *** [all] Error 2

I am getting this error from so long. I even changed my working directory to root.


39 Suneel October 29, 2011 at 7:44 am

How did you fix this problem Prashanth? I’m getting exactly same error make[1]: *** No rule to make target `−C’. Stop. Please help me if you have some info on this.


40 SIFE December 1, 2010 at 8:47 am

I have trouble with my custom kernel for the last tow days, it does not boot due [b]ahci[/b] module, can I compile it as module and then make initrd image or I have to add it to kernel configuration, if so where I have to activate it, I tried to go to:
Device drivers-> Serial ATA Drivers and Parelle ATA Drivers-> AHCI SATA Support
But it seems does not compile it according to this message when I install modules:
WARNING: No module ahci found for kernel 2.6.37-rc4, continuing anyway


41 SIFE December 1, 2010 at 8:48 am

I have trouble with my custom kernel for the last tow days, it does not boot due ahci module, can I compile it as module and then make initrd image or I have to add it to kernel configuration, if so where I have to activate it, I tried to go to:
Device drivers-> Serial ATA Drivers and Parelle ATA Drivers-> AHCI SATA Support
But it seems does not compile it according to this message when I install modules:

WARNING: No module ahci found for kernel 2.6.37-rc4, continuing anyway


42 Prabhu December 8, 2010 at 10:44 am

Cool article. My first module programming is running successfully. Thanks a lot.
– Prabhu


43 Romy January 28, 2011 at 6:12 pm

Really Thankful…..A great note for Beginners


44 Rigved Rakshit February 4, 2011 at 2:36 pm

The line:

obj-m = foo.o

is wrong. It should be:

obj-m += foo.o


45 s singh February 7, 2011 at 3:26 pm

i am getting the following error after executing make
make: Nothing to be done for `all’.


46 alexpally April 10, 2011 at 4:48 pm

You must use TABs instead of spaces in make file


47 touristguy87 February 14, 2011 at 2:08 am

um, obviously one part of the problem here is the fact that depending on what editor you use, what exactly the offsets are between different sections of the relevant files and whether you use spaces or tabs, you will get different results.

that’s on top of exactly what version of Linux you’re running, and what version of the relevant libraries.

Trust me on this. Start with a working example, one that you simply download and run, with no edits. If you don’t have one find one on the internet somewhere. The most that you can pick up from a guide like this are just guidelines for what should work. Doesn’t mean that it will work for you on your system, the way that you’ve configured it or edited the file.
When it should work and it does not? odds are that you are about to learn something new about Linux.
good luck


48 touristguy87 February 14, 2011 at 2:09 am

also remember that there is a difference between ‘ and `
just as between space and tab
so if you see one don’t use the other


49 touristguy87 March 1, 2011 at 3:18 pm

If you’re really having trouble getting this to fly, then I would suggest reading this:
The full documentation on gnu make
probably will help a lot.
and as always
start with something that works and make minimal changes between verifications
something as small as a simple tab where the make utility expects spaces or vice-versa
will definitely keep it from working. Not to mention an out-of-date install or an install that needs a reboot.


50 sudhanshu March 7, 2011 at 6:00 pm

@s singh
refer to link
copy and paste the following from that page itself

obj-m += hello-1.o
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

but you will have to modify as:-

obj-m += hello.o
	make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
	make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean

as pointed out by one of friend here.
be sure to copy and paste from this link only no need of replacing space with tabs and all that


51 Iranist September 22, 2011 at 7:29 pm

It was so useful, thanks!


52 Debian user November 11, 2011 at 1:13 am

Debian Squeeze 2.6.32-5-amd64

It worked right away!

Thank you. ^^

— screen dump —
Module Size Used by
hello 786 0
powernow_k8 10978 1
cpufreq_stats 2740 0
cpufreq_conservative 5162 0
cpufreq_userspace 1992 0
parport_pc 18855 0
cpufreq_powersave 902 0
ppdev 5030 0


53 Kesava November 14, 2011 at 6:15 am

when I try to do the same with the different code; getting the following error. Any Help is really appreciated.

> make
make -C /lib/modules/ SUBDIRS=/home/vmware1/netp15 modules
make[1]: Entering directory `/usr/src/linux-’
make[1]: *** No rule to make target `modules’. Stop.
make[1]: Leaving directory `/usr/src/linux-’
make: *** [all] Error 2



54 Moe March 21, 2012 at 9:17 pm

In the makefile change all spaces to tabs….
also change the lines with



M=$(shell pwd)


55 Martin April 19, 2012 at 8:42 pm

Just a great entry point for beginners!


56 mansha April 20, 2012 at 10:01 pm

i try to follow these steps but i fieled plz someone reply me
int init_module(void)
printk(KERN_INFO “Hello world 1.\n”);
* * A non 0 return means init_module failed; module can’t be loaded.
* */
return 0;
void cleanup_module(void)
printk(KERN_INFO “Goodbye world 1.\n”);

2===> make file
obj-m = hello.o
KVERSION = $(shell uname -r)
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

finally i execute “make” and i get this message

make: Nothing to be done for `all’.
plz help mz so urgent


57 arizonadude April 27, 2012 at 5:34 pm

I had the same problem, and it turns out that if you copy and paste the code above there is some random encoding, try to type all the Makefile in an editor and then save it :)


58 arizonadude April 27, 2012 at 6:46 pm

I also needed to run depmod command to load the module after a reboot


59 sandeep June 1, 2012 at 6:36 am

ya its working fine
thanks very much


60 teju June 16, 2012 at 9:58 am

iam trying to write a simple hello module i hae written a Makefile as
obj-m += hello.o
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) module
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) module
i given make
i am getting the error of
missing a saparetor like tat
wheather i have to re install any packages i am using fedora16 3.1.0-7 linux kernel


61 arizonadude June 22, 2012 at 8:40 pm

I think your problem relies for the line obj-m += hello.o . According to the code above, it doesn’t have the ‘+’ sign. Also I think it is modules and not module.


62 kks September 28, 2012 at 12:06 pm

I have kernel module. I wanted to compile it as user mode application. what steps do i need to follow.
Say i have wireless driver from compat wireless. Now i have debug it to know trace the API calls across three sub layers(wpa-supplicant, cfg80211,nl80211,mac80211) using hwsim. In this wpa_supplicant is user space module/application and other are kernel mode.
I have to now compile them together as user space modules/application. and probable use simple print statement or use gdb to know the flow.
Please help.


63 paulus November 9, 2012 at 3:50 pm

GNU make insists that every command line beginns with a tab (can’t give u a reason for that)
so just add this tabulator to your two make commands and it should work.

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


64 Sudarshan April 11, 2013 at 8:16 am

Thanks you. I followed your instructions. Removed spaces from Makefile and replaced with tabs, it worked for me.


65 AruniRC April 15, 2013 at 4:28 am

I just wrote the makefile as given, but cannot compile using make.

the console keeps showing this message

make: Nothing to be done for `all’.

could someone please help?


66 AruniRC April 15, 2013 at 4:32 am

sorry. this was mentioned in previous comments


67 Yogesh September 3, 2013 at 12:37 pm

i have error “kernel/module.h” &”/kernel/init.h” is not avaliable . on my ubntu linux os.
so what need to be run program.


68 Yogesh September 3, 2013 at 12:43 pm

is there required compiled hello.c file before Makefile ?


69 Rosario Tyrell November 17, 2014 at 8:48 pm

@ Alex, quel commento è probabile che non fatta da un lettore normale, ma piuttosto da un lettore in visita che ha inciampato sul posto per caso.


Leave a Comment

Tagged as: , , , , , , ,

Previous post:

Next post: