Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Creating Device Nodes with mknod and udev | Writing Your First Character Driver
Practice
Projects
Quizzes & Challenges
Cuestionarios
Challenges
/
C Device Drivers Basics

bookCreating 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

mknod_example.sh

copy
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

99-mychardev.rules

copy
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"
question mark

What is the main advantage of using udev over manual mknod for device node creation?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 6

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

bookCreating Device Nodes with mknod and udev

Desliza para mostrar el menú

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

mknod_example.sh

copy
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

99-mychardev.rules

copy
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"
question mark

What is the main advantage of using udev over manual mknod for device node creation?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 6
some-alt