🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Linux User Session Management: Active Sessions, Idle Detection, and TTY Monitoring

Linux User Session Management: Active Sessions, Idle Detection, and TTY Monitoring

Managing user sessions is a critical security and operational task. Knowing who is logged in, what they are doing, and how long they have been idle helps detect unauthorized access, manage server resources, and maintain compliance. This guide covers practical session management techniques for Linux sysadmins.

Monitoring Active Sessions

# Who is logged in
who -u

# What are they doing (includes load average)
w

# Simple username list
users

# Active SSH connections
ss -tnp dport = :22

The w command is the most informative, showing username, TTY, login time, idle time, and current process for each session.

Session Types

Linux sessions come in different types:

  • pts/N — Pseudo-terminal (SSH, tmux, screen sessions)
  • ttyN — Physical/virtual console
  • :N — X11/Wayland graphical session

Idle Session Detection

Idle sessions consume resources and pose security risks. Monitoring idle time helps enforce session policies:

# Set automatic logout for idle sessions
# Add to /etc/profile or ~/.bashrc:
TMOUT=1800    # 30 minutes
export TMOUT
readonly TMOUT

Login History Analysis

# Recent login history
last -n 20

# Failed login attempts
lastb -n 20

# Last login for each user
lastlog

# Total connect time per user
ac -p

Automated Session Management

pip install dargslan-user-sessions
dargslan-sessions report     # Full session report
dargslan-sessions active     # Current sessions
dargslan-sessions idle 30    # Sessions idle >30 minutes
dargslan-sessions types      # Sessions by type
dargslan-sessions last 20    # Recent login history

Security Best Practices

  • Set TMOUT to auto-logout idle sessions
  • Use PAM pam_limits.so to restrict concurrent sessions per user
  • Monitor for sessions from unexpected IP addresses or countries
  • Log and alert on root logins
  • Regularly audit active sessions during security reviews

Download our free User Session Management Cheat Sheet. For advanced security techniques, explore our Security & Hardening eBooks.

Share this article:
Dargslan Editorial Team (Dargslan)
About the Author

Dargslan Editorial Team (Dargslan)

Collective of Software Developers, System Administrators, DevOps Engineers, and IT Authors

Dargslan is an independent technology publishing collective formed by experienced software developers, system administrators, and IT specialists.

The Dargslan editorial team works collaboratively to create practical, hands-on technology books focused on real-world use cases. Each publication is developed, reviewed, and...

Programming Languages Linux Administration Web Development Cybersecurity Networking

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.