logout Command

The logout command terminates the current login shell session, properly ending the user session and returning control to the parent process or login prompt.

Syntax

logout

Description

The logout command is a shell built-in that terminates the current login shell session. It can only be used in login shells and provides a clean way to end a user session.

Key characteristics:

  • Works only in login shells
  • Executes logout scripts and cleanup
  • Updates login records and logs
  • Terminates all child processes
  • Returns to parent shell or login prompt
Note: The logout command only works in login shells. For non-login shells, use the exit command instead.

Basic Usage

Simple logout

logout

Terminates the current login shell session

Keyboard shortcut

# Press Ctrl+D ^D

EOF (End of File) signal that triggers logout

Check if in login shell

echo $0 # Output starting with '-' indicates login shell # Example: -bash (login shell) vs bash (non-login shell)

Verify if current shell is a login shell

Logout Process

Steps During Logout

  1. Signal Handling - Process logout signal
  2. Script Execution - Run logout scripts (~/.bash_logout)
  3. Process Cleanup - Terminate child processes
  4. Session Recording - Update login records
  5. Resource Cleanup - Free system resources
  6. Shell Termination - Exit the shell process

Logout Scripts

# ~/.bash_logout - executed when bash login shell exits # Example logout script echo "Goodbye, $(whoami)!" echo "Session ended at $(date)" # Clear sensitive history history -c # Clean temporary files rm -f /tmp/user_temp_*

Example logout script for cleanup tasks

Alternative Logout Methods

Using exit Command

Basic exit

exit

Works in both login and non-login shells

Exit with status code

exit 0 # Success exit 1 # General error exit 2 # Misuse of shell command

Exit with specific return code

Keyboard Shortcuts

End of file (EOF)

# Ctrl+D sends EOF signal # Equivalent to logout in login shells # Equivalent to exit in non-login shells

Universal logout/exit shortcut

Interrupt signal

# Ctrl+C sends SIGINT (interrupt) # Does not logout, just interrupts current command # Ctrl+Z sends SIGTSTP (suspend) # Suspends current process, does not logout

Other keyboard shortcuts (not for logout)

Session Management

Checking Session Status

Current user and session info

# Current user whoami # Current session details who am i # All logged in users who # Detailed user activity w

Display current session information

Login history

# Recent logins last # Last login for current user last $(whoami) # Failed login attempts lastb

View login and logout history

Session Timeout

Set automatic logout timeout

# Set timeout to 30 minutes (1800 seconds) export TMOUT=1800 # Add to ~/.bashrc for permanent setting echo "export TMOUT=1800" >> ~/.bashrc # Disable timeout unset TMOUT

Configure automatic session timeout

Remote Session Logout

SSH Session Logout

Normal SSH logout

# Any of these will end SSH session logout exit # Ctrl+D

Standard methods to end SSH sessions

Disconnect SSH session

# SSH escape sequence (after Enter) ~. # Kill SSH connection # From another terminal: pkill -f "ssh user@hostname"

Force disconnect SSH sessions

Screen/Tmux Sessions

Screen session logout

# Detach from screen session # Ctrl+A, then D # Exit screen session completely exit # or logout

Manage screen session termination

Tmux session logout

# Detach from tmux session # Ctrl+B, then D # Exit tmux session completely exit # Kill tmux session tmux kill-session -t session_name

Manage tmux session termination

Troubleshooting Logout Issues

Common Problems

Logout command not working

# Check if in login shell echo $0 # If not login shell, use exit instead exit # Force logout (if needed) kill -9 $$

Resolve logout command issues

Hanging logout process

# Check for background jobs jobs # Kill background jobs kill %1 # Kill job 1 kill %2 # Kill job 2 # Kill all jobs kill $(jobs -p) # Then logout logout

Handle hanging logout due to background processes

Stuck in nested shells

# Check shell nesting level echo $SHLVL # Exit each shell level exit # Repeat until back to login shell # Then logout logout

Exit from nested shell environments

Security Considerations

Secure Logout Practices

Clear sensitive data on logout

# Add to ~/.bash_logout # Clear command history history -c history -w # Clear environment variables unset HISTFILE # Remove temporary files rm -f /tmp/user_* # Clear clipboard (if applicable) echo "" | xclip -selection clipboard 2>/dev/null

Secure cleanup during logout

Automatic screen lock

# Lock screen on logout (GUI systems) # Add to logout script if [ "$XDG_SESSION_TYPE" = "x11" ]; then xscreensaver-command -lock fi

Automatically lock screen during logout

Audit and Logging

Log logout events

# Add to ~/.bash_logout echo "$(date): User $(whoami) logged out from $(tty)" >> ~/.logout_log # System-wide logout logging logger "User $(whoami) logged out from $(tty)"

Track logout events for security auditing

Best Practices

Logout Best Practices
  • Always Logout - Properly end sessions when finished
  • Save Work - Save all work before logging out
  • Close Applications - Exit applications cleanly
  • Check Background Jobs - Ensure no important processes are running
  • Secure Cleanup - Clear sensitive data during logout
  • Use Timeouts - Set automatic logout for idle sessions

See also