How to Implement Automatic Logout in Linux Bash Session After 5 Minutes of Inactivity


If you’re looking to enhance the security of your Linux system, setting up an automatic logout for the bash session after a period of inactivity is a great step. Here’s how to implement a 5-minute timeout policy:

  1. Set the Timeout Policy: Open the `~/.bash_profile` or `/etc/profile` file in your preferred text editor. Add the following lines to set a 5-minute (300 seconds) timeout:
    # Set a 5 min timeout policy for bash shell
    readonly TMOUT
    export TMOUT

    This code sets the `TMOUT` variable to 300 seconds. The `readonly` command ensures that the timeout duration cannot be modified during the session, and `export` makes it available to all shell sessions.

  2. Disabling the Timeout: If you need to disable the automatic logout feature, you can do so by running one of the following commands:
    1. To temporarily disable the timeout for your current session:
      # Disable timeout for the current session
      export TMOUT=0
    2. Or, to remove the `TMOUT` setting completely from your session:
      # Unset the TMOUT variable
      unset TMOUT
  3. Important Considerations: It’s crucial to note that the `readonly` attribute can only be reset by the root (administrator) user. This can be done either in the global bash configuration file (`/etc/profile`) or in a user’s custom bash configuration file (`~/.bash_profile`).

By following these steps, you can effectively manage the automatic logout feature for your Linux bash sessions, enhancing the security and efficiency of your system.