Categories
Howto

How to control Nvidia graphics card Fan Speed Automatically in Linux

In linux, the fan speed is not controlled by default and the card itself auto adjusts the fan speed; in this tutorial I’m going to teach a way to Automatically and Manually control the fan speed of an Nvidia graphics card on Linux with proprietary drivers at Boot and Reboot.

Table of contents:
1. Controlling Fan Speed manually (Mandatory step)
2. Controlling Fan Speed Automatically at boot with a script
3. Controlling Fan Speed Automatically at boot with the Green With Envy GUI
4. Controlling Fan Speed Headlessly

1.Controlling fan speed Manually (Mandatory configuration changes)

Nvidia doesn’t enable this feature by default and it’s on the user to enable it using the command line.
Back in the days, it used to be nvclock but the support was dropped in 2010. Here’s a short guide on how to control the fan using Nvidia X Server Settings.

The ‘Nvidia X server Settings’ application is installed along with the proprietary driver.The following command needs to be executed in Terminal to enable fan control in Nvidia X server Settings:

sudo nvidia-xconfig -a --cool-bits=28 --allow-empty-initial-configuration

Note that this might not work on some distributions and you may be required to edit /etc/xorg.conf.d/20-nvidia.conf and add the following lines:

Section "Device"
        # You may change the identifier number to your use case.
        Identifier      "Device0"
        Driver         "nvidia"
        BoardName      "GeForce GTX"
        VendorName     "NVIDIA Corporation"
        Option         "Coolbits" "28"
EndSection

Note: You may want to change the Coolbits value to your own needs. The reference sheet is available at http://download.nvidia.com/XFree86/Linux-… But it’s recommended to keep it at 28 to enable all the features.

After this, a reboot or a logout is required to apply the changes.

After the reboot/login, open the NVIDIA X Server Settings application 
nvidia xserver powermizer

Go to the ‘Thermal Settings’ section and you can control the fan speed by your own hands:
controlling fan speed in nvidia xserver settings

2.Controlling fan speed Automatically at boot with a script (Step 1 required)


Now this was only controlling it manually through the nvidia-settings software. These changes will not apply at boot and do not auto adjust; but the changes were required in order to enable fan control.
I found a little program on github to control the fan speed with a fan curve:

Download the program from github with this command:

cd ~/
git clone https://github.com/nan0s7/nfancurve

after that, cd to the directory which it downloaded in:

cd nfancurve

now we see a few files:

config
LICENCE
nfancurve.service
README.md
temp.sh
USAGE.md

The only file which needs to be modified is the config file; inside this file, fan curve and some other options can be set; here are some of the important ones:

min_t="25" # This is the temperature which any number below it will cause the fan speed to drop to 0
fcurve = "30 37 45 55 57" # Fan Speeds

tcurve = "45 55 65 75 85" # Temperatures

Now in this example; the fan speed would be 30% at the temperature of 45 Celsius, 37% at 55 Celsius and so on. after changing the values to your preference, save the changes.
The fan speed is controlled by file temp.sh. First make the script, executable with the command below:

chmod +x ./temp.sh

Then run the script:

./temp.sh

If everything is working fine, the fan speed will adjust and you’ll see the changes on your terminal. Now stop the script by using CTRL+C.

Now if I want the script to run at reboot without being bothered; it can be added to the startup applications manager of the DE(kde has ~/.config/autostart-scripts/) or add it to ~/.xprofile. The latter is highly recommended since it will start whenever your X server has started and every distribution supports it.

If you want to add it to your Desktop Environment’s startup application manager; just simply search it and add the temp.sh script wherever you saved it. Every Desktop Environment is different and the process is super easy; so I’m not including it in this tutorial. You can search for it for your own Desktop Environment.

To add this script to~/.xprofileand make it executable; do as follows:

echo '~/nfancurve/temp.sh &' >> ~/.xprofile
chmod +x ~/.xprofile

Or just edit it with your own text editor. Note that this file might not exist in some cases; but it will execute anyways.

Save the file and wallah! Now the script will run and auto adjust the fan speed by the curve set at boot.

3.Controlling fan speed Automatically at boot with the Green With Envy GUI

There is a nice GUI to properly control Nvidia graphics cards in linux called Green With Envy or GWE for short.

It’s not available in ubuntu’s official repos as of now but it’s available on flatpak. Setup flatpak using this link: https://flatpak.org/setup/.

