How can I blank the screen from the command line over SSH?
Short answer:
xrandr --output DVI-I-1 --brightness 0
where DVI-I-1
is your screen's name.
The other way around:
xrandr --output DVI-I-1 --brightness 1
to set to normal brightness again.
To get the screen's name
Simply run the command:
xrandr
In the output, you will find the screen's name, in the line, including connected
Small script to either darken the screen or set it back to normal
The script can be used to set (all) connected screen(s) to black and vice versa. The script finds your screens automatically.
#!/usr/bin/env python3
import subprocess
import sys
arg = sys.argv[1]
screens = [l.split()[0] for l in subprocess.check_output("xrandr").decode("utf-8").splitlines()
if " connected" in l]
val = "0" if arg == "black" else "1"
for s in screens:
subprocess.Popen(["xrandr", "--output", s, "--brightness", val])
To run
- Copy the script into an empty file, save it as
set_black.py
Run it either with:
python3 /path/to/set_black.py black
to darken the screen, or
python3 /path/to/set_black.py normal
to set brightness to normal again.
While the answer above should run fine on all Ubuntu distro's locally, the question turns out to be on ssh/remote (the information was edited into the question).
In case of a remote situation, we'd need to set the $DISPLAY
variable correctly. If the display variable is e.g. :0
, we'd need to run the script with:
DISPLAY=:0 python3 /path/to/script.py black
The variable is not necessarily :0
though. This post on U&L seems an excellent one set the DISPLAY
variable on the remote machine.
Edit: The question has been changed since I had provided this answer. I will let this answer stand because it provides some information which may be of use.
If by console you mean one of the character-cell virtual consoles, then install vlock
:
sudo apt-get -y install vlock
You can then black out and lock your virtual console:
vlock
When unlocking the console the screen is not restored. If you don't want to lose the contents of the screen, the I suggest to login to a second console; when you want to lock the screen move to that second console and enter
vlock -a
This will lock all consoles and prevent console switching. After unlocking the consoles you can switch back to the main work console and find your screen contents intact.
Simply run:
gnome-screensaver-command -a
Note: This will also call the lockscreen unless you disable the "Lock" option in system settings: