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:lsmodmodinfo: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:tophtop:htopfree: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 lsmodModule Size Used byusb_storage 73728 1... -
Getting Information about a Module:
Terminal window modinfo usb_storagefilename: /lib/modules/5.15.0-76-generic/kernel/drivers/usb/storage/usb-storage.kolicense: GPLauthor: David Brownelldescription: USB Mass Storage driver for removable mediaalias: 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.swappinessvm.swappiness = 60 -
Temporarily Changing a sysctl value:
Terminal window sudo sysctl -w vm.swappiness=10 -
Making a sysctl change persistent:
Edit
/etc/sysctl.confor create a file in/etc/sysctl.d/:Terminal window sudo nano /etc/sysctl.d/99-sysctl.confAdd the line:
vm.swappiness=10Then apply the changes:
Terminal window sudo sysctl -p -
Monitoring System Performance (vmstat):
Terminal window vmstat 1 5This will display virtual memory statistics every 1 second, for 5 iterations. Pay attention to
si(swap in) andso(swap out) columns, which indicate swapping activity. -
Monitoring Disk I/O (iostat):
Terminal window iostat -x sda 1This shows extended statistics for the
sdadevice 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.shThis runs
cpu_intensive_task.shwith the lowest possible priority (19). -
Adjusting Process I/O Priority (ionice):
Terminal window ionice -c 2 -n 7 ./backup.shThis runs
backup.shwith 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: None1: Realtime2: Best-effort3: 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 theiwlwifimodule) -
Using
sysctlfor 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=2147483647sudo sysctl -w net.core.wmem_max=2147483647Make these persistent by adding them to
/etc/sysctl.confor a file in/etc/sysctl.d/. -
Using
perffor Deep Performance Analysis: Whilevmstat,iostat, andtopgive you a general overview,perfallows 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_applicationsudo perf report
6. Tips & Tricks
- Tab Completion: Use tab completion in the shell to quickly find module names and
sysctlparameters. uname -r: Useuname -rto get the current kernel version when specifying module paths.- Backup
sysctl.conf: Always back up your/etc/sysctl.conffile before making changes. - Test
sysctlChanges: Testsysctlchanges 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/syslogor/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>.
- Check the system logs (
-
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.
-
sysctlChanges Not Applied:- Verify that the changes are correctly entered in
/etc/sysctl.confor a file in/etc/sysctl.d/. - Run
sudo sysctl -pto reload the configuration. - Check for syntax errors in the configuration file.
- Verify that the changes are correctly entered in
-
High CPU/Disk Usage:
- Use
toporhtopto identify the processes consuming the most resources. - Use
iostatto identify disk I/O bottlenecks. - Use
vmstatto identify memory-related issues. - Use
perfto profile the code and find bottlenecks.
- Use
-
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 -ato update module dependencies. Verify that the kernel headers are installed (sudo apt install linux-headers-$(uname -r)on Debian/Ubuntu).
- Solution: Ensure the module is installed and located in the correct directory. Run
-
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.koorlsmodto see which modules depend on it. Stop those processes or unload the dependent modules first (carefully!).
- Solution: The module is currently being used by another process or module. Identify the processes using
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 theudevdevice 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 tonetstat).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.