Special File Types  «Prev  Next»
Lesson 3 Device files and other special files
ObjectiveDescribe the Special Properties of Device Files.

Describe the Special Properties of Device Files

Access to hardware devices on a UNIX system is managed through special files. These files have
  1. permissions,
  2. owners, and
  3. groups,

just as regular files and directories do, and this permission data determines access to the associated device. Most device files are, by convention, located in a directory called /dev (or in subdirectories of /dev). Device files are marked in directory listings by b or c in the initial position:
crw-------  1 root system 10, 0 Oct 01 1997  __vg10
brw-rw----  1 root system 10, 1 Aug 14 1996  hd6

Here the b and c denote, respectively, block devices and character devices. This distinction is not particularly significant for the administrator. Every device file also has a major number and a minor number. The major number associates the device with a particular major device[1], and the minor number identifies the particular instance of that device. In the above example, the major and minor numbers appear as 10, 0 and 10, 1 respectively.

Special Properties of device files in Unix

In Unix-like operating systems, device files are special files that represent hardware devices. Here are some key properties and characteristics of device files:
  1. Types of Device Files:
    • Character Devices (c): These handle data one character at a time. They are used for devices where data transfer needs to be synchronous, like terminals or serial ports.
    • Block Devices (b): These handle data in blocks, suitable for devices like hard drives where data can be buffered. Block devices can seek to any location on the device.
  2. Location:
    • Device files are typically found in /dev/. This directory contains all device files that the system recognizes.
  3. Major and Minor Numbers:
    • Each device file has a major number and a minor number.
      • The major number identifies the driver associated with the device.
      • The minor number specifies the particular device controlled by the driver.
  4. Permissions and Ownership:
    • Device files have standard Unix permissions (read, write, execute) but these are interpreted differently:
      • Read usually means read from the device.
      • Write means write to the device.
      • Execute has no meaning for device files but might be used by the system for special operations.
    • Ownership and group membership are crucial for access control to these devices.
  5. Pseudo Devices:
    • Some device files like /dev/null, /dev/zero, /dev/random, and /dev/urandom don't correspond to physical hardware but provide special functionality:
      • /dev/null discards all data written to it and provides EOF on reading.
      • /dev/zero provides an endless supply of null bytes.
      • /dev/random and /dev/urandom provide random numbers, with random being cryptographically secure but might block if entropy is low.
  6. Dynamic Device Creation:
    • Modern Unix systems use udev (or similar systems like devfs in FreeBSD) to dynamically create and manage device nodes at runtime based on hardware detection.
  7. Special Operations:
    • Interaction with these files often requires special system calls or commands:
      • mknod to manually create device files (though this is less common with systems like udev).
      • Tools like dd for direct reading/writing to/from devices.
  8. Security Considerations:
    • Because these files directly interface with hardware, incorrect permissions can lead to security vulnerabilities. For instance, allowing write access to /dev/mem or /dev/kmem can be extremely dangerous.

These properties make device files a unique and powerful aspect of Unix-like systems, providing a uniform interface to interact with both physical and pseudo hardware.

Unix System Administration

Special Properties of device files in Unix

Device files in Unix and Unix-like operating systems are special types of files that provide a way for software and hardware to interact. These special files are used to represent and manage physical and virtual devices, making them accessible in the same way regular files are. Here are some key properties of device files in Unix:
  1. Everything is a File: In accordance with Unix's "everything is a file" philosophy, devices are accessed and manipulated like regular files. You can read from a device file and write to it, much like a regular file.
  2. File Location: Device files typically reside in the /dev directory. This directory is populated at boot time with device files corresponding to the hardware detected by the system.
  3. File Types: Device files are classified into two categories: character (or raw) devices and block devices.
    1. Character Devices: These devices transfer data one character at a time and have no buffering mechanism. This means that data is processed immediately, making it suitable for devices like keyboards or mice, where immediate data transfer is essential.
    2. Block Devices: These devices transfer data in large fixed-size blocks. They have a buffering mechanism and support random access, meaning you can read or write data in non-sequential order. Block devices are commonly used for storage devices like hard drives, flash drives, and CD-ROM drives.
  4. File Naming: The names of device files often give clues about the device they represent. For example, 'sda' refers to the first SCSI hard disk, 'tty' represents a terminal, and 'lp' refers to a printer.
  5. Major and Minor Numbers: Every device file is associated with a pair of numbers: a major number and a minor number. The major number identifies the driver associated with the device. The minor number is used by the kernel to differentiate between devices that use the same driver, such as multiple hard drives.
  6. Permissions: Device files have the same permission settings as regular files. They can be set to read, write, or execute for the file owner, group, and others. This is critical for controlling access to hardware devices. For instance, limiting write access to certain devices can prevent inadvertent data loss.
  7. Inodes: Like regular files, each device file has an inode (index node) that stores its metadata (like permissions, ownership, major and minor numbers).
  8. Symbolic Links: In some cases, more user-friendly symbolic links are created to device files. For example, /dev/dvd might be a symbolic link to the actual DVD drive device file, providing a more intuitive way for users and applications to access the device.

Unix's device files, with their unique properties and functions, serve as an elegant and consistent mechanism for interacting with both physical and virtual devices, and provide a seamless interface for users and applications to interact with hardware.

How IP Addresses Apply to Network Interfaces

To connect to the network, a system must have at least one physical network interface. Each network interface must have its own unique IP address. During Oracle Solaris installation, you must supply the IP address for the first interface that the installation program finds. Usually that interface has the name device-name0, for example eri0 or hme0. This interface is considered the primary network interface. If you add a second network interface to a host, that interface also must have its own unique IP address. When you add the second network interface, the host then becomes multihomed. By contrast, when you add a second network interface to a host and enable IP forwarding, that host becomes a router. Each network interface has a device name, a device driver, and an associated device file in the /devices directory. The network interface might have a device name such as eri or smc0, which are device names for two commonly used Ethernet interfaces.

[1] device: A device is a piece of hardware used by the system, such as hard drives, CD-ROMs, and so on. Devices are treated as a special type of file.

SEMrush Software 3 SEMrush Banner 3