Skip to content

Kernel Modules and System Tuning

Category: Advanced Linux Administration
Type: Linux Commands
Generated on: 2025-07-10 03:16:27
For: System Administration, Development & Technical Interviews


Kernel Modules and System Tuning Cheatsheet (Linux)

Section titled “Kernel Modules and System Tuning Cheatsheet (Linux)”

This cheatsheet provides a comprehensive guide to managing kernel modules and tuning system performance on Linux. It covers essential commands, practical examples, and troubleshooting tips for both beginners and advanced system administrators.

1. Command Overview

This section focuses on commands for managing kernel modules and tuning system performance.

  • lsmod: Lists currently loaded kernel modules. Useful for identifying what drivers and functionalities are active.
  • modinfo: Displays information about a specific kernel module. Essential for understanding a module’s purpose, dependencies, and parameters.
  • insmod: Inserts a kernel module into the running kernel. Used for manually loading modules that aren’t automatically loaded.
  • rmmod: Removes a kernel module from the running kernel. Used for unloading modules that are no longer needed or causing issues.
  • modprobe: Intelligently adds or removes modules, resolving dependencies. This is the preferred method for module management.
  • update-initramfs: Updates the initial RAM disk image. Necessary after changes to kernel modules that are required during boot.
  • sysctl: Reads and modifies kernel parameters at runtime. Powerful for system tuning and performance optimization.
  • vmstat: Reports virtual memory statistics. Helps diagnose memory-related performance issues.
  • iostat: Reports CPU utilization and disk I/O statistics. Essential for identifying disk bottlenecks.
  • top / htop: Displays real-time system processes and resource usage. Useful for identifying resource-intensive processes.
  • free: Displays the amount of free and used memory in the system.
  • nice / renice: Adjusts the priority of a process.
  • ionice: Adjusts the I/O scheduling class and priority of a process.

2. Basic Syntax

  • lsmod: lsmod
  • modinfo: modinfo <module_name> (e.g., modinfo usb_storage)
  • insmod: insmod <module_path> (e.g., insmod /lib/modules/$(uname -r)/kernel/drivers/usb/storage/usb-storage.ko)
  • rmmod: rmmod <module_name> (e.g., rmmod usb_storage)
  • modprobe: modprobe <module_name> (e.g., modprobe usb_storage); modprobe -r <module_name> (remove)
  • update-initramfs: update-initramfs -u (update)
  • sysctl: sysctl <parameter> (read); sysctl -w <parameter>=<value> (write - temporary); sysctl -p (load from /etc/sysctl.conf)
  • vmstat: vmstat [delay [count]] (e.g., vmstat 1 5 - report every 1 second, 5 times)
  • iostat: iostat [ -c ] [ -d ] [ -k | -m ] [ -N ] [ -n ] [ -p [ device,... ] ] [ -t ] [ -x ] [ device [,...] | ALL ] [ interval [ count ] ]
  • top: top
  • htop: htop
  • free: free [-b|-k|-m|-g] [-t] [-s delay -c count]
  • nice: nice -n <priority_value> <command> (e.g., nice -n 10 ./my_process)
  • renice: renice -n <priority_value> -p <process_id>
  • ionice: ionice -c <class> -n <priority> <command> (e.g., ionice -c 2 -n 4 ./backup_script.sh)

3. Practical Examples

  • Listing Loaded Modules:

    Terminal window
    lsmod
    Module Size Used by
    usb_storage 73728 1
    ...
  • Getting Information about a Module:

    Terminal window
    modinfo usb_storage
    filename: /lib/modules/5.15.0-76-generic/kernel/drivers/usb/storage/usb-storage.ko
    license: GPL
    author: David Brownell
    description: USB Mass Storage driver for removable media
    alias: usb:v*p*d*dc08dsc50dp*ic*isc*ip*in*
    ...
  • Loading a Module: (using modprobe - recommended)

    Terminal window
    sudo modprobe usb_storage
  • Unloading a Module: (using modprobe - recommended)

    Terminal window
    sudo modprobe -r usb_storage
  • Updating initramfs: (After installing a new driver)

    Terminal window
    sudo update-initramfs -u -k all
  • Checking a sysctl value:

    Terminal window
    sysctl vm.swappiness
    vm.swappiness = 60
  • Temporarily Changing a sysctl value:

    Terminal window
    sudo sysctl -w vm.swappiness=10
  • Making a sysctl change persistent:

    Edit /etc/sysctl.conf or create a file in /etc/sysctl.d/:

    Terminal window
    sudo nano /etc/sysctl.d/99-sysctl.conf

    Add the line: vm.swappiness=10

    Then apply the changes:

    Terminal window
    sudo sysctl -p
  • Monitoring System Performance (vmstat):

    Terminal window
    vmstat 1 5

    This will display virtual memory statistics every 1 second, for 5 iterations. Pay attention to si (swap in) and so (swap out) columns, which indicate swapping activity.

  • Monitoring Disk I/O (iostat):

    Terminal window
    iostat -x sda 1

    This shows extended statistics for the sda device every 1 second. Look for high %util (device utilization) indicating a potential bottleneck.

  • Adjusting Process Priority (nice):

    Terminal window
    nice -n 19 ./cpu_intensive_task.sh

    This runs cpu_intensive_task.sh with the lowest possible priority (19).

  • Adjusting Process I/O Priority (ionice):

    Terminal window
    ionice -c 2 -n 7 ./backup.sh

    This runs backup.sh with the lowest I/O priority (best effort class, priority 7).

