1. Overview

Storage is a vital part of every computer system. Naturally, monitoring and maintaining our storage is important to prevent software and hardware damage. One of the most important parameters, especially in SSDs, is temperature. In this tutorial, we’ll introduce some ways to read HDD (Hard Disk Drive) and SSD (Solid-State Drive) temperatures from the Linux command line.

2. What Is S.M.A.R.T.?

S.M.A.R.T. (often written as SMART), which stands for “Self-Monitoring, Analysis, and Reporting Technology”, is a monitoring system included in recent HDDs and SSDs. This system was developed to detect and report some indicators of drive reliability with the intent of anticipating imminent hardware failures. One of those parameters is temperature.

So, to monitor drive temperature, we can use most software tools that retrieve SMART data. In this tutorial, we’ll introduce some of these tools and learn how to read temperatures from SMART data.

3. Using smartctl to Read S.M.A.R.T. Data

To read SMART data in the command line, we can use the smartctl command. We must pass the device address (for example, /dev/sda) to it as an argument, and to print all data, we can use the -a option. In the result, we can see a lot of information including drive temperature in Celsius:

$ sudo smartctl -a /dev/sda
194 Temperature_Celsius     0x0000   100   100   000    Old_age   Offline      -       18

Note that this command needs root privileges. So, we must run it as a root user or use sudo.

Also, we can filter the results using the grep command:

$ sudo smartctl -a /dev/sda | grep "Temperature"
194 Temperature_Celsius     0x0000   100   100   000    Old_age   Offline      -       18

4. Check Temperature Using hddtemp

hddtemp is a special command for reading drive temperature. Its usage is pretty simple. We just pass the device address as an argument and it prints the temperature in Celcius. Like smartctl, hddtemp needs root privileges. So, we must run it with sudo or as a root user:

$ sudo hddtemp /dev/sda
/dev/sda: TS128GSSD370S: 18°C

Notice that some drives may not support SMART at all, or their SMART information may not contain temperature. For example, most USB flash drives don’t support SMART:

$ sudo hddtemp /dev/sdb
/dev/sdb: General USB Flash Disk: S.M.A.R.T. not available

If we want the temperature of all or multiple drives, we can use wildcards in the device name. For example, we could use a ? wildcard to check all drives whose names are /dev/sd followed by a single character:

$ sudo hddtemp /dev/sd?
/dev/sda: TS128GSSD370S: 18°C
/dev/sdb: General USB Flash Disk: S.M.A.R.T. not available

Or, if we’re only interested in the first three devices that start with /dev/sd, we could use /dev/sd[abc] as the device name parameter.

Also, hddtemp offers some extra features. For instance, it can run as a daemon to provide a TCP/IP interface for reading temperature via the network. Using the -d option, hddtemp executes in the background and opens a TCP port (port 7634 by default). Furthermore, we can change the port by using the -p option.

For example, if we want to execute the hddtemp as a daemon on port 1234, we can run:

$ sudo hddtemp /dev/sda -dp1234

To read a temperature value from the daemon, we can use telnet or nc (netcat) commands:

$ nc localhost 1234

5. Read Temperature Without Installing Any Package

If we need to read the temperature without installing any extra package or software, we can use drivetempa kernel module that reads drive temperature on Linux kernel 5.6 and earlier. To load this module on the kernel, we need to use the modprobe command with sudo or as a root user:

$ sudo modprobe drivetemp

After loading that, we can read the temperature from the /sys/class/hwmon/ directory. There are some subdirectories there, and each of them belongs to one hardware device. We can see device names by reading the content of the /sys/class/hwmon/hwmon*/name directory:

$ cat /sys/class/hwmon/hwmon*/name

As we can see, only one of them belongs to drive temperature. To find out which directory contains that, we can use the grep command with the -l option:

$ grep -l "drivetemp" /sys/class/hwmon/hwmon*/name

In this case, to see the drive model and the current temperature, we must print the content of /sys/class/hwmon/hwmon4/device/model and /sys/class/hwmon/hwmon4/temp1_input directories:

$ cat /sys/class/hwmon/hwmon4/device/model

$ cat /sys/class/hwmon/hwmon4/temp1_input

Note that the temperature is reported in milli-degrees Celsius. So, if we want to change it to Celsius, we must divide it by 1,000. Also, we can scan all directories to find the drive temperature directory, then print the model and temperature in Celsius by this bash loop:

$ grep -l "drivetemp" /sys/class/hwmon/hwmon*/name | while read f;\
>        do echo `cat ${f%/*}/device/model` $((`cat ${f%/*}/temp1_input`/1000));
> done
TS128GSSD370S 21

6. Conclusion

In this article, first, we introduced the S.M.A.R.T. system. Then we talked about some Linux command-line applications that can read data, especially temperature, from the HDD or SSD S.M.A.R.T. system. Then at the end, we used a Linux kernel module to read the drive temperature without using any external package.

Comments are closed on this article!