When I log in with the same user on tty1 and tty2 and start an X server session on tty1 - as soon as I log out of tty2, the X server session on tty1 crashes.

This seems to be a known bug:

As you can read in the bug reports, the problem lies with the call to clear_console in ~/.bash_logout. If I remove that line, everything works fine.

My question:

I didn't notice any changes. (Obviously apart from the X server not crashing any more.) The console is being cleared as I log out - regardless of me removing that line. So, what is the call to clear_console there for in the first place?


The world wants you to clean your screen.

As I said at https://unix.stackexchange.com/a/233855/5132 and indeed in the indirectly referenced mailing list discussion ☺ …

Wanting not to clear a virtual terminal between log-off and subsequent log-on is very much swimming against the tide, as Greg Wooledge and others have discovered. The presence of sensitive output from privileged users, or bosses, remaining after log-off has been a security problem for Unices (and indeed other timesharing remote-access operating systems) since the 1970s, and it takes a lot of effort to undo all of the things that people have put in to avert this problem. As you have observed, they have put in several overlapping mechanisms that do this.

  • Many systems have a clear_console command in their shell logout scripts as standard. (This is problematic in its own right, as it doesn't play well with graphical programs running on kernel virtual terminal #1, and doesn't work with any other kinds of terminals, virtual or real.)

    This command has to be removed.

  • The default in getty programs aimed at virtual terminals, such as mingetty, is to clear the terminal. (It does this before log-on, meaning that terminal output can remain unerased if a TTY login service is stopped. Ironically, this functionality would have been better placed in login, which thanks to the necessities of PAM is still running at log-off.)

    The --noclear option has to be deployed to disable this. On systemd operating systems this involves writing one or more unit file override files, changing the ExecStart setting, or simply pointing autovt@.service at a local unit file of one's own devising.

  • systemd's supplied getty@.service template service unit sets TTYVTDisallocate=yes which instructs systemd to clear a kernel virtual terminal. (This again doesn't work with any other kinds of terminals, not even user-space virtual terminals, as partly reflected in its name.)

    This too has to be removed, again with an override or a different service template pointed to by autovt@.service.

They are not entirely overlapping, of course. mingetty isn't useful for real terminals connected via serial devices; clear_console is, oddly, part of the Bourne Again shell package and isn't invoked by people who have the Korn, Z, Almquist, Fish, Watanabe, or other shell as their user account's interactive login shell; and the systemd TTYVTDisallocate mechanism has no applicability to non-systemd operating systems.

As I said in the mailing list discussion several years ago, console_clear is doubly unnecessary. Since 2011 the Linux built-in terminal emulator has supported the ED 3 control sequence for clearing the scrollback buffer, so the mucking around with switching virtual terminals that has these detrimental side-effects isn't necessary in the first place. I added a console-clear command to the nosh toolset several years ago, that emits this control sequence (and is thus usable with more than just the Linux built-in terminal emulator, as well as working over a remote connection). The ncurses clear command also nowadays knows to emit the relevant control sequences, if terminfo says so.

And of course console-terminal-emulator clears its display buffer at terminal hangup as standard.

Further reading

| improve this answer | |

According to dlocate, clear_console is part of the (Debian) bash package. It is not part of the upstream bash sources. The package changelog tells you when/where it came from:

 -- Matthias Klose   Thu, 23 Mar 2006 01:16:22 +0100

bash (3.1-3) unstable; urgency=low


  Merge from Ubuntu:
  * clear_console: New helper program to clear the console, including
    the scrollback buffer.
  * /etc/skel/.bash_logout: Install it again and use clear_console.
    Ubuntu #29405. Closes: #331504.

The referenced bug-reports give the reason why it was added:

  • Ubuntu #29405, January 2006 'clear' is not run before 'exit'

    When logging out of a F1-F6 terminal, all of your existing output of activies are left on the screen even when the next login screen appears. The screen should be cleared before the new login appears, so private output won't be viewed by other users.

  • Debian #331504, October 2005 bash: Please provide a default /etc/skel/.bash_logout

    It would be nice if bash would provide a default .bash_logout in /etc/skel for users to use by default to clean up the screen when login through console this is common request, specially in multi-user environments where people don't want others to see what they were working on.

By the way, that was mentioned in conjunction with ncurses here:

The former was resolved by adapting a feature from xterm patch #107 in 1999 (for the Linux console), while the latter went nowhere because of license incompatibilities.

| improve this answer | |

I think this has to do with privacy and security. It wouldn't be wise to leave open to anyone's eyes what you did in your ended session. On my Debian the full entry in .bash_logout is:

if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q

$SHLVL is 1 when it's the first shell in the chain. (You can start a new shell from a shell and this increments the SHLVL.) Once you're ending the first session and so passing the console and prompt to anyone, the console should keep the contents of your session inaccessible to unauthorised eyes, and so it does.

| improve this answer | |
  • @Jayjayyy If it does clear anyway, then I see no reason for not removing it. But be careful if you change some other parameters, which change this behaviour (see sourcejedi's answer). – user147505 Jun 21 '18 at 11:49

Your tty is being cleared due to a configuration in the init system. Nowadays, probably systemd.

for systemd set TTYVTDisallocate to no.

to achieve this, run systemctl edit getty@tty1 and enter the code below



A comment points to a longer page which also mentions a configuration which could be used under sysvinit: the --noclear option of getty.


Finally, it sounds like the clear feature in getty (and hence the --noclear option of getty) has not existed in all versions.

As the system administrator, you can configure the console to always be cleared before showing the login prompt. The easiest way to do that is to use mingetty instead of getty; you lose support for serial consoles but gain a few features such as screen clearing.

-- How to clear terminal after logging out? answer posted 2010.

Therefore, bash_logout might have been the only way to clear the screen without switching to a different getty.

| improve this answer | |

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.