Posted on
Questions and Answers

Script fan speed control using `lm-sensors` and `pwmconfig`

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Control Fan Speed in Linux Using lm-sensors and pwmconfig: A Q&A Guide

Many Linux users who manage their own hardware, especially in the context of desktops or self-built computers, might sometimes find it necessary to adjust the fan speed to optimize cooling or reduce noise. This article explores how to control fan speeds using lm-sensors and pwmconfig.

Q1: What are lm-sensors and pwmconfig?

A1: lm-sensors is a Linux tool that provides essential drivers and utilities to monitor the temperature, voltage, and fans in a system. pwmconfig is a script that comes with lm-sensors, which helps users configure fan speed control settings by manipulating the PWM (Pulse Width Modulation) signals based on the sensor readings.

Q2: How do I install lm-sensors and pwmconfig?

A2: You can install both tools via your Linux distribution’s package manager. For Ubuntu or Debian-based distributions, use:

sudo apt-get install lm-sensors

This command typically installs both lm-sensors and the scripts needed for pwmconfig. To confirm, you can run pwmconfig directly in the terminal to see if it executes.

Q3: How do I use lm-sensors to check my system’s current temperatures and fan speeds?

A3: First, run the following command to detect all sensors on your system:

sudo sensors-detect

Follow the on-screen instructions and agree to add the new modules to /etc/modules. After this, use:

sensors

This command will display all the temperatures, voltages, and fan speeds currently detected by your system.

Q4: How do I set up and use pwmconfig to control fan speed based on temperature?

A4: To configure your fan speed controls, start pwmconfig with:

sudo pwmconfig

This script will guide you through creating a configuration file /etc/fancontrol by testing the PWM outputs on your system. You'll be able to define the temperature thresholds and corresponding fan speeds.

Q5: Are there any risks in manually controlling fan speeds?

A5: Yes, there are risks. Incorrect configurations might prevent fans from spinning at necessary speeds to cool down the hardware, potentially leading to overheating and damage. Always monitor temperatures regularly and ensure settings allow the fan to reach full speed if needed.

Background and Simple Example

The core functionality behind lm-sensors and pwmconfig revolves around detecting and manipulating hardware signals directly. This approach can be very powerful but requires some user knowledge and attention to detail. Here’s an outline of a simplified usage of these tools:

  1. Detecting sensors and testing: It's crucial first to understand what sensors are available and their respective outputs. sensors-detect helps in identifying these sensors and making them actively monitored by the system.

  2. Setting fan speed policies: The pwmconfig utility allows you to test each controllable fan to ensure it responds to PWM signal changes and thereafter set up rules; such as "increase fan speed when the CPU temperature surpasses 50 degrees Celsius".

Example Script

Here's a basic example script to automate reading temperatures and adjusting fan speed accordingly – a Linux bash script utilizing lm-sensors output:

#!/bin/bash
# Load common functions for `sensors`
. /usr/share/acpi-support/power-funcs

# Define the maximum acceptable temperature
MAX_TEMP=50

# Get the current CPU temperature (replace 'coretemp-isa-0000' with the sensor appropriate for your system)
CURRENT_TEMP=$(sensors | grep 'coretemp-isa-0000' | grep -Eo '[0-9]{2,3}' | head -1)

# Check if the temperature is above the threshold
if [[ "$CURRENT_TEMP" -gt "$MAX_TEMP" ]]; then
    # Speed up the fan (max PWM value might vary)
    echo 255 > /sys/class/hwmon/hwmon0/device/pwm1
else
    # Slow down the fan (set a safer minimum)
    echo 100 > /sys/class/hwmon/hwmon0/device/pwm1
fi

Summary Conclusion

While controlling fan speeds in Linux using lm-sensors and pwmconfig might initially seem daunting, it provides robust tools for detailed management of your system’s cooling efficacy. Careful setup and maintenance can yield enhanced performance and quieter operation – key benefits for any Linux user with hardware they directly manage. As always, tread carefully with system configurations that affect hardware operation.

Further Reading

For further reading on controlling fan speeds and related hardware management in Linux, consider exploring these resources:

These articles provide both fundamental and advanced insights into managing hardware through Linux, specifically focusing on thermal management and performance enhancement.