{"id":7720,"date":"2024-09-27T01:07:00","date_gmt":"2024-09-27T05:07:00","guid":{"rendered":"https:\/\/www.both.org\/?p=7720"},"modified":"2024-09-26T07:53:48","modified_gmt":"2024-09-26T11:53:48","slug":"how-i-create-linux-device-files-and-why","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=7720","title":{"rendered":"How I create Linux device files &#8212; and why"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"7720\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>First, what the heck is a Linux device file &#8212; and why should I care?<\/p>\n\n\n\n<p>Linux handles almost everything as a file. This has some interesting, powerful, and amazing implications. This concept makes it possible to copy an entire hard drive, boot record included, because the entire hard drive is a file, just as are the individual partitions. \u201c<a href=\"https:\/\/www.both.org\/?p=6843\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=3191\" target=\"_blank\" rel=\"noreferrer noopener\">Everything is a file<\/a>\u201d is possible because all devices are implemented by Linux as these things called device files. Device files are not device drivers, rather they are gateways to devices that are exposed to the user.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"> Device Files<\/h2>\n\n\n\n<p>Device files are technically known as <a href=\"https:\/\/en.wikipedia.org\/wiki\/Device_file\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Device_file\" target=\"_blank\" rel=\"noreferrer noopener\">device special files<\/a>. Device files are employed to provide the operating system and the users with an interface to the devices that they represent. All Linux device files are located in the \/dev directory, which is an integral part of the root (\/) filesystem because they must be available to the operating system during early stages of the boot process \u2013 before other filesystems are mounted.<\/p>\n\n\n\n<p>CentOS and RHEL, as well as all versions of Fedora going back to at least as far Fedora 15, use the newer method of creating the device files. All required device files are created at boot time. This functionality is possible because the <strong>udev<\/strong> device manager detects addition and removal of devices as they occur. This allows for true dynamic plug-n-play functionality while the host is up and running. It also performs the same task at boot time by detecting all devices installed on the system very early in the boot process. <a href=\"https:\/\/www.linux.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Linux.com<\/a> has a good <a href=\"https:\/\/www.linux.com\/news\/udev-introduction-device-management-modern-linux-system\" target=\"_blank\" rel=\"noreferrer noopener\">description of udev<\/a> and Seth Kenlon has posted an article here on Both.org that shows you <a href=\"https:\/\/www.both.org\/?p=4875\" target=\"_blank\" rel=\"noreferrer noopener\">How to Use udev<\/a>.<\/p>\n\n\n\n<p>List the files in \/dev, notice the date and time on the files. All of them were created during the last boot. You can verify this using the <strong>uptime<\/strong> or <strong>last<\/strong> commands. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a device file<\/h2>\n\n\n\n<p>In the past, the device files in \/dev were all created at installation time, resulting in a directory full of almost\u00a0every possible device file, even though most would never be used. In the unlikely event that a new device file was needed or one was accidentally deleted and needed to be re-created, the <strong>mknod<\/strong> program was available to manually create device files. All you had to know was the device major and minor numbers. I needed to do just that for the first and only time ever, a few years ago. The need doesn&#8217;t arise very often.<\/p>\n\n\n\n<p>Somehow I managed to delete the default printer device, lp0, so couldn&#8217;t print to my laser printer. I needed to recreate that printer without a reboot as I was in the middle of a project. Normally, Linux creates four parallel printer devices at boot time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># <strong>ll \/dev\/lp*<\/strong>\ncrw-rw----. 1 root lp 6, 0 Sep 25 10:59 lp0\ncrw-rw----. 1 root lp 6, 1 Sep 25 10:59 lp1\ncrw-rw----. 1 root lp 6, 2 Sep 25 10:59 lp2\ncrw-rw----. 1 root lp 6, 3 Sep 25 10:59 lp3<\/code><\/pre>\n\n\n\n<p>But this is what I saw. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># <strong>ll \/dev\/lp*<\/strong>\ncrw-rw----. 1 root lp 6, 1 Sep 25 10:59 lp1\ncrw-rw----. 1 root lp 6, 2 Sep 25 10:59 lp2\ncrw-rw----. 1 root lp 6, 3 Sep 25 10:59 lp3<\/code><\/pre>\n\n\n\n<p>The first thing I did was refer to the <a href=\"https:\/\/www.kernel.org\/doc\/html\/v4.15\/admin-guide\/devices.html\" target=\"_blank\" rel=\"noreferrer noopener\">Linux Allocated Devices<\/a>, at the Kernel.org web site. A quick search revealed the information I needed to create the device.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   6 char       Parallel printer devices<br>                  0 = \/dev\/lp0          Parallel printer on parport0<br>                  1 = \/dev\/lp1          Parallel printer on parport1<br>                    ...<br><br>                Current Linux kernels no longer have a fixed mapping<br>                between parallel ports and I\/O addresses.  Instead,<br>                they are redirected through the parport multiplex layer.<\/pre>\n\n\n\n<p>The major code for parallel printer devices is 6, and the the minor code is 0 for lpr0. It was already clear to me that these were the codes I needed, but I always check the list of devices at kernel.org to be sure. <\/p>\n\n\n\n<p>The following command created the missing device file. I specify the fully qualified name of the device file, and c defines it as a character file. The 6 and 0 are the major and minor codes. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># # <strong>mknod \/dev\/lp0 c 6 0<\/strong>\nroot@testvm1:~# ll \/dev\/lp*\ncrw-r--r--. 1 root root 6, 0 Sep 25 15:34 \/dev\/lp0\ncrw-rw----. 1 root lp   6, 1 Sep 25 10:59 \/dev\/lp1\ncrw-rw----. 1 root lp   6, 2 Sep 25 10:59 \/dev\/lp2\ncrw-rw----. 1 root lp   6, 3 Sep 25 10:59 \/dev\/lp3<\/code><\/pre>\n\n\n\n<p>I then changed the group ownership to lp and the permissions to 660. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># chgrp lp \/dev\/lp0 ; chmod 660 \/dev\/lp0 \n# ll \/dev\/lp*\ncrw-rw----. 1 root lp 6, 0 Sep 25 16:03 lp0\ncrw-rw----. 1 root lp 6, 1 Sep 25 10:59 lp1\ncrw-rw----. 1 root lp 6, 2 Sep 25 10:59 lp2\ncrw-rw----. 1 root lp 6, 3 Sep 25 10:59 lp3<\/code><\/pre>\n\n\n\n<p>You can practice creating device files in the \/tmp directory as that&#8217;s a safe place to do so. The files in \/dev can be deleted by root just as easily as other files, so it pays to be careful. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Even now, a situation caused by a missing device file can still happen and knowing how to deal with it can be important. <\/p>\n\n\n\n<p>I haven&#8217;t covered many of the myriad different types of device files that you might encounter. That information is available in plenty of detail in the resources cited. I hope I have given you some basic understanding of how to work with device files. I encourage you to explore more on your own. explore more on your own.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Additional Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.both.org\/?p=3191\" target=\"_blank\" rel=\"noreferrer noopener\">Everything is a file<\/a>, David Both, Both.org<\/li>\n\n\n\n<li><a href=\"https:\/\/www.both.org\/?p=3157\" target=\"_blank\" rel=\"noreferrer noopener\">An introduction to Linux filesystems<\/a>, David Both, Both.org<\/li>\n\n\n\n<li><a href=\"https:\/\/www.both.org\/?p=5446\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=5446\" target=\"_blank\" rel=\"noreferrer noopener\">Managing Devices in Linux<\/a>, David Both, Both.org<\/li>\n\n\n\n<li><a href=\"http:\/\/www.tldp.org\/LDP\/Linux-Filesystem-Hierarchy\/html\/dev.html\" target=\"_blank\" rel=\"noreferrer noopener\">Filesystem Hierarchy<\/a>, The Linux Documentation Project<\/li>\n\n\n\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Device_file\" target=\"_blank\" rel=\"noreferrer noopener\">Device File<\/a>, Wikipedia<\/li>\n\n\n\n<li><a href=\"https:\/\/www.kernel.org\/doc\/html\/v4.15\/admin-guide\/devices.html\" target=\"_blank\" rel=\"noreferrer noopener\">Linux Allocated Devices<\/a>, Kernel.org<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>First, what the heck is a Linux device file &mdash; and why should I care? Linux handles almost<\/p>\n","protected":false},"author":2,"featured_media":4295,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":"[]"},"categories":[5,83,89],"tags":[412,196],"class_list":["post-7720","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-problem-solving","category-system-administration","tag-device-special-files","tag-problem-solving"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/7720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7720"}],"version-history":[{"count":18,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/7720\/revisions"}],"predecessor-version":[{"id":7757,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/7720\/revisions\/7757"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/4295"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}