How to create the character device files?

I have tried cat > xxx^c

But I can't create a character device file


You can create device file using of mknod command provided by linux.

mknod <name> <type of device c:- character b:- block device etc> <major> <minor>


mknod test_device c 89 1

for you case you have to provide c , as you are creating character device file. After creating device file you also have to change permissions of file if you want to manipulate file in future.


With mknod:

mknod <name> c <major> <minor>

Read 'linux device drivers' (free book) to understand all details about what is character device.


Find the character device number in /proc/devices

For character devices that dynamically allocate device numbers, which is the norm to avoid conflicts, find the number with cat /proc/devices, which contains lines like:

195 nvidia-frontend


insmod /device_name.ko
major="$(grep "$dev" /proc/devices | cut -d ' ' -f 1)"
mknod "/dev/$dev" c "$major" 0

Here's an example that also contains a module for you to try out: Understanding character device (or character special) files


It may be worth looking at the lofi command to see if it fits your purpose

from the description in the man page

"The lofi file driver exports a file as a block device"

Example I just tried

mkfile -n 512k test_file

sudo lofiadm -a ~a6098/test_file /dev/lofi/1

ls -l /dev/lofi/1
lrwxrwxrwx   1 root     root          29 Sep 18 14:33 /dev/lofi/1 -> ../../devices/pseudo/lofi@0:1

ls -l /devices/pseudo/lofi@0:1  
brw-------   1 root     sys      147,  1 Sep 18 14:33 /devices/pseudo/lofi@0:1

When you have a cpio file with "character special file specified within", then you will need to use root to unpack (sudo cpio -idmv) otherwise the character special file will not be created.

So yes, another way to create character special file (by copying from existing character special file):

a. Create the file listing the files to be copied:

enter image description here

b. create the cpio file itself (must be running as root):

enter image description here

(note that "cpio -it" is just to list and verify)

c. from the cpio file, recreate the character file in another directory:

enter image description here

(here running as root is necessary, and we can see that "cpio" itself is using mknod command to create the special files).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.