Creating Device Nodes with mknod and udev
When you write a character device driver, user applications need a way to interact with it. This is done through device nodesβspecial files in the /dev directory. Each node represents a device and provides an interface for user space programs to perform operations like open, read, write, and close. Sometimes, you may need to manually create these device nodes, especially when testing a new driver or working on systems without automatic device management. The mknod command allows you to create a device node by specifying its type (character or block), its name, and its major and minor numbers.
mknod_example.sh
123# Create a character device node named /dev/mychardev with major number 240 and minor number 0 sudo mknod /dev/mychardev c 240 0 sudo chmod 666 /dev/mychardev
While mknod works for manual creation, most modern Linux systems use udev to manage device nodes automatically. udev is a device manager that runs in user space and listens for kernel events, such as a new device being registered. When your character device driver is loaded, the kernel sends an event with the deviceβs major and minor numbers. udev responds by creating or removing device nodes in /dev as needed, following rules you define. This means you do not have to manually run mknod every time you insert or remove a device, reducing errors and making device management seamless. For instance, if you want udev to automatically create a node like the one shown in the previous example, you could write a rule that matches the major and minor numbers.
99-mychardev.rules
12# udev rule to create /dev/mychardev when a device with major 240 and minor 0 appears KERNEL=="*", MAJOR=="240", MINOR=="0", NAME="mychardev", MODE="0666"
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how to write a udev rule for my device?
What are major and minor numbers, and how do I find them for my driver?
How do I manually create a device node using mknod?
Awesome!
Completion rate improved to 3.85
Creating Device Nodes with mknod and udev
Swipe to show menu
When you write a character device driver, user applications need a way to interact with it. This is done through device nodesβspecial files in the /dev directory. Each node represents a device and provides an interface for user space programs to perform operations like open, read, write, and close. Sometimes, you may need to manually create these device nodes, especially when testing a new driver or working on systems without automatic device management. The mknod command allows you to create a device node by specifying its type (character or block), its name, and its major and minor numbers.
mknod_example.sh
123# Create a character device node named /dev/mychardev with major number 240 and minor number 0 sudo mknod /dev/mychardev c 240 0 sudo chmod 666 /dev/mychardev
While mknod works for manual creation, most modern Linux systems use udev to manage device nodes automatically. udev is a device manager that runs in user space and listens for kernel events, such as a new device being registered. When your character device driver is loaded, the kernel sends an event with the deviceβs major and minor numbers. udev responds by creating or removing device nodes in /dev as needed, following rules you define. This means you do not have to manually run mknod every time you insert or remove a device, reducing errors and making device management seamless. For instance, if you want udev to automatically create a node like the one shown in the previous example, you could write a rule that matches the major and minor numbers.
99-mychardev.rules
12# udev rule to create /dev/mychardev when a device with major 240 and minor 0 appears KERNEL=="*", MAJOR=="240", MINOR=="0", NAME="mychardev", MODE="0666"
Thanks for your feedback!