128

I am runnning Ubuntu Server on my laptop. There is really no reason for the screen to be on. I have been trying to use this command to turn the screen off:

sleep 1 && xset dpms force off

The problem is I get the following error

unable to open display "".

Any idea what is going on / what is wrong? Any other suggestions for how to do this?

enzotib
  • 96,253
AngryBird
  • 1,281

12 Answers12

86

To turn off monitor in console, the command is the following:

sudo vbetool dpms off

To regain control of the console on pressing Enter key, I suggest

sudo sh -c 'vbetool dpms off; read ans; vbetool dpms on'
enzotib
  • 96,253
61

Try these commands...

To Turn Off:

xset -display :0.0 dpms force off 

To Turn On:

xset -display :0.0 dpms force on 

If your display turns off and then immediately back on then try the following which adds a delay of 1 second before turning the screen off. This give a chance for all events to be processed by the X server before turning the display off.

sleep 1 && xset -display :0.0 dpms force off 
Eliah Kagan
  • 119,820
  • How do I know which screen is which – Karl Morrison Jul 30 '17 at 15:26
  • 1
    The following worked for me (also partially inspired by @enzotib's answer): xset -display $DISPLAY dpms force off; read temp; xset -display $DISPLAY dpms force on – stiemannkj1 Aug 04 '17 at 02:52
  • 1
    Be careful with this. I'm unable to turn my monitor back on with a test machine after issuing force off – Zmart Oct 31 '17 at 17:02
  • 5
    Update: Pulling out and replugging all cables (including power) on the monitor has remedied the situation. – Zmart Oct 31 '17 at 17:08
  • 2
    Ubuntu 20.04 LTS does not work: server does not have extension for dpms option. Blanking via keyboard shortcut (Fn+F6 in my case) works (a bit unreliable, releasing Fn often unblanks screen again). But I need a solution which works via ssh when the Laptop's keyboard is out of reach. (It's really annoying if you wake up in bed because the Laptop lights the room. Just a short voice command could invoke ssh to stop the Laptop burning! Note that I disable screensavers on consoles, as there is only one thing which is even more annoying than wrong time console blanking: CapsLock!) – Tino Feb 09 '21 at 18:51
54

The xset command mentioned in other answers will turn off all the monitors if you have multiple monitor setup. However if you want turn off only one or some of the monitor you should use xrandr.

First run xrandr -q command, which will list all the monitors and its current display settings. At the start of each monitor details you will find monitor name set by the system. You have to keep track of that.

I have two monitors (one is connected to DVI port and other to VGA port) and when I run the command I see this:

Screen 0: minimum 320 x 200, current 1360 x 768, maximum 2726 x 2726
DFP1 connected 1360x768+0+0 (normal left inverted right x axis y axis) 304mm x 228mm
   1360x768       59.7*+
   ... (more details are shown here, but I have hidden it)

CRT1 connected 1360x768+0+0 (normal left inverted right x axis y axis) 410mm x 230mm
   1360x768       59.8*+   60.0  
   ... (more details are shown here, but I have hidden it)

In this output, DVI port connected monitor is labelled as DFP1 and other one as CRT1. So if I want turn off the secondary monitor(i.e., CRT1) I have run this command:

xrandr --output CRT1 --off

if you want switch on that monitor again then you have run this command:

xrandr --output CRT1 --auto
  • I have 3 Monitors, 1 VGA, 1 DVI and 1 HDMI. When i ran: xrandr --output VGA1 --auto, it turned the VGA on, but it cloned the DVI monitor. – ThiagoPonte Feb 23 '15 at 00:43
  • Hmm strange. Try --on instead of --auto – Harshith J.V. Feb 23 '15 at 02:58
  • @HarshithJ.V. this is a good answer! Its better than the xset because xset turns on if you just press the touchpad etc. Btw hope you remember me from your previous company :-) – Nishant Nov 13 '16 at 18:05
  • @Nishant Thanks for compliments. Strangely I don't remember you. Please ping me on any other social media as we can't chat over as its against the rules. – Harshith J.V. Apr 14 '17 at 05:00
  • If you got crtc error, then type "chvt 7" before xrandr command, so that it reads "chvt 7 && xrandr -d :0 --output eDP1 --auto" – haytham-med Nov 11 '17 at 06:36
  • To save xrandr settings even after relogging, so that you won't get a blank screen, you need to go to display settings in kde then set the primary output in size and orientation tab. – haytham-med Nov 11 '17 at 06:42
  • 3
    thank you for your answer, I would only add that (as in the case of @ThiagoPonte) xrandr --output DP-5 --auto --right-of CRT1 allows to turn back on the monitor without cloning (replace --right-of with whatever your setup is) – Louis Gagnon Jan 08 '20 at 08:41
  • X Error of failed request: BadMatch (invalid parameter attributes) – france1 Jul 20 '21 at 18:50
45

I've just installed Ubuntu Server 18.04.1 (no GUI, X, nor anything graphical), and after breaking my back last night trying to turn off the screen I finally found the magic command:

setterm --blank 1

After executing the command the screen will turn off automatically every minute (if idle).


And even better, if you want the command to be executed automatically at boot, you can add it to the GRUB commandline, to do so we have to edit the next file:

sudo nano /etc/default/grub

Once there, just add consoleblank=60 to GRUB_CMDLINE_DEFAULT, it should look like this:

GRUB_CMDLINE_LINUX_DEFAULT="quiet consoleblank=60"

Then close the file and save it, after that just run sudo update-grub and voila, every time you boot the screen will turn off automatically every 60 sec. (again, if idle).

And this way (adding the consoleblank to the GRUB) works even from remote terminals (ssh).

Enjoy! (again)

AxeAR
  • 548
  • 2
    Executing the above command I get: "setterm: terminal xterm-256color does not support --blank". What can be wrong? – zx485 Sep 19 '18 at 21:23
  • 2
    Are you trying from a remote session? – AxeAR Sep 20 '18 at 01:41
  • 1
    @zx485 Check out the updated answer, now you can make the screen turn off from a remote session. I'm sure this will solve your problem – AxeAR Jun 08 '19 at 20:34
  • 1
    Just what I was looking for! This is usually included by default in most distros but not server-centric ones like proxmox. Works like a charm. – pinkeen Apr 28 '20 at 16:57
  • 4
    You can do this remotely via setterm -term linux -blank < /dev/tty1. The command who shows which tty's users are currently logged into. You can also "turn off the screen" so it doesn't react to keys via setterm -blank force and reverse it with setterm -blank poke. – pinkeen Apr 28 '20 at 17:23
  • 2
    Caution! The value passed to setterm --blank is minutes not seconds. I was scratching my head for a while until decided to read TFM. – pinkeen Apr 28 '20 at 17:25
  • Can confirm this worked well on Ubuntu 22.04 server with no X or GUI. Both the local and remote session solutions worked well. Thank you! – Deekshith Apr 07 '23 at 14:55
  • This works great on my 2011 21.5" iMac running Ubuntu Server 24.04 LTS. However, the LCD backlight stays on when the screen blanks and uses a lot of power. – Langdon Jun 13 '24 at 15:50
26

Blank the screen after 1 min and turn it off after 2 min. Any keypress will turn it back on. Make it auto-start after reboot.

You can try it out with:

setterm --blank 1 --powerdown 2

If you get the error: setterm: terminal xterm-256color does not support --blank

  • You are probably trying to use this command via SSH. You must run it locally from the machine, or do the next stage of this guide.

Make it auto-start

Insert the command into an executable file. Store it for example in the directory /usr/local/bin/screen-off.sh

#!/bin/bash
setterm --blank 1 --powerdown 2

And to make the script file executable by systemctl. Create file /etc/systemd/system/screen-off.service

[Unit]
Description=Blank screen after 1 min and turn it off after 2 min. Any keypress will turn it back on.
After=ssh.service

[Service] Type=oneshot Environment=TERM=linux StandardOutput=tty TTYPath=/dev/console ExecStart=/usr/local/bin/screen-off.sh

[Install] WantedBy=local.target

Make it executable:

sudo chmod +x /usr/local/bin/screen-off.sh

And finally, enable it for boot:

sudo systemctl enable screen-off.service

Optionally, temporarily start the service right away:

sudo systemctl start screen-off.service

To disable it:

