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,093
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,093
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,640
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
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
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
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.

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
  • 2
  • 7
  • 12
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

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.

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