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 > Free Laravel, PHP, WordPress & Server Tutorials > 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
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

Run a Linux cron job as a non-root user
Server Management

How to Run a Cron Job as a Non-Root User

5 Min Read
Completely remove MariaDB from a RHEL-family server
Server Management

How to Remove MariaDB Completely from RHEL/CentOS

5 Min Read
Installing Docker on AlmaLinux guide
Server Management

Install Docker on AlmaLinux: Step-by-Step (Docker CE + Compose)

4 Min Read
JavaScript check valid URL — URL constructor vs regex
Web Development

How to Check if a JavaScript String Is a Valid URL

5 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?