13

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.

1

4 Answers 4

17

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.

18
  • But wouldn't you have to blindly type the command to bring it back? Jan 4, 2017 at 17:06
  • @AndroidDev I assume that is scripted. Jan 4, 2017 at 17:07
  • 2
    @JacobVlijm It seems that 'DISPLAY=:0 xrandr' and 'DISPLAY=:0 xrandr --output HDMI1 --brightness 0' are working. Thanks!
    – JPX
    Jan 4, 2017 at 18:09
  • 1
    But I set my script to +x
    – Terrance
    Jan 4, 2017 at 18:45
  • 1
    @JPX I'd use the full path, since we don't know where we are :) Jan 4, 2017 at 18:50
3

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.

1
  • 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. Jan 5, 2017 at 11:51
2

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

6
  • This will also call the lock screen. Jan 4, 2017 at 17:03
  • @JacobVlijm - It doesn't for me. Jan 4, 2017 at 17:06
  • @ AndroidDev That is weird, Ubuntu 16.04 here. Jan 4, 2017 at 17:07
  • It depends on how you have configured "System Settings → Brightness & Lock". The setting "Lock ON/OFF" and "Lock screen after ..." control the behaviour.
    – Byte Commander
    Jan 4, 2017 at 17:09
  • @JacobVlijm - Edited Jan 4, 2017 at 17:11
0

Maybe this answer in superuser will help too: https://superuser.com/a/374661/958509
It works on local terminal but I'm not sure about over ssh. (at least on Ubuntu 20 it doesn't need install new package to work)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.