After setting up flatpak; install GWE with the following commands:

flatpak install flathub com.leinardi.gwe

After that, you can run it with the following command:

flatpak run com.leinardi.gwe

Working with the GUI is pretty easy and straightforward(Hell, that’s why I introduced it) and doesn’t require explanation.

This program can be added to startup by adding the above to ~/.xprofile; To do so, execute the following:

echo 'flatpak run com.leinardi.gwe --hide-window &' >> ~/.xprofile

By default, this program cannot add itself to startup but the above will work around that.

4. Controlling Fan Speed Headlessly

In order to control an nvidia graphics card’s fan speed headlessly; an X server needs to be started. It can be the minimum. Just to get X initialized; then nvidia-settings will work and so will tools such as nfancurve.

There is a great tool to do that automatically by starting an X server for each gpu: https://github.com/andyljones/coolgpus

Apparently nvidia unlike amd doesn’t provide hwmon in sysfs in order to control the fans. 

32 replies on “How to control Nvidia graphics card Fan Speed Automatically in Linux”

There is a free tool ‘Green With Envy’ for nvidia cards. Using this software you can easy set up a custom fan curve.

Script says ‘min_t2 is greater than the first value in the tcurve2!’
This is not true, as t2 is set to 20, and tcurve2 is set to 35.
 
I would like to ask to add power limiting to the script as well.
Eg:
When temperature gets to 80c, limit power to 160W.
If it gets to 84C, limit power to 130W; through the command:

sudo nvidia-smi -pl 130

 
Third, some people have 2 GPUs.
The above command can be modified by doing:

sudo nvidia-smi -i 0 pl 130
sudo nvidia-smi -i 1 pl 145

In case one wants to modify the power curve separately between GPUs.
To bypass the script from needing user interaction on the sudo command, you could put sudo password in config, so it will automatically fill out the password value.
Remember that passwords can hold spaces, dots, and other characters…

I just realized that nfancurve changed how it interprets the fan curves and temperatures (fcurve and tcurve) from arrays(using parentheses for array literals ) to strings; Updated the article; Test it now. If the problem still persists; you could comment out min_t2 , fcurve2 and tcurve2 and modify fan2gpu and which_curve to bind the fans. Thanks for the contribution for power limiting. Will leave it here. Someone might find it useful. You could also fork the project on github and link your fork here.

Well, You should edit the file with respect to your machine.
Maybe you can give me the contents of /var/log/Xorg.0.log and maybe I can help you with the errors it’ll give.
You can do that by switching to another terminal with Ctrl+Alt+f2 or f3 or … and login.

Hey countercookie,

I managed to solve it (hopefully.. Will reboot and test it out again later tonight as I am remoting and have no access to the pc in case anything happens).

Just FYI, I am running dual GPU setup on my PC. Both RTX2080Ti. When I ran the set of codes, no Xorg.conf was found, so it was created for me manually. It had problems cause it just gave me a blank screen. When I switch to another terminal, it hangs too, and does not allow me to access anything. At first, I took the long path and reinstalled Ubuntu. But after that, what I did was that I deleted Xorg.conf through recovery and then it allowed me to boot back into Ubuntu normally.

So I created the file again but with cool-bits = 4. After that, I deleted all the info in Xorg.conf but only left these (added cool-bits myself).The generated config had other info like Screen, Keyboard etc. which I deleted.

Section "Device"
    Identifier     "Device0"
    Driver         "nvidia"
    VendorName     "NVIDIA Corporation"
    BoardName      "GeForce RTX 2080 Ti"
    BusID          "PCI:10:0:0"
    Option         "Coolbits" "4"
EndSection


Section "Device"
    Identifier     "Device1"
    Driver         "nvidia"
    VendorName     "NVIDIA Corporation"
    BoardName      "GeForce RTX 2080 Ti"
    BusID          "PCI:66:0:0"
    Option         "Coolbits" "4"
EndSection


After that I am able to control the fan speed manually (which I intended to do through the help of your guide) and I restarted the pc to find it working as usual.

Do you think that there’s something I might have mistakenly done wrong?

Hey,

Glad it’s working for now. When you install the nvidia drivers, at first it always creates a /etc/X11/xorg.conf for you which has fundamental configuration to get the display to show such as Screen entry. You can always regenerate this with the nvidia-xconfig command.

