Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Creating Device Nodes with mknod and udev | Writing Your First Character Driver
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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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?

bookCreating 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

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 6
some-alt