When you use an SSH client or a local terminal emulator to connect to a Linux server, the commands you execute and the output generated are left visible on the screen when you type exit or log out. If you are sharing a physical workstation or stepping away from your desk, this leftover text can expose sensitive information, such as server IP addresses, file paths, or configuration data, to anyone who glances at the monitor. You can enhance your privacy by forcing the bash shell to automatically wipe the screen the moment you log out.
How the .bash_logout File Works
Every time a user logs out of an interactive login shell, the bash interpreter looks for a hidden script named .bash_logout inside that user’s home directory. If the file exists, bash executes any commands contained within it before finally closing the session. By appending a simple clear command to this file, we can guarantee the terminal buffer is erased.
How to Configure Automatic Screen Clearing
You can set this up for your individual user account in just a few seconds using the nano text editor.
- Open your terminal and log into your Linux system.
- Navigate to your home directory by typing
cd ~and pressing Enter. - Open the logout script using nano:
nano .bash_logout(If the file does not exist, nano will automatically create a new, empty file). - Move your cursor to the very bottom of the file.
- Type the following command on a new line:
clear - Press Ctrl + O followed by Enter to save the file.
- Press Ctrl + X to exit the nano editor.
How to Ensure the Scrollback Buffer is Also Cleared
While the standard clear command removes the visible text, a determined person could still scroll up using their mouse wheel to see the history if their terminal emulator supports a scrollback buffer. To securely wipe the entire buffer in addition to the visible screen, use this slightly modified command instead:
In your .bash_logout file, replace clear with the following line:
clear && echo -ne "\033c"
This command sends an explicit escape sequence to the terminal emulator instructing it to perform a hard reset, effectively destroying the scrollback history before the session fully terminates.