All you need to do is enter those device sections (Section Device) into the /etc/X11/xorg.conf.d/20-nvidia.conf config file and that’ll work a treat!

I tested with Ubuntu and Arch that entering your device sections in the xorg.conf.d directory is better and applies all the settings.

In your case, it’s a bit weird. I suggest you get physical access to the machine and try all these. Also you might be having a problem with multiple gpus and your xorg config may not be correctly saying which gpu the monitor(Screen, Display) is connected to. Just spit balling since there can be so much wrong. definitely try fiddling with nvidia-xconfig for initial configuration.

Hi,

You did mention that I would have to enter those device sections into /etc/xorg.conf.d/20-nvidia.conf but I do not seem to have that folder. I am very confused too as there are quite a few number of things such as xorg.con.d missing for my PC. Do you mind helping me out on this?

Sorry about all the problems I’ve caused. I meant /etc/X11/xorg.conf.d .
Post your /var/log/Xorg.0.log whenever it has problems showing something. We may tell a lot just from that.

Hey hey, no need to apologise. I’m learning a lot from your post, and just by having a conversation with you I’m learning much more. Thank you for that!

I’ve listened to your advice and I generated a new Xorg.conf by using the nvidia-xconfig command. I added cool-bits = 4 and everything works fine now. I am able to control both the GPU fans.Thank you for helping out on this.

Noted on the log. So far, all is well, and there are no errors! Again, thank you so much for the help, countercookie. Appreciate it a lot.

Your post is really really helpful. Take care! 🙂

You’re very welcome. Glad it’s all working now but if it messes up again, don’t hesitate to post the log here; I’ll reply as quickly as I can.

Thanks so much hombre! It really helped me in my Ubunru 20 with my RTX2080. I allways forgot to start the fans manualy at the start of the session and the temp were 50-55 while coding… Now it’s history :))
Tnx!

I’m running Ubuntu Focal (i.e. 20.04) on an Aorus B450 Pro Wifi motherboard with an AMD Ryzen 3 3100-U CPU & an Asus NVIDIA GeForce GT710 graphics card, as stated in the NVIDIA X Server Settings app. I’ve tried the ‘Controlling fan speed Automatically at boot with a script’ method. However, nfancurve (running temp.sh using the default config file) shows “No fans detected”. Any ideas?

Hi, First make sure that nvidia-smi and the NVIDIA X Server Settings show your fan. Have you gone through the first step of setting the cool-bits ? I included another method of creating 20-nvidia.conf since that seems to work better. A reboot is preferred after such changes.

Thanks for suggestions. I didn’t do the cool-bits stuff in method 1 (Controlling fan speed Manually) because I wanted to use method 2 (Controlling fan speed Automatically at boot with a script) and didn’t realise that I had to do method 1 first. I’ve tried the ‘cool bits’ command in method 1 and it gave:

sudo nvidia-xconfig -a –cool-bits=28 –allow-empty-initial-configuration

[sudo] password for john: 

WARNING: Unable to locate/open X configuration file.

Package xorg-server was not found in the pkg-config search path.

