How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: Automatic Logout Timeout for Command Line in Ubuntu (TMOUT 300s)
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Learn > Server Management > Automatic Logout Timeout for Command Line in Ubuntu (TMOUT 300s)
Server Management

Automatic Logout Timeout for Command Line in Ubuntu (TMOUT 300s)

how7o
By how7o
Last updated: January 12, 2026
5 Min Read
Automatic logout timeout for command line in Ubuntu (TMOUT 300s)
SHARE

A while ago I left an SSH session open on my Ubuntu VPS while I went to do something “for two minutes”… and of course it turned into a long break. When I came back, the terminal was still logged in. No password, no screen lock, nothing. That was the moment I decided to set an automatic logout timeout for command line in Ubuntu — not because I don’t trust myself, but because “future me” is always busy and forgetful.

Contents
  • What TMOUT actually does (so you don’t get surprised)
  • Best way: set a global timeout for all users (recommended)
    • Step 1: create a global timeout script
    • Step 2: log out and log back in (or reconnect via SSH)
  • Quick method: add it to /etc/profile (works, but less clean)
  • Optional: exclude a specific user (like root or a service account)
  • What if users try to bypass it?
  • Bonus: enforce an idle disconnect at the SSH level (different thing)
  • Related guides (internal links)
  • Final thoughts

The good news: Ubuntu (and Bash) already has a simple built-in option for this. You can force inactive terminals to auto-logout after a set time (like 300 seconds / 5 minutes) using a Bash variable called TMOUT. The trick is setting it globally for all users, and locking it so users can’t bypass it.

How to set TMOUT automatic logout timeout on Ubuntu (step-by-step)

What TMOUT actually does (so you don’t get surprised)

TMOUT is a Bash timeout value (in seconds). When it’s set, Bash will exit an interactive shell after waiting at the prompt with no input for that amount of time. In other words: it kicks you out when you’re idle at the command prompt — not in the middle of running a long command. :contentReference[oaicite:0]{index=0}

Best way: set a global timeout for all users (recommended)

You can put the setting into /etc/profile (and that’s what many quick answers suggest), but I prefer using a dedicated file in /etc/profile.d/. It’s cleaner and easier to manage later.

Step 1: create a global timeout script

sudo nano /etc/profile.d/timeout.sh

Paste this (5 minutes = 300 seconds):

# Apply only to interactive shells
case $- in
  *i*) ;;
  *) return ;;
esac

TMOUT=300
readonly TMOUT
export TMOUT

Make it executable:

sudo chmod +x /etc/profile.d/timeout.sh

Step 2: log out and log back in (or reconnect via SSH)

Because this is loaded at login time, you need a fresh session to test it. Bash reads /etc/profile (and files like /etc/profile.d/*.sh) when it starts as a login shell. :contentReference[oaicite:1]{index=1}

Test the value:

echo $TMOUT

Now just stop typing at the prompt and wait 5 minutes — it should automatically log you out.

Quick method: add it to /etc/profile (works, but less clean)

If you want the simplest “do exactly what the forum answer says” approach, edit /etc/profile and add these lines at the end:

sudo nano /etc/profile

# Add at the bottom:
TMOUT=300
readonly TMOUT
export TMOUT

This is the same solution from your original How7o thread (set TMOUT=300, make it readonly, export it, then re-login). :contentReference[oaicite:2]{index=2}

Optional: exclude a specific user (like root or a service account)

On some servers I prefer not to enforce auto-logout for a specific automation user (or sometimes root during maintenance). You can add a simple skip rule before setting TMOUT:

# Example: skip root
if [ "$(id -u)" -eq 0 ]; then
  return
fi

Add that near the top of /etc/profile.d/timeout.sh, above the TMOUT lines.

What if users try to bypass it?

That’s why I always include:

  • readonly TMOUT → prevents users from changing it inside their shell
  • export TMOUT → ensures it applies consistently

Bonus: enforce an idle disconnect at the SSH level (different thing)

TMOUT is a Bash behavior. If you also want SSH itself to drop idle connections (even if someone isn’t sitting at a Bash prompt), look at SSH server keepalive options like ClientAliveInterval and ClientAliveCountMax in /etc/ssh/sshd_config. :contentReference[oaicite:3]{index=3}

Related guides (internal links)

  • How to change the welcome message on Ubuntu VPS
  • How to install Docker on AlmaLinux

Final thoughts

If you manage a VPS (or even a shared server), setting an automatic logout timeout for command line in Ubuntu is one of those small hardening steps that pays off forever. I went with 5 minutes (TMOUT=300) because it’s enough time to think, but short enough to protect me from leaving sessions open by accident.

TAGGED:BashLinux HardeningSecuritySSHterminalTMOUTUbuntuvps

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Create a Directory in Ubuntu Create a Directory in Ubuntu (mkdir Command + Examples)
Next Article Change welcome message on Ubuntu VPS server (MOTD + SSH banner) Change Welcome Message on Ubuntu VPS (MOTD + SSH Banner)
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Display PHP errors — ini_set + php.ini configuration
How to Display PHP Errors
May 10, 2026
PHP convert string to uppercase — strtoupper and mb_strtoupper
How to Convert a String to Uppercase in PHP
May 10, 2026
PHP string to float conversion with cast, regex cleanup, NumberFormatter
How to Convert a String to Float in PHP
May 10, 2026
PHP merge arrays without duplicates — union operator and array_unique
How to Combine Two Arrays Without Duplicates in PHP
May 10, 2026
PHP delete array element — unset, array_splice, array_filter, array_search
How to Delete an Element from a PHP Array
May 10, 2026

You Might Also Like

Install Node.js on Ubuntu — terminal with NodeSource setup_22.x curl command and Node.js hexagon icon
Web Development

How to Install Node.js on Ubuntu (22.04 & 24.04): Step-by-Step

11 Min Read
MySQL connect remote from Ubuntu — mysql-client + mysql -h host
Server Management

How to Connect to a Remote MySQL Database from Ubuntu

7 Min Read
Check if GD library is installed in PHP (phpinfo and extension_loaded)
Web Development

How to Check if GD Library Is Installed in PHP (3 Easy Methods)

5 Min Read
Laravel run without .env file — env() fallback in config/app.php
Web Development

How to Run a Laravel Project Without a .env File

8 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?