sudo systemctl disable screen-off.service
Carolus
  • 600
  • Why does it need to start after ssh.service? – philosophie Dec 15 '19 at 14:44
  • no effect on macbook pro – Drew Feb 05 '20 at 20:44
  • service works perfectly on Ubuntu Linux using systemd; thanks for the fantastic tutorial. This is great for a laptop server. Also @Drew this is "Ask Ubuntu" not "Ask Different" the instructions are for Linux not macOS. – cody.codes Jun 05 '20 at 06:21
  • I guess @Drew is running Ubuntu on his Macbook Pro... setterm --blank 1 --powerdown 2 doesn't seem to work on those machines. – TCB13 Jun 17 '20 at 21:31
  • 1
    Lacking support for xset dpms, running chvt 2 && setsid sh -c 'TERM=linux setterm --blank force <>/dev/tty2' as root via ssh does the trick at my side. However chvt likes to crash X11 such, that, immediately, screen and keyboard and, not immediately, network connections become unavailable. I really have no good idea how Ubuntu 20.04 managed to archive this. Probably NetworkManager hangs in X11, so everything goes south when DHCP expires or something similar toggles something on the network. The machine definitively stays up, but is in it's own lonely Universe afterwards. – Tino Feb 09 '21 at 19:42
  • service files set executable will generate warning, request to set off. – Stephen Boston Jun 29 '21 at 17:17
6

If you have multiple monitors use this:

Turning off your second monitor:
(Assuming CRT-0 is your left monitor and CRT-1 is your right monitor)
xrandr --output CRT-1 --off
Turning on your second monitor:
xrandr --output CRT-1 --right-of CRT-0 --auto
This way xrandr knows not to duplicate the first screen.

  • I was testing this, turning off a screen this way is more like disabling it, eg, the screen is still powered on. Its just not used. The --off'd screen cannot be turned back on with the screen's power button (because its still on / was never off). It also messed with my desktop display settings, (from extended desktop to mirrored displays). If this was done to turn both displays off, I'm not sure it would be easy to turn them back on without logging in to TTY and clearing the monitors.xml file wherever it is. The more you know. – ThorSummoner Dec 12 '16 at 18:02
4

You can set it with setterm remotely, just redirect your command to correct TTY on your laptop screen, usually is /dev/tty1.

Try this:

TERM=linux setterm --blank 1 >/dev/tty1 </dev/tty1
4
  • Suggested way:
xset -display $DISPLAY dpms force off

Move your mouse and your screen will come back

  • Not suggested way:
xrandr --output $(xrandr | grep ' connected' | cut -d' ' -f1) --off

Then you lose your screen until you restart your machine or replug your monitor wire :)

wsdzbm
  • 977
3

@stiemannkj1's answer(inspired by @enzotib's answer) buried in the comments to @harshith-j-v's answer (works on my laptop connected to a monitor):

xset -display $DISPLAY dpms force off; read temp; xset -display $DISPLAY dpms force on

Maybe a mod or site developer can make a way to pull good comments out to a separate answer so that attributions and reputation scoring can be settled correctly

metaforma
  • 31
  • 1
0

I was having the same problem. What I discovered is that on my laptop (an old acer aspire) the default screentoggle button is supported. This could be something like fn+F6.

This solution probably didn't work back in the day. I hope it'll help anyone that encounters this problem.

  • 2
    Ubuntu 20.04 Me Too, but I need something from commandline. On my Acer xset dpms force off does not work. chvt 2 && setsid sh -c 'TERM=linux setterm --blank force <>/dev/tty2' (run as root via ssh) works, though, but often X11 crashes doing so, rendering screen+keyboard dead. In that case, networkmanager seems to be unable to keep network connections alive, so the machine is neither dead nor alive afterwards. All you can do is a long press of the power button. Checkmate. – Tino Feb 09 '21 at 19:28
0

Off topic, but important!

Notice <>/dev/tty1

I see something like setterm ... <>/dev/tty1
in this page more than 2 times.

have to point out that CMD <>/dev/tty1
  is not same with CMD >/dev/tty1 </dev/tty1


The latter does the expected job in this situation.
// change setterm -blank value, and verify by cat /sys/module/kernel/parameters/consoleblank


NOTE: <>FILE not shorthand for >FILE <FILE
   // at least I'm sure in bash.

Prove it