Perhaps you should add the directory containing `xorg-server.pc’

to the PKG_CONFIG_PATH environment variable

No package ‘xorg-server’ found

Option “AllowEmptyInitialConfiguration” “True” added to Screen “Screen0”.

New X configuration file written to ‘/etc/X11/xorg.conf’

There was no /etc/X11/xorg.conf file before doing that. There is no /etc/xorg.conf.d/20-nvidia.conf file. In fact, there is no /etc/xorg.conf.d/ directory. Should I create (using sudo) that directory & its file 20-nvidia.conf? Should I follow the cryptic instructions above for the PKG_CONFIG_PATH environment variable (that variable not set currently)?

I rebooted and I still don’t see anything about a fan in Thermal Settings of NVIDIA X Server Settings: screenshot attached. Terminal command nvidia-smi seems to show that fan is off though I can hear it:

john@johnpc:~$ nvidia-smi

Sun Nov 15 07:20:23 2020   

+—————————————————————————–+

| NVIDIA-SMI 450.80.02 Driver Version: 450.80.02 CUDA Version: 11.0  |

|——————————-+———————-+———————-+

| GPU Name  Persistence-M| Bus-Id  Disp.A | Volatile Uncorr. ECC |

| Fan Temp Perf Pwr:Usage/Cap|   Memory-Usage | GPU-Util Compute M. |

|        |      |    MIG M. |

|===============================+======================+======================|

| 0 GeForce GT 710  Off | 00000000:09:00.0 N/A |     N/A |

| N/A 37C P0 N/A / N/A | 747MiB / 978MiB |  N/A  Default |

|        |      |     N/A |

+——————————-+———————-+———————-+

                     

+—————————————————————————–+

| Processes:                 |

| GPU GI CI  PID Type Process name     GPU Memory |

|  ID ID             Usage  |

|=============================================================================|

| No running processes found             |

+—————————————————————————–+

john@johnpc:~$ 

PS I looked at /var/log/Xorg.0.log and it’s 68k. I haven’t provided an image as I presumed that it wouldn’t be of any help and I didn’t see a method of attaching a file. It has some worrying messages near its end e.g. event8 – USB 2.0 PC Camera: PC Camera: device removed

As I see from the errors, it says that you don’t have xorg even installed which doesn’t make any sense; I suppose that tool looks for deprecated environment variables but those errors shouldn’t bother the tool just writing fan. And yes you can make the xorg.conf.d directory yourself and add the 20-nvid… file yourself, no problem.

I also tried searching for your graphics card “Asus NVIDIA GeForce GT 710” and there doesn’t seem to be a version with fan. I’m confused about what fan you’re hearing. Please elaborate.

You don’t need to worry about that last line of the log; the log contains a lot of normal events.You could attach the file with pastebin.

Apologies for wasting your time. My grandson built this computer and he’s not very communicative. There are 2 fans: one mounted on top of the CPU’s heat sink & the other attached to the motherboard/case (not sure which) but near the graphics card. I mistakenly thought that the second was attached to the graphics card.
The BIOS has allowed me to set fan speeds according to temperature. So I’ve done that for the second fan (called SYS_FAN1 by the BIOS) so as to have it off until the graphics card reaches 50 degrees.
I clean installed Ubuntu 20.04 on this computer a month ago. I have the feeling that Canonical are moving away from xorg, perhaps to Mir or Wayland.

Hey, newbie to Linux here. Thanks very much for the tutorial.

This is probably a stupid question, but will this also work with EVGA cards? I know that in Windows you need to use their X1 software in order to get all three fans spinning, and with my fresh install of Garuda Linux I only have one fan running.

This method will probably work on every card as long as it’s an Nvidia running with proprietary drivers. But EVGA could be using a specific solution to control the 3 fans separately. This is probably not overlooked by them and the fans will get controlled by the bios in the worst case scenario to prevent overheating.

Having only that one fan running probably is the lowest mode for cooling the card with no noise and it’ll probably ramp up during loads. Try testing the card with a benchmark.

hi, i did every step but it looks like xprofile does not start at boot (and yes, i’ve gave it exec permissions)

Hi, I can’t keep with every change ubuntu or every other distribution or DE makes. The SDDM login manager runs .xprofile after the X server has started which is what we want. Try searching in your DE’s for startup programs and add the script there. Tell me your result.

sudo nvidia-xconfig -a --cool-bits=28 --allow-empty-initial-configuration

use this command with care! After executing it and rebooting, i got stuck at blank screen. This is a guide for anyone with the same issue. ALSO keep in mind that im a beginner and just sharing what worked for me.

  1. THIS STEP IS NEEDED ONLY IF YOU HAVE SPLASH SCREEN ENABLED. if you have splash screen enabled, you need to disable it. To do this, press shift (if that doesnt work try esc or space) at boot when manufacturers logo is shown. In case of dualbooting, press shift right after you picked linux. now you should see the name of your distro. Press e. Remove “splash” from the end of the command
  2. press ctrl + alt + f2. You should get prompted with login. Enter login and password
  3. now youre in the terminal. Make sure you type the next command correctly. Type: sudo rm /etc/X11/xorg.conf This removes the troublemaking file and upon reboot its contents will be changed back to default.

My english and explaining is pretty bad so if you have any questions, ask me

Thanks, but doesn’t the nvidia-xconfig command write the configuration into /etc/X11/xorg.conf? doesn’t deleting the file just reverse what the command did? Could you elaborate by providing your X log file after the blank screen? it’ll be either /var/log/Xorg.0.log or ~/.local/share/xorg/Xorg.0.log. Thank you.

Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.

Leave a Reply

Your email address will not be published. Required fields are marked *

captcha