In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to manage equipment in Linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Device file
Device files are also known as device-specific files. Device files are used to provide operating systems and users with the device interfaces they represent. All Linux device files are located in the / dev directory and are part of the root (/) file system because they must be available during operating system startup.
One of the important things to remember about these device files is that most of them are not device drivers. More accurately, they are portals for device drivers. The data is passed from the application or operating system to the device file, which then passes it to the device driver, which sends it to the physical device. Reverse data channels can also be used, from physical devices through device drivers to device files, to applications or other devices.
Let's take a visual look at the data flow of a typical command.
Figure 1: a simple data flow for a typical command.
In figure 1 above, a simplified data flow of a simple command is shown. Issue the cat / etc/resolv.conf command from a GUI terminal emulator, such as Konsole or xterm, which reads the resolv.conf file from disk, and the disk device driver handles the specific functions of the device, such as locating the file on the hard drive and reading it. The data is transferred through the device file, and then from the command to the device file, then to the device driver of the No. 6 pseudo terminal, and then displayed in the terminal session.
Of course, the output of the cat command can be redirected to a file, cat / etc/resolv.conf > / etc/resolv.bak, in the following way, which creates a backup of that file. In this case, the data traffic on the left side of figure 1 will remain the same, while the data traffic on the right will go through the / dev/sda2 device file, the hard disk device driver, and then to the hard drive itself.
These device files make it easy to access any device on a Linux or Unix computer using standard streams (STD/IO) and redirects. Data can be sent to the device simply by directing the data stream to the device file.
Device file category
Device files can be divided in at least two ways. * the most commonly used classification is based on the data flow associated with the device. For example, tty (teletype) and serial devices are considered character-based because data streams are transmitted and processed one character or byte at a time, while block-type devices (such as hard drives) transmit data in blocks, usually multiples of 256bytes.
You can change the current working directory (PWD) to / dev and display a long directory list as a non-root user on the terminal. This displays a list of device files, file permissions, and their primary and secondary number. For example, the device files below are just a few files in the / dev directory on my Fedora 24 workstation. They represent disk and tty device types. Notice the leftmost character of each line in the output. B stands for block type device and c for character device.
Brw-rw---- 1 root disk 8, 0 Nov 7 07:06 sda brw-rw---- 1 root disk 8, 1 Nov 7 07:06 sda1 brw-rw---- 1 root disk 8, 16 Nov 7 07:06 sdb brw-rw---- 1 root disk 8, 17 Nov 7 07:06 sdb1 brw-rw---- 1 root disk 8, 18 Nov 7 07:06 sdb2 crw--w---- 1 root tty 4, 0 Nov 7 07:06 tty0 crw--w---- 1 root tty 4 1 Nov 7 07:07 tty1 crw--w---- 1 root tty 4, 10 Nov 7 07:06 tty10 crw--w---- 1 root tty 4, 11 Nov 7 07:06 tty11
A more detailed and explicit way to identify device files is to use device primary and secondary numbers. The disk devices master number is 8 and they are designated as SCSI block devices. Note that all PATA and SATA hard drives are managed by the SCSI subsystem because the old ATA subsystem was considered unmaintainable many years ago because of poor code quality. As a result, the hard drive formerly known as "HD [a-z]" is now called "SD [a-z]".
You can probably derive the mode of disk drive secondary device number from the above example. Secondary device numbers 0, 16, 32, and so on, up to 240, is the number of the entire disk. So the primary / secondary 8max 16 represents the entire disk / dev/sdb, and 8max 17 is the device file of * partitions, / dev/sdb1. The number 8x34 stands for / dev/sdc2.
The tty device file number in the list above is simpler, from tty0 to tty63.
The assigned device file under Linux on Kernel.org is the official registry of device type and primary and secondary number assignments. It helps you understand the primary / minor numbers of all currently defined devices.
Interesting equipment file
Let's take a few minutes to perform several interesting experiments to demonstrate the power and flexibility of Linux device files. Most Linux distributions have one to seven virtual consoles that can be used to log in to a local console session using the shell interface. It can be accessed by using keyboard combinations such as Ctrl-Alt-F1 (console 1) and Ctrl-Alt-F2 (console 2).
Press Ctrl-Alt-F2 to switch to console 2. In some distributions, the login displays information that includes the tty devices associated with this console, but most of them do not. It should be tty2, because you are in console 2.
Log in as a non-root user. Then you can use the who am i command-yes, this command, with spaces-to determine which tty device is connected to the console.
Before we actually do this lab, take a look at the list of devices for tty2 and tty3 in / dev.
Ls-l / dev/tty [23]
There are a lot of tty devices, but we don't care about most of them, just tty2 and tty3 devices. As device files, they are nothing special. They are all just character type devices. We will use these devices to carry out this experiment. The tty2 device is connected to the virtual console 2 and the device 3 is connected to the virtual console 3.
Press Ctrl-Alt-F3 to switch to console 3. Log in as the same non-root user again. Now enter the following command on console 3.
Echo "Hello world" > / dev/tty2
Press Ctrl-Alt-f2 to return to console 2. The string "Hello world" (without quotation marks) will be displayed on console 2.
The experiment can also be performed using a terminal emulator on the GUI desktop. The terminal session on the desktop uses a pseudo terminal device in / dev, such as / dev/pts/1. Open two terminal sessions using Konsole or Xterm. Determine which pseudo terminals they are connected to and use one to send messages to the other.
Now continue the experiment and use the cat command to try displaying the / etc/fstab file on different terminals.
Another interesting experiment is to use the cat command to print the file directly to the printer. Assuming your printer device is / dev/usb/lp0 and your printer can print PDF files directly, the following command will print test.pdf files on your printer.
Cat test.pdf > / dev/usb/lp0
The / dev directory contains some very interesting device files that are entrances to the hardware, and people usually don't think of them as devices such as hard drives or monitors. For example, the system memory RAM is not generally considered a "device", while / dev/mem is the entry through which direct access to the memory can be achieved. The following example has some interesting results.
Dd if=/dev/mem bs=2048 count=100
The dd command above provides more control over the memory of all systems than simply using the cat command to dump all systems. It provides the ability to specify how much data to read from / dev/mem and also allows you to specify where to start reading data from memory. Although some memory is read, the kernel responds to the following error, as you can see in / var/log/messages.
Nov 14 14:37:31 david kernel: usercopy: kernel memory exposure attempt detected from ffff9f78c0010000 (dma-kmalloc-512) (2048 bytes)
This error means that the kernel is doing its job by protecting memory that belongs to other processes, which is exactly how it should work. So, although / dev/mem can be used to display data stored in RAM memory, most of the memory space accessed is protected and can cause errors. Only the virtual memory allocated by the kernel memory manager to the BASH shell running the dd command can be accessed without causing an error. I'm sorry, but you can't snoop on memory that doesn't belong to you unless you find an exploitable vulnerability.
There are also some very interesting device files in / dev. The device files null,zero,random and urandom are not associated with any physical devices.
For example, an empty device / dev/null can be used as a target for output redirection from shell commands or programs so that they do not appear on the terminal. I often use this in my BASH scripts to prevent users from showing output that might confuse them. The / dev/null device can be used to generate an empty string. Use the dd command shown below to view some output from the / dev/null device file.
# dd if=/dev/null bs=512 count=500 | od-c 0 records in 0 records out0 bytes copied, 1.5885e-05 s, 0.0 kB/s 0000000
Note that there is really no visible output because there is nothing in the empty character. Look at the number of bytes.
/ dev/random and / dev/urandom devices are also interesting. As their name implies, they all produce random output, not just numbers, but any combination of bytes. / dev/urandom devices produce deterministic random output and are very fast. This means that the output is determined by the algorithm and uses the seed string as the starting point. As a result, if the original seed is known, the hacker can reproduce the output, which is very difficult, but it is possible. Use the command cat / dev/urandom to view the typical output and exit with Ctrl-c.
The / dev/random device file generates non-deterministic random output, but it produces slower output. The output is not determined by algorithms that rely on previous numbers, but by keystrokes and mouse movements. This approach makes it much more difficult to copy a specific series of random numbers. Use the cat command to view some output from / dev/random device files. Try moving the mouse to see how it affects the output.
As the name implies, the / dev/zero device file produces an endless zero as output. Note that these are octal zeros, not ASCII character zeros (0). Use the dd shown below to view some output in the / dev/zero device file
# dd if=/dev/zero bs=512 count=500 | od-c 0000000\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0 * 500 kB 0 records out 256000 bytes (0.00126996 kB, 250 KiB) copied, 0.00126996 s, 202 MB/s 0764000
Note that the number of bytes in this command is not zero.
Create a device file
In the past, device files in / dev were created at installation time, resulting in almost all device files in a directory, although most files were never used. In infrequent situations, such as the need for a new device file, or the need to recreate the device file after accidental deletion, you can use the mknod program to create the device file manually. The premise is that you must know the primary and secondary numbers of the equipment.
CentOS and RHEL 6, 7, and all versions of Fedora-- which can be traced back to at least Fedora 15-- use the newer method of creating device files. All device files are created at boot time. This is because udev device Manager detects when device additions and deletions occur. This enables true dynamic plug and play when the host is started and running. It also performs the same task at boot time by detecting all devices installed on the system early in the boot process. There is a great description of udev on Linux.com.
Go back to the list of files in / dev and notice the date and time of the file. All files were created the last time you started. You can use the uptime or last command to verify this. In my device list above, all of these files were created at 7:06 on November 7, which was the first time I booted the system.
Of course, the mknod command is still available, but the new MAKEDEV (yes, all uppercase, which in my opinion violates Linux's principle of using lowercase command names) provides an easier interface for creating device files. In the current version of Fedora or CentOS 7, the MAKEDEV command is not installed by default; it is installed in CentOS 6. You can use YUM or DNF to install the MAKEDEV package.
Thank you for reading this article carefully. I hope the article "how to manage equipment in Linux" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.