chen:/run/test$ cat ./test.sh 
#!/bin/bash
ls -lh /proc/$$/fd/
chen:/run/test$ ./test.sh <>./out1
total 0
lrwx------ 1 chen chen 64 6月   9 13:37 0 -> /run/test/out1
lrwx------ 1 chen chen 64 6月   9 13:37 1 -> /dev/pts/2
lrwx------ 1 chen chen 64 6月   9 13:37 2 -> /dev/pts/2
lr-x------ 1 chen chen 64 6月   9 13:37 255 -> /run/test/test.sh
chen:/run/test$ ./test.sh >./out2 <./out2
chen:/run/test$ cat ./out2
total 0
lr-x------ 1 chen chen 64 6月   9 13:38 0 -> /run/test/out2
l-wx------ 1 chen chen 64 6月   9 13:38 1 -> /run/test/out2
lrwx------ 1 chen chen 64 6月   9 13:38 2 -> /dev/pts/2
lr-x------ 1 chen chen 64 6月   9 13:38 255 -> /run/test/test.sh

man bash

[n]<>word

causes the file whose name is the expansion of word
 to be opened for both reading and writing on file descriptor n,
or on file descriptor 0 if n is not specified.

 If the file does not exist, it is created.

yurenchen
  • 471
0

TWIMC, if the solution proposed by @marek-vach does not fully work (the screen is blanked but not powered off, e.g. on a Mac running Linux), here's the possible fix.

Tested on MacBook Pro 15" 2013 running Fedora 41 Minimal headless.

  1. Remove his solution if you set it up.

  2. Add kernel parameter consoleblank=60.

This will lead to screen blanking (but not powering off) 60 seconds after boot and 60 seconds after any key press.

You could test it by simply pressing 'e' on grub screen during boot and adding this to the cmd line after something like "rhgb quiet".

To persist, update /etc/default/grub and add it to the end of GRUB_CMDLINE_LINUX before the finishing quote. Something like

GRUB_CMDLINE_LINUX="rd.luks.uuid=luks-... rhgb quiet consoleblank=60"

Then run something like to regenerate your grub config.

grub2-mkconfig -o /boot/grub2/grub.cfg
  1. Create the following script and set it executable.
> cat /usr/bin/screen-powersaving
#!/bin/sh -ex
DELAY="$1"
POWERSAVE_MODE="$2"
/usr/bin/setterm --powersave="${POWERSAVE_MODE:-powerdown}" --term=linux </dev/tty1 >/dev/tty1
{
/bin/sleep "$DELAY"
/usr/bin/setterm --blank=poke --term=linux </dev/tty1 >/dev/tty1
} &
> chown root:root /usr/bin/screen-powersaving
> chmod +x /usr/bin/screen-powersaving
> ls -Al /usr/bin/screen-powersaving
-rwxr-xr-x. 1 root root 231 Nov 10 10:54 /usr/bin/screen-powersaving

This script sets powersaving mode to power off, unless you pass a second parameter overriding it, and then pokes (wakes up) the screen after N delay, which you MUST pass as the first parameter.

  1. Create the following systemd service.
> cat /etc/systemd/system/screen-powersaving.service
[Unit]
Description=Set screen powersaving mode and poke it once after delay.
After=ssh.service

[Service] Type=oneshot ExecStart=/usr/bin/screen-powersaving 2m KillMode=process

[Install] WantedBy=multi-user.target

> ls -Al /etc/systemd/system/screen-powersaving.service
-rw-r--r--. 1 root root 215 Nov 10 11:03 /etc/systemd/system/screen-powersaving.service
  1. Enable this service.
> systemctl daemon-reload 
> systemctl enable --now screen-powersaving.service
Created symlink '/etc/systemd/system/multi-user.target.wants/screen-powersaving.service' → '/etc/systemd/system/screen-powersaving.service'.

After reboot, the behaviour would be as follows:

  • screen would blank roughly 60 seconds (consoleblank=60) after boot
  • it would then wake up roughly 60 seconds later (screen-powersaving 2m)
  • it would then blank AND power off roughly 60 seconds later (consoleblank=60)
  • and every time you press a key and wake it up, it would blank AND power off roughly 60 seconds later (consoleblank=60)

Feel free to adjust consoleblank=60 and screen-powersaving 2m delays to your liking.

Bear in mind that consoleblank=60 seems to start counting once grub passes control to kernel boot process, and it seems it remembers the powersave mode set at that time. screen-powersaving 2m starts counting moments before you see login prompt on the screen.

You should set the delays so that screen-powersaving 2m ends AFTER first consoleblank=60 ends. Otherwise, the screen will stay blanked until you wake it up manually once. After that it will blank AND power off every consoleblank=60 seconds. And both should finish after you see the login prompt.

As a rule of thumb, at minimum set consoleblank=X to 1.5 your bootup time, and screen-powersaving Xs to the same value.

neuen
  • 1