Change Linux console screen blanking behavior

72,263

Solution 1

setterm from @whitequark's answer is a reasonable userspace tool, but it's not the whole story.

The default console blanking behavior is baked into the kernel at compile time. It is configurable at boot time with the paramater consoleblank=, or in userspace with setterm. From the kernel documentation (kernel-parameters.txt):

consoleblank=  [KNL] The console blank (screen saver) timeout in
               seconds. Defaults to 10*60 = 10mins. A value of 0
               disables the blank timer.

Here are the options, their defaults on my Ubuntu system, and their spheres of influence:

  • setterm -blank [0-60]; always reports 0 when queried; effective when run on a real VT; affects all real TTYs; not effective when run in screen sessions on a VT.
  • setterm -powerdown [0-60]; always reports "3]" (??); doesn't seem to have any effect. Ubuntu kernels don't enable APM_DISPLAY_BLANK, and this could be related.
  • consoleblank=N; defaults to 600 (10 minutes); affects all real VTs; affects screen sessions in a VT; no way to set while running.

So my options for changing the default is one of the following:

  1. Add setterm -blank X (X in minutes, 0 to disable) to a shell init file like .bashrc.
  2. Add setterm -blank X to /etc/rc.local.
  3. Add consoleblank=Y (Y in seconds, 0 to disable) to the kernel commandline by adding it to the parameter lists in /etc/default/grub, either GRUB_CMDLINE_LINUX or GRUB_CMDLINE_LINUX_DEFAULT. (Don't forget to update-grub.)

Solution 2

Try setterm -blank $minutes (or pass 0 to disable); -powersave option may also be related. setterm has a plenty of other useful options, too.

If you want to set these attributes on system startup, consider writing an initscript. This is just a script placed in /etc/init.d directory. Let it be called setterm:

#!/bin/sh
[ "$1" == "start" ] || exit 0 # only initialize everything when called as /etc/init.d/setterm start
for term in /dev/tty[0-9]*; do # select all ttyNN, but skip ttyS*
    setterm -blank 0 >$term <$term
    setterm -powersave off >$term <$term
done

Then make it executable:

# chmod +x /etc/init.d/setterm

And finally, create the /etc/rcX.d symlinks (the Debian way):

# update-rc.d setterm defaults

(If you'll get tired of that behavior, do # update-rc.d -f setterm remove. Note that -f must be the first argument).

Solution 3

If anyone is looking for another possible solution for Debian (possibly not Ubuntu):

In /etc/kbd/config, look for a setting called "BLANK_TIME":

# screen blanking timeout.  monitor remains on, but the screen is cleared to
# range: 0-60 min (0==never)  kernels I've looked at default to 10 minutes.
# (see linux/drivers/char/console.c)
BLANK_TIME=30

Change it to 0, this will disable it:

BLANK_TIME=0

Tested on Debian 6 and 7.

Solution 4

If you are running a newer Ubuntu that uses upstart, you can use:

for file in /etc/init/tty*.conf; do tty="/dev/`basename $file .conf`"; echo "post-start exec setterm -blank 0 -powersave off >$tty <$tty" | sudo tee -a "$file"; done

A little explanation of what's going on here:

Newer Ubuntu versions use upstart for system startup. With upstart, the Linux consoles are setup with config files stored within /etc/init. The command above starts by iterating over each of those config files:

for file in /etc/init/tty*.conf;

The tty's upstart config file name in $file is used to build the name of the tty device:

tty="/dev/`basename $file .conf`";

An upstart "post-start" command is built that runs "setterm" to disable screen blanking and power saving after the tty has been started:

echo "post-start exec setterm -blank 0 -powersave off >$tty <$tty"

And finally that command is appended to the upstart config file:

| sudo tee -a "$file";

Solution 5

On my systems (various releases of RedHat Enterprise Linux), I have found that different approaches are needed.

For my RHEL 5 and 6 systems, I am able to add the line

/bin/setterm -blank 0 -powerdown 0 -powersave off

to /etc/rc.local. This disables the console screen blanking at system startup.

I found that this does not work on RHEL 7 systems. On RHEL7, running setterm from rc.local causes an error to be generated:

setterm: $TERM is not defined.

The command works from an interactive shell, where $TERM is defined (as linux). If I force setterm to use it:

/bin/setterm -term linux -blank 0 -powerdown 0 -powersave off

Then I get a different error:

setterm: cannot (un)set powersave mode: Inappropriate ioctl for device

Even though the same command works fine from an interactive session. Setting the consoleblank kernel parameter worked.

On RHEL7, edit /etc/default/grub and append consoleblank=0 to the GRUB_CMDLINE_LINUX parameter. Then run grub2-mkconfig -o /boot/grub2/grub.cfg and reboot.

I haven't tried setting consoleblank on RHEL5 or 6.

Share:
72,263

Related videos on Youtube

quack quixote
Author by

quack quixote

Updated on September 17, 2022

Comments

  • quack quixote
    quack quixote over 1 year

    How do I change the screen blanking behavior on Linux virtual terminals?

    For example, if I switch to a VT from X, login, and leave the system alone for 5 minutes or so, the screen will blank like a screensaver. It comes back with any keypress, like a screensaver.

    Mostly I just want to change the timeout, but I'm also interested in other settings.

    If it helps, one of my systems is running Ubuntu 10.04 with the stock graphics drivers. fbset shows the console using the radeondrmfb framebuffer device.

  • quack quixote
    quack quixote almost 14 years
    hrm. seems useful for on-the-fly, tho it seems on my test Ubuntu system it's actually the -powerdown setting that's in effect. what about setting an on-boot, system-wide default? is running setterm -blank X or setterm -powerdown Y in /etc/rc.local (or ~/.bashrc) reasonable?
  • whitequark
    whitequark almost 14 years
    Yes, but only if rc.local actually works. (Recent upstart migration screwed up some things, through it apparently exists and works on my system.) Otherwise you should create an initscript. That's easy: in our case, it must initialize everything when passed start as first argument, and do nothing otherwise. I'll describe that in my answer.
  • whitequark
    whitequark almost 14 years
    Ahem. Seems that setterm emits escape sequences on stdout, but checks TTY type with stdin: I just checked, and setterm ... >/dev/ttyN </dev/ttyN works as intended.
  • quack quixote
    quack quixote almost 14 years
    that makes sense, and it works (from a real TTY), but what's the point of using that syntax at all? (1) still doesn't work from within screen (or potentially other PTYs), which is where specifying a /dev/ttyN would be useful; (2) setting one VT affects all, so no need for the initscript loop.
  • whitequark
    whitequark almost 14 years
    Very strange: I tested that command from an ssh session (don't have a keyboard attached to server anyway). upd: probably that's because I have a Debian system there. Usually Ubuntu and Debian don't differ much, but this is apparently the case.
  • Cody Hess
    Cody Hess over 9 years
    I'd like to note that setterm -blank X did not work for me from within tmux, but worked perfectly when I exited tmux before running the command.
  • user3132194
    user3132194 over 9 years
    If you are using grub2 as a bootloader, you could add this kernel parameter in /etc/default/grub GRUB_CMDLINE_LINUX_DEFAULT="consoleblank=0" and then update your grub config with grub[2]-mkconfig -o /boot/grub/grub.cfg.
  • Daniel Alder
    Daniel Alder over 9 years
    3] for setterm -powerdown is only the half true: setterm -powerdown 3 | hexdump -c returns 033 [ 1 4 ; 3 ]. Unsure what should interprete this escape sequence...
  • basic6
    basic6 almost 9 years
    This is about console screen blanking, not Gnome screensavers.
  • David C.
    David C. over 8 years
    Depending on your distribution and where you want the change to take effect, some of these options may not work. I found that setterm in rc.local works great for RHEL 5 and 6, but not 7.
  • mu1988
    mu1988 almost 6 years
    RHEL7 uses systemd; rc.local is not connected to a terminal by default. I think you can add </dev/tty1 >/dev/tty1 or something to make it work
  • sunknudsen
    sunknudsen about 2 years
    What is the difference between -blank and -powerdown? Trying to turn off display (vs set brightness to 0).
  • sunknudsen
    sunknudsen about 2 years
    Does setting consoleblank=600 in /etc/default/grub and running update-grub turn off display or does it set display brightness to 0?
  • Admin
    Admin almost 2 years
    Since linux 4.12 the consoleblank default is 0, i.e. disabled: tty: Disable default console blanking interval