1、安装内核头文件 2 apt-get install linux-headers-`uname -r` 3 4 2、编写hello.c文件 5 6 #include <linux/init.h> 7 #include <linux/module.h> 8 MODULE_LICENSE("Dual BSD/GPL"); 9 10 static int hello_init(void) 11 { 12 printk(KERN_ERR"Hello,world.\n"); 13 return 0x0; 14 } 15 static void hello_exit(void) 16 { 17 printk(KERN_ERR"Hello,exit.\n"); 18 return; 19 } 20 module_init(hello_init); 21 module_exit(hello_exit); 22 23 3、编写Makefile 24 25 obj-m += hello.o 26 CURRENT_PATH := $(shell pwd) 27 LINUX_KERNEL := $(shell uname -r) 28 LINUX_KERNEL_PATH := /usr/src/linux-headers-$(LINUX_KERNEL) 29 all: 30 make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) moduels 31 clean: 32 make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean 33 34 4、编译ko文件 35 make 36 37 5、加载内核文件 38 insmod hello.ko 39 40 6、查看内核文件是否加载成功 41 dmesg 42 43 7、查看内核模块信息 44 modinfo hello.ko 45 46 8、卸载内核模块 47 rmmod hello.ko 48 49