Change Linux console screen blanking behavior
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:
- Add
setterm -blank X
(X in minutes, 0 to disable) to a shell init file like .bashrc. - Add
setterm -blank X
to /etc/rc.local. - 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 toupdate-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.