Black Friday 2025 – NPI EA (cat = Baeldung on Linux)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

We can change the speed of a CPU (Central Processing Unit) by changing its frequency or voltage. There are several CPU frequency scaling algorithms that set a target frequency for the processor and operate the processor within a frequency range. These frequency scaling algorithms are known as CPU governors.

For example, the performance CPU governor forces a processor to run at the highest possible clock frequency. On the other hand, the powersave CPU governor forces a processor to run at the lowest possible clock frequency. Therefore, we can optimize the CPU speed depending on the load.

In this tutorial, we’ll discuss how to set the CPU governor for all cores at once.

2. Using sysfs

The sysfs virtual file system provides an interface to the kernel data structures related to the hardware. We can get and set the CPU governor of a core using sysfs. For example, we can get the governor of the first CPU on our host utilizing the cat command:

$ cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
performance

A subdirectory exists for each core of the host in the /sys/devices/system/cpu directory. For example, the cpu0 subdirectory, /sys/devices/system/cpu/cpu0, corresponds to the first core, while cpu1 corresponds to the second core. The /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor file contains the CPU governor of the first core.

Therefore, we can list the CPU governors of all cores using a single cat command:

$ cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
performance
...
performance

The CPU governor settings for all cores on our host are set to performance. We used the * wildcard, cpu*, to access all the subdirectories in /sys/devices/system/cpu. We truncated the output since there are 24 cores on our host. The number of cores can be checked using the nproc –all command:

$ nproc --all
24

We can also change the CPU governors of all the cores at once using cpu*. Let’s set them to powersave:

$ echo powersave | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
powersave

The tee command reads from the standard input and writes to the standard output and the specified file(s). Therefore, it reads powersave from the standard input, printed by the echo powersave command. Consequently, it writes powersave to the standard output and the scaling_governor file corresponding to each core. We need root privileges to update each scaling_governor file, so we use the tee command together with the sudo command.

Let’s check the CPU governors of all cores once more:

$ cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
powersave
...
powersave

We succeeded in updating the CPU governors as expected.

3. Using cpupower frequency-set

Another way to change the CPU governors of all cores at once is by using the cpupower frequency-set command. This command is a member of the cpupower tools collection which gets and sets the power-related parameters of a CPU.

We need to install the linux-cpupower package on Debian to use the cpupower frequency-set command. The version we’ve used for this tutorial is 5.10.223-1.

Let’s first see how to change the CPU governor of a single core using cpupower frequency-set:

$ sudo cpupower -c 0 frequency-set -g powersave
Setting cpu: 0

We set the first CPU’s governor to powersave. The -c option specifies the core while the -g option specifies the CPU governor. We use the sudo command since running cpupower frequency-set needs root privileges.

Therefore, it’s possible to set the CPU governors of all cores one after the other in a for loop. However, instead of using a for loop, we can specify multiple cores as a group:

$ n=$(expr $(nproc --all) - 1)
$ sudo cpupower -c 0-$n frequency-set -g powersave
Setting cpu: 0
Setting cpu: 1
...
Setting cpu: 23

The n=$(expr $(nproc –all) – 1) command sets the n variable to 23 in our case. The -c 0-$n option specifies all the cores from 0 to 23 inclusively.

It’s also possible to pass a specific value, namely all, to the -c option to change the CPU governors of all cores at once:

$ sudo cpupower -c all frequency-set -g powersave
Setting cpu: 0
Setting cpu: 1
...
Setting cpu: 23

As is apparent from both outputs, the CPU governors of all cores were updated one after the other thanks to the -c 0-$n and -c all options.

Alternatively, we can omit the -c option altogether to apply the change to all CPU governors by default:

$ sudo cpupower frequency-set -g powersave
Setting cpu: 0
Setting cpu: 1
...
Setting cpu: 23

Let’s check the CPU governors of all cores once more:

$ cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
powersave
...
powersave

We succeeded in updating the CPU governors as expected.

4. Conclusion

In this article, we discussed how to set the CPU governor for all cores at once.

First, we learned that we could use the sysfs file system. We saw that there’s a subdirectory specific to each core in the sysfs file system. Additionally, there’s a file, scaling_governor, within each subdirectory. We used the * wildcard to access these files and change the value of the CPU governors within them.

Then, we learned that using the cpupower frequency-set command was another alternative. We can use its -c and -g options to update the CPU governor for all cores at once or for any specific number of cores.