4. Common Options

  • modprobe:
    • -r: Remove the module.
    • -v: Verbose mode (shows what’s happening).
    • -n: Dry run (don’t actually load/unload).
    • --remove-dependencies: Remove all modules that depend on the target module.
  • sysctl:
    • -w: Write a new value to the parameter (temporary).
    • -p: Load settings from a configuration file.
    • -a: Display all kernel parameters.
  • vmstat:
    • delay: The interval in seconds between reports.
    • count: The number of reports to generate.
  • iostat:
    • -x: Display extended statistics.
    • -d: Display disk statistics.
    • -c: Display CPU statistics.
    • -k / -m: Report in kilobytes or megabytes.
  • nice:
    • -n <priority_value>: Sets the niceness value. Lower niceness values mean higher priority (range -20 to 19).
  • renice:
    • -n <priority_value>: Sets the niceness value.
    • -p <process_id>: Specifies the process ID to renice.
  • ionice:
    • -c <class>: Sets the I/O scheduling class.
      • 0: None
      • 1: Realtime
      • 2: Best-effort
      • 3: Idle
    • -n <priority>: Sets the I/O priority within the class. Ranges from 0-7 (lower is higher priority) for Realtime and Best-effort. Idle class ignores priority.

5. Advanced Usage

  • Blacklisting Modules: Prevent a module from loading automatically.

    Create a file in /etc/modprobe.d/ (e.g., /etc/modprobe.d/blacklist.conf):

    blacklist <module_name>

    Example: blacklist nouveau (prevents the Nouveau Nvidia driver from loading)

  • Setting Module Options: Pass parameters to a module when it loads.

    Create a file in /etc/modprobe.d/ (e.g., /etc/modprobe.d/options.conf):

    options <module_name> <parameter>=<value>

    Example: options iwlwifi 11n_disable=1 (disables 802.11n for the iwlwifi module)

  • Using sysctl for TCP Tuning: Optimize TCP settings for high-performance networking.

    Terminal window
    sudo sysctl -w net.ipv4.tcp_rmem="4096 87380 6291456"
    sudo sysctl -w net.ipv4.tcp_wmem="4096 16384 4194304"
    sudo sysctl -w net.core.rmem_max=2147483647
    sudo sysctl -w net.core.wmem_max=2147483647

    Make these persistent by adding them to /etc/sysctl.conf or a file in /etc/sysctl.d/.

  • Using perf for Deep Performance Analysis: While vmstat, iostat, and top give you a general overview, perf allows you to drill down into specific functions and instructions that are consuming CPU time. This is a very advanced topic and requires significant understanding of system internals.

    Terminal window
    sudo perf record -g ./my_application
    sudo perf report

6. Tips & Tricks

  • Tab Completion: Use tab completion in the shell to quickly find module names and sysctl parameters.
  • uname -r: Use uname -r to get the current kernel version when specifying module paths.
  • Backup sysctl.conf: Always back up your /etc/sysctl.conf file before making changes.
  • Test sysctl Changes: Test sysctl changes in a non-production environment before applying them to production servers.
  • Read Module Documentation: Refer to the module’s documentation (often found online) for detailed information about its parameters.
  • Use a Monitoring System: Implement a monitoring system (e.g., Prometheus, Grafana) to track system performance metrics over time. This is crucial for identifying trends and potential bottlenecks.
  • Avoid Unnecessary Modules: Keep the number of loaded modules to a minimum to reduce the kernel’s footprint and improve security.
  • Reboot After initramfs changes: After making changes to the initramfs, reboot the system to ensure the changes are applied correctly.

7. Troubleshooting

  • Module Fails to Load:

    • Check the system logs (/var/log/syslog or /var/log/messages) for error messages.
    • Verify that the module exists in the correct directory (/lib/modules/$(uname -r)/kernel/).
    • Ensure that the module is compiled for the correct kernel version.
    • Check for missing dependencies using modprobe -v <module_name>.
  • System Hangs After Module Load:

    • Try booting into recovery mode and unloading the problematic module.
    • Check the system logs for error messages related to the module.
    • Consult the module’s documentation or online forums for known issues.
  • sysctl Changes Not Applied:

    • Verify that the changes are correctly entered in /etc/sysctl.conf or a file in /etc/sysctl.d/.
    • Run sudo sysctl -p to reload the configuration.
    • Check for syntax errors in the configuration file.
  • High CPU/Disk Usage:

    • Use top or htop to identify the processes consuming the most resources.
    • Use iostat to identify disk I/O bottlenecks.
    • Use vmstat to identify memory-related issues.
    • Use perf to profile the code and find bottlenecks.
  • Error: modprobe: FATAL: Module <module_name> not found in directory /lib/modules/$(uname -r)

    • Solution: Ensure the module is installed and located in the correct directory. Run sudo depmod -a to update module dependencies. Verify that the kernel headers are installed (sudo apt install linux-headers-$(uname -r) on Debian/Ubuntu).
  • Error: rmmod: ERROR: Module <module_name> is in use

    • Solution: The module is currently being used by another process or module. Identify the processes using lsof /path/to/module.ko or lsmod to see which modules depend on it. Stop those processes or unload the dependent modules first (carefully!).

8. Related Commands

  • dmesg: Displays kernel messages (useful for troubleshooting module loading).
  • lspci / lsusb: Lists PCI and USB devices, respectively. Helps identify hardware that might require specific modules.
  • udevadm: Manages the udev device manager, which dynamically creates and removes device nodes in /dev.
  • systemd-analyze: Analyzes system boot time and performance.
  • ps: Displays information about running processes.
  • lsof: Lists open files (including modules).
  • ethtool: Displays and modifies network interface settings.
  • ss: Socket Statistics (more modern alternative to netstat).
  • iptables / nftables: Firewall configuration.

This cheatsheet provides a foundation for managing kernel modules and tuning system performance. Remember to consult the official documentation and online resources for more detailed information and specific use cases. Always exercise caution when modifying kernel parameters or loading/unloading modules, and test changes in a non-production environment first.