The Linux logout command lets you cleanly end your current session, freeing up system resources and removing access for other users. You’ll need no special tools—just your terminal or graphical session—but ensure you’re not in critical files or processes before logging out.

What You'll Need

  • A running Linux system (desktop, server, or embedded).
  • Your current user session open (via terminal, GUI desktop, or SSH).
  • If running GUI apps, be mindful of unsaved work (see "Tips" for saving strategies).
  • No additional software needed—built-in commands suffice.

Step-by-Step

  1. In terminal: Open your command-line interface (Ctrl+Alt+T on many desktops). Use one of these commands to exit:
    • logout – Immediate logout (does not save desktop session).
    • exit – Same as logout (common shortcut).
    • pkill -u $USER – Forcefully terminates all user processes (use with caution).
    Press Enter after typing.
  2. GUI desktop: Click the top-left menu (application launcher) > Quit or Exit (e.g., GNOME menu > Quit > Exit). This saves desktop settings (like window layouts) before logging out.
  3. SSH session: On remote servers, type exit or logout in the terminal. Logout does not close the SSH connection—use exit or control-D to fully disconnect.
  4. Systemd-based systems: For services managed by systemd (most modern Linux), log out via any standard method. Logout terminates the user session but leaves systemd services running unless you use systemctl isolate default (requires sudo).

Common Mistakes

  • Using kill or pkill incorrectly: These forcefully terminate processes. Only use pkill -u $USER if absolutely necessary (e.g., stuck sessions), but it may leave orphaned processes. Avoid on shared servers.
  • Ignoring unsaved work: Logout does not save GUI desktop sessions by default. Use exit or GUI menus to save before logging out, or back up files manually.
  • Confusing logout with reboot: Logout only ends your session; reboot requires sudo reboot or sudo shutdown -r now. Typing reboot by mistake will halt everything.
  • Overlooking sudo privileges: If you’re logged in as root, logout works, but sudo may prompt for a password afterward if services require it.

Tips

  • Save before logging out: For GUI apps, use Alt+F2 > type r > confirm to restart the desktop environment (saves session). On TTYs, use exit with a backup plan.
  • Check for locked files: If you’re unsure if processes are running, run ps -u $USER to list user processes. Close only what you need (e.g., kill a single process).
  • Use loginctl for systemd: List sessions with loginctl list-sessions to check active users. Kill a session with loginctl terminate-session (replace with the session ID from the list).
  • SSH sessions: If you’re unsure if you’re fully disconnected, run ssh -v to debug reconnection attempts.
  • Automatic logout: To set a default timeout (e.g., 5 minutes), edit /etc/systemd/logind.conf: HandleLongevity=ignore IdleAction=relax (Requires systemd; note: this affects all users.)

Troubleshooting

  1. Logout doesn’t work:
    1. Check if another session is running: who or w. If another user is logged in, log that user out first.
    2. Verify the command is recognized: Try which logout. If it returns nothing, your system uses exit as the default.
    3. Restart the session: On desktop, log in again; on TTY, press Ctrl+Alt+F2 to switch to a fresh TTY.
    4. Check systemd status: If you’re on systemd, run systemctl status systemd-logind. A crash may block logout.
  2. Processes persist after logout:
    1. List processes: ps -ef | grep $USER. Identify orphaned processes (e.g., grep -v grep to exclude grep itself).
    2. Kill processes one by one: kill -9 (replace with the process ID).
    3. Reset the user session: loginctl terminate-user $USER (systemd only).
  3. GUI session freezes on logout:
    1. Kill the desktop environment: pkill gdm (GNOME), pkill kdeinit (KDE), or pkill xrdp (Remote Desktop).
    2. Log in via TTY (Ctrl+Alt+F2) and restart the desktop.

Frequently Asked Questions

How do I log out from a TTY?

  1. Use exit or logout in the terminal. If you’re in an SSH session, exit will fully disconnect.

Can I log out without closing all my applications?

  1. For GUI sessions, use Alt+F2 > r to restart the desktop (saves session).
  2. For TTYs, save files manually or use exit with a backup plan.

What if my logout command doesn’t work?

  1. Check for another session: Run who or w. If another user is logged in, log that out first.
  2. Try logging in via a different TTY (Ctrl+Alt+F2) and using exit there.

How do I log out remotely on a server?

  1. SSH into the server and type exit or logout. This closes the SSH session.
  2. If you need to fully reboot, use sudo reboot.

Will logging out affect systemd services?

  1. Logout ends the user session but leaves systemd services running unless you use systemctl isolate default.
  2. To stop a service during logout, add --system to your logout command (e.g., sudo systemctl isolate default --force).

How can I force a logout if I’m stuck?

  1. Switch to another TTY (Ctrl+Alt+F2), become root with sudo su, and run loginctl terminate-user $USER (systemd).
  2. Or kill all user processes with pkill -u $USER (use with caution).

Remember: Always verify your session before forcefully closing it, especially on shared systems.