How can I blank the screen from the command line over SSH?

5,694

Solution 1

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.

Solution 2

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.

Solution 3

Simply run:

gnome-screensaver-command -a

Note: This will also call the lockscreen unless you disable the "Lock" option in system settings:

enter image description here

Share:
5,694

Related videos on Youtube

JPX
Author by

JPX

Updated on September 18, 2022

Comments

  • JPX
    JPX over 1 year

    I need a way to show black screen by running command in console by using SSH connection. Monitor should not go to standby I just need a black screen to hide everything that is on the screen. Screen should be black until I give another command to show screen content.

    Addition: It would be good if the command worked in Ubuntu, Lubuntu and Xubuntu.

    Addition 2: I have also a projector that needs blanking. I want to use SSH to connect to my server and show blank screen. If I try to power off the projector "No signal" is shown on the screen.

  • Jacob Vlijm
    Jacob Vlijm over 7 years
    This will also call the lock screen.
  • You'reAGitForNotUsingGit
    You'reAGitForNotUsingGit over 7 years
    But wouldn't you have to blindly type the command to bring it back?
  • You'reAGitForNotUsingGit
    You'reAGitForNotUsingGit over 7 years
    @JacobVlijm - It doesn't for me.
  • Jacob Vlijm
    Jacob Vlijm over 7 years
    @ AndroidDev That is weird, Ubuntu 16.04 here.
  • Jacob Vlijm
    Jacob Vlijm over 7 years
    @AndroidDev I assume that is scripted.
  • JPX
    JPX over 7 years
    I use SSH connection and I need to set black screen to the monitor.
  • Byte Commander
    Byte Commander over 7 years
    It depends on how you have configured "System Settings → Brightness & Lock". The setting "Lock ON/OFF" and "Lock screen after ..." control the behaviour.
  • You'reAGitForNotUsingGit
    You'reAGitForNotUsingGit over 7 years
    @JacobVlijm - Edited
  • Jacob Vlijm
    Jacob Vlijm over 7 years
    @JPX do you mean the remote screen?
  • JPX
    JPX over 7 years
    @JacobVlijm Yes
  • JPX
    JPX over 7 years
    xrandr prints "Can't open display".
  • JPX
    JPX over 7 years
    testuser@media:~$ gnome-screensaver-command -a ** Message: Failed to get session bus: Cannot autolaunch D-Bus without X11 $DISPLAY
  • Jacob Vlijm
    Jacob Vlijm over 7 years
    @JPX Sure, that's the missing $DISPLAY variable on ssh (has nothing to with your screen). ssh is not my speciality, will try to find out how to solve. You might want to add that to the question.
  • Jacob Vlijm
    Jacob Vlijm over 7 years
    @JPX Hi JPX see my edited answer.
  • JPX
    JPX over 7 years
    @JacobVlijm It seems that 'DISPLAY=:0 xrandr' and 'DISPLAY=:0 xrandr --output HDMI1 --brightness 0' are working. Thanks!
  • Terrance
    Terrance over 7 years
    @JPX This command would work too: xrandr -d :0 --output HDMI1 --brightness 0 without the need to set DISPLAY every time.
  • Jacob Vlijm
    Jacob Vlijm over 7 years
    @JPX ah, great! Glad it works :)
  • Terrance
    Terrance over 7 years
    You're welcome! Good answer by the way, this will come in handy when I am working from home but need my screens blanked. =)
  • JPX
    JPX over 7 years
    So "xrandr" -> "xrandr -d :0" and the Python script will work.
  • Jacob Vlijm
    Jacob Vlijm over 7 years
    @JPX Someone correct me if I am wrong, but I believe DISPLAY=:0 python3 /path/to/script.py black should work on the script as it is.
  • Terrance
    Terrance over 7 years
    Yep, it works fine that way as well. You don't need the call to python3 before the script, as it is called within your script. =)
  • Terrance
    Terrance over 7 years
    But I set my script to +x
  • JPX
    JPX over 7 years
    So DISPLAY=:0 ./script.py black
  • Jacob Vlijm
    Jacob Vlijm over 7 years
    @JPX I'd use the full path, since we don't know where we are :)
  • David Foerster
    David Foerster over 7 years
    A better option to restore the previous session it to use a terminal multiplexer like tmux or screen and detach the session, lock the console and then reattach to the previous session. Bonus: you can log out or disconnect entirely and still resume the session later.