None of the above answers worked for me using the nvidia-settings GUI. Here's the bash commands that enabled me to change my fan speed from the cli, when ssh tunneled into my desktop. (Note: I have SDDM (Simple Desktop Display Manager) rather than GDM (Gnome Desktop Manager) installed).
Get the Xauthority for the current Xserver.
(Note: you may need to plug in the monitor first and turn it on, otherwise, there's a super interesting workaround option to run a new X server (e.g., X :0 && export DISPLAY=:0
avialable on the nvidia forums).
I didn't need that since I can just plug a monitor (powered off) into the HDMI outlet. Here's the simpler version:
ps -ax | grep Xorg
Find the path to the Authorization File string that looks like
-auth /var/run/sddm/{31a6ce92-51d1-4cef-b30e-61d63b0221bb}
and copy the path (i.e., /var/run/sddm/{Authorization File}
). (Don't use the dummy example here, it won't work - you need to find your Auth File path with the command above).
Adjust your Nvidia GPU fanspeed
Now we're ready to put the full command together - to set GPU 0 with a speed of 95% it'll look like this (remember to replace the Auth File with the path you copied above):
sudo DISPLAY=:0 XAUTHORITY=/var/run/sddm/{31a6ce92-22d1-4cef-b29e-91d63b0221bb} nvidia-settings -a [gpu:0]/GPUFanControlState=1 -a [fan:0]/GPUTargetFanSpeed=95
Change the values to what you need. Check it worked by running this
nvidia-smi
Wrapping it all into a python script
If you're lazy like me and just want a python script to do all this - you can paste the following into a file (e.g., a new file you create callsed fanspeed.py), then run python fanspeed.py
.
#!/usr/bin/python
import subprocess
import re
# Run 'ps' command and capture the output
ps_process = subprocess.Popen(['ps', '-ax'], stdout=subprocess.PIPE)
ps_output = ps_process.communicate()[0].decode('utf-8')
# Search for the string that looks like '/usr/lib/xorg/Xorg -nolisten tcp -auth /var/run/sddm/{c88c6851-f0c3-48f8-9689-814f25e12450} -background none -noreset -displayfd 17 -seat seat0 vt1'
xauth_line = None
for line in ps_output.split('\n'):
if '-auth /var/run/sddm/{' in line:
xauth_line = line
print("Found Xauth line " + xauth_line)
break
# Extract the '-auth' value
xauth_value = None
if xauth_line:
match = re.search(r'-auth\s+(.+?})', xauth_line)
#match = re.search(r'-auth\s+(.+)\s+', xauth_line)
if match:
xauth_value = match.group(1)
print("Found Xauth value:" + xauth_value)
# Adjust fan speed
if xauth_value:
fan_speed_command = f'sudo DISPLAY=:0 XAUTHORITY={xauth_value} nvidia-settings -a [gpu:0]/GPUFanControlState=1 -a [fan:0]/GPUTargetFanSpeed=95'
print("Issuing command to adjust fanspeed"+fan_speed_command)
subprocess.run(fan_speed_command, shell=True)
# Check if it worked by running 'nvidia-smi' command
nvidia_smi_process = subprocess.run(['nvidia-smi'], stdout=subprocess.PIPE)
nvidia_smi_output = nvidia_smi_process.stdout.decode('utf-8')
print(nvidia_smi_output)