🐧 Linux

Updated at 2016-12-10 02:11

This note is about configuring hardware devices in Linux systems.

lspci lists devices in your computer.

00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371SB PIIX3 IDE [Natoma/Triton II]
00:01.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 01)
00:02.0 VGA compatible controller: Cirrus Logic GD 5446
00:03.0 Unassigned class [ff80]: XenSource, Inc. Xen Platform Device (rev 01)

lspci -v -k
# for more verbose information

"Everything is a file" philosophy applies to hardware.

cat /proc/cpuinfo       # CPU overview
cat /proc/interrupts    # shows how interrupts are distributed between devices
cat /proc/version       # kernel version
uname -a                # filtered result of /proc/version

ls -l /sys/block/           # lists attached disks
find /sys -iname \*eepc\*   # search for drivers of hardware buttons on Eee PC
cat /sys/bus/platform/devices/eeepc/camera # is camera hardware button enabled?
echo 1 > /sys/bus/platform/devices/eeepc/rfkill:rfkill0/state # turn on WiFi

Linux hardware is configured with kernel modules. This allows changing hardware and their settings without booting the system.

rmmod modulename # unloads kernel module if not used by any process
modprobe modulename option1=value # configure and load kernel module

modinfo shows available options while loading the module.

modinfo /lib/modules/4.4.23-31.54.amzn1.x86_64/kernel/drivers/cdrom/cdrom.ko
filename:       /lib/modules/4.4.23-31.54.amzn1.x86_64/kernel/drivers/cdrom/cdrom.ko
license:        GPL
srcversion:     05419E88D1E55A3545ADE7F
intree:         Y
vermagic:       4.4.23-31.54.amzn1.x86_64 SMP mod_unload modversions
parm:           debug:bool
parm:           autoclose:bool
parm:           autoeject:bool
parm:           lockdoor:bool
parm:           check_media_type:bool
parm:           mrw_format_restart:bool

# `alias` field tells the device/product id
# and vendor id of the hardware that this module responsible for
alias:          cpu:type:x86,ven*fam*mod*:feature:*00E8*
alias:          cpu:type:x86,ven*fam*mod*:feature:*0016*

Some module options can be configured using the file system. Works in modern 2.6+ kernels.

cat /sys/module/printk/parameters/console_suspend

Disk are partitioned using MBR or GPT standard.

  • MBR: A disk is divided to partitions, defined in Master Boot Record (MBR) on the disk. Max 4 partitions and 2TB per partition.
  • GPT: GUID Partition Table (GPT) is a new standard that should be used if the disk supports it. Max 128 partitions.
fdisk -l                # all disk info
fdisk -l /dev/xvda      # disk info
fdisk -l /dev/xvda1     # partition info
fdisk /dev/xvda         # start interactive mode for e.g. creating partitions

gdisk -l /dev/xvda      # disk info
gdisk /dev/xvda         # start interactive mode
sda   # SATA Drive, First (A)
sda1  # SATA Drive, First (A), First partition (1)
sdb   # SATA Drive, Second (B)
sdb1  # SATA Drive, Second (B), First partition (1)
xvda1 # Xen Virtual Block Drive, First (A), First partition (1)

# there are frequently symbolic links between device names
ls -la /dev/sda1
... /dev/sda1 -> xvda1
mkfs -h
mkfs -t ext4 /dev/xvda1 # creates ext4 type filesystem to partition xvda1
mkfs -t ext4 -b 4096 /dev/xvda1 # force 4096-byte blocks
mkfs.ext4 # there are also file system specific helper tools

tune2fs     # tool for configuring ext2, ext3 and ext4 file systems
e4defrag    # tool for defragmenting ext4 file system
df -h            # show all partition info in human-readable format
df -h /dev/xvda1 # show information on a specific partition
du -ha /tmp/     # show storage usage of this directory tree

Mounting means associating directory to media device. Devices such as hard drives, CDs or USB disk. Most modern distros handle mounting automatically but manual mounting is sometimes required for debugging hardware problems.

cat /etc/fstab   # lists file systems that are mounted on boot
mount            # lists currently mounted devices
mount /dev/sdc1 /media/usb # mount device `sdc` in an existing directory
mount -o remount,ro /dev/sdc1 /media/usb # remount as read-only
unmount /media/usb         # unmount, removing the linking

# sometimes unmounting is blocked by open file
# then you need to stop the process that is using the file
lsof            # list open files (and which process id is using it)
lsof /dev/xvda1 # list open files on /dev/xvda1
ncal                # prints command-line calendar for this month
ncal -y             # prints command-line calendar for this year
ncal -wy            # also print week numbers

date                # print date and time

# you can customize the used locale with environment variables
LC_TIME="de_DE.utf8" date
TZ="America/Los_Angeles" date


  • Linux Shell Handbook, 7th Edition