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"
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 3.85
Creating Device Nodes with mknod and udev
Svep för att visa menyn
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"
Tack för dina kommentarer!