Skip to main content
Engineering LibreTexts

09-B.2: Configuring Linux Devices

  • Page ID
    39113
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    EXAM OBJECTIVES COVERED
    2.7 Explain the use and operation of Linux devices.

    Device Information Files

    There are numerous files spread across the Linux filesystem that are useful for managing the various devices that are connected to or used by the system.

    /proc is a special filesystem in Linux that presents information about processes and other system information in a hierarchical file-like structure, providing a more convenient and standardized method for dynamically accessing process data held in the kernel than traditional tracing methods or direct access to kernel memory. Typically, it is mapped to a mount point named /proc at boot time. The proc file system acts as an interface to internal data structures in the kernel. It can be used to obtain information about the system and to change certain kernel parameters at runtime (sysctl).

    /sys is a pseudo file system provided by the Linux kernel that exports information about various kernel subsystems, hardware devices, and associated device drivers from the kernel's device model to user space through virtual files. In addition to providing information about various devices and kernel subsystems, exported virtual files are also used for their configuration.

    /dev is a device file or special file in an interface to a device driver that appears in a file system as if it were an ordinary file. These special files allow an application program to interact with a device by using its device driver via standard input/output system calls. Using standard system calls simplifies many programming tasks, and leads to consistent user-space I/O mechanisms regardless of device features and functions.

    /etc contains all system related configuration files or its sub-directories. A "configuration file" is defined as a local file used to control the operation of a program; it must be static and cannot be an executable binary.

    udev

    Udev is the Linux subsystem that supplies your computer with device events. In plain English, that means it's the code that detects when you have things plugged into your computer, like a network card, external hard drives (including USB thumb drives), mouses, keyboards, joysticks and gamepads, DVD-ROM drives, and so on. That makes it a potentially useful utility, and it's well-enough exposed that a standard user can manually script it to do things like performing certain tasks when a certain hard drive is plugged in.

    udev rules written by the administrator go in /etc/udev/rules.d/; their file name has to end with .rules. The udev rules shipped with various packages are found in /usr/lib/udev/rules.d/. The files in which the rules are defined are conventionally named with a number as prefix (e.g 50-udev-default.rules) and are processed in lexical order independently of the directory they are in. Files installed in /etc/udev/rules.d, however, override those with the same name installed in the system default path.

    The udevadm Command

    The udevadm command manages udev. There are various options, arguments and subcommands.

    Syntax:

    udevadm [ OPTIONS ] [subcommand] [arguments]
    

    Some of the most frequently used subcommands are shown below. 

    Sub-command Meaning
    control Modify the internal state of the running udev daemon<
    info Query the udev database for device information.
    monitor Listens to the kernel uevents and events sent out by a udev rule and prints the devpath of the event to the console. It can be used to analyze the event timing by comparing the timestamps of the kernel uevent and the udev event.
    test Simulate a udev event run for the given device, and print debug output.
    trigger Request device events from the kernel. Primarily used to replay events at system coldplug time.

    Here is an example of a rule that creates a symlink when a webcam is connected. If this camera is currently connected and has been loaded with the device name /dev/video2, then we can run the udevadm command with some options and we can find out a lot about the webcam.

    pbmac@pbmac-server $ udevadm info --attribute-walk --path=$(udevadm info --query=path --name=/dev/video2)
    
    Udevadm info starts with the device specified by the devpath and then walks up the chain of parent devices.
    It prints for every device found, all possible attributes in the udev rules key format.
    A rule to match, can be composed by the attributes of the device and the attributes from one single parent device.
    
    looking at device '/devices/pci0000:00/0000:00:04.1/usb3/3-2/3-2:1.0/video4linux/video2':
      KERNEL=="video2"
      SUBSYSTEM=="video4linux"
       ...
    looking at parent device '/devices/pci0000:00/0000:00:04.1/usb3/3-2/3-2:1.0':
      KERNELS=="3-2:1.0"
      SUBSYSTEMS=="usb"
      ...
    looking at parent device '/devices/pci0000:00/0000:00:04.1/usb3/3-2':
      KERNELS=="3-2"
      SUBSYSTEMS=="usb"
      ATTRS{idVendor}=="05a9"
      ATTRS{manufacturer}=="OmniVision Technologies, Inc."
      ATTRS{removable}=="unknown"
      ATTRS{idProduct}=="4519"
      ATTRS{bDeviceClass}=="00"
      ATTRS{product}=="USB Camera"

    We can gather some of the information here: SUBSYSTEM, SUBSYSTEMS, and ATTRS (attributes),

    Then we can create a file, /etc/udev/rules.d/83-webcam.rules, that creates a symbolic link when this device is plugged in. In this fashion we can ALWAYS refer to this device as video-cam, no matter if it gets mounted on /dev/video0, or /dev/video1, or wherever.

    KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", SUBSYSTEMS=="usb", ATTRS{idVendor}=="05a9", ATTRS{idProduct}=="4519", 
    SYMLINK+="video-cam"

    Adapted from:
    "procfs" by Multiple ContributorsWikipedia is licensed under CC BY-SA 3.0
    "sysfs" by Multiple ContributorsWikipedia is licensed under CC BY-SA 3.0
    "Device file" by Multiple ContributorsWikipedia is licensed under CC BY-SA 3.0
    "1.6. /etc" by Multiple Contributors, The Linux Documentation Project is in the Public Domain, CC0
    "udev" by Multiple Contributors, Arch Linux is licensed under CC BY-SA 3.0


    09-B.2: Configuring Linux Devices is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?