Logging out of the Linux command line means ending your current terminal session gracefully, freeing up resources, and properly closing all open applications or background processes. The command is simple: use `exit` or `logout`, but there are critical steps, common pitfalls, and scenarios that require different approaches depending on your setup. This guide covers everything—from the basic command to advanced troubleshooting—so you never lose work or encounter session corruption.
What You'll Need
- A terminal session already open in Linux (Ubuntu, Fedora, Arch, Debian, or any other distro).
- A basic understanding of Linux commands—`exit` and `pkill` are required for this, but no other tools are necessary.
- Optional: A list of running processes you want to ensure terminate (if you’re unsure, proceed with caution).
Step-by-Step
- Use the `exit` command. Type `exit` in the terminal and press Enter. This closes the current shell session and logs you out of your user account. If you're in a desktop environment (like GNOME, KDE, or Xfce), the window manager will also close. This works in most shells (bash, zsh, fish) and is the standard way to log out.
- Use `logout` for persistent sessions. If you’re in a **persistent terminal session** (common in X11 environments where the session persists across terminal restarts), type `logout`. This sends a proper termination signal to the desktop environment and closes all open applications. Only use this if you’re in a session managed by a desktop environment (not a detached SSH session).
-
Force a clean shutdown if stuck.
If your session freezes or hangs and `exit`/`logout` doesn’t respond, try killing all running processes:
- Find your user’s PID (Process ID):
whoami→ Let’s say your user isjohn. Run:ps -u johnto list all processes for your user. - Kill all processes:
pkill -u john(replacejohnwith your username). This kills all processes owned by your user. Use with caution!
- Find your user’s PID (Process ID):
-
Log out via GUI if in a desktop environment.
If you’re using a graphical desktop (like GNOME, KDE, or Unity), you can also:
- Click the top-right menu bar and select Quit or Logout.
- Or press Alt+SysRq (Alt + Print Screen key), then type
kto force a clean shutdown (useful if the session is unresponsive).
Common Mistakes
-
Logging out mid-process.
If you log out while a critical task (like compiling code or running a long script) is still running, your work may be lost. Always:
- Save changes (e.g., `git commit` or `sudo cp` if necessary).
- Or run `Ctrl+D` (EOF) to exit gracefully if you don’t need the terminal anymore.
-
Using `exit` in a detached SSH session.
If you’re in a **detached SSH session** (e.g., `ssh user@host && detach`), `exit` won’t log you out—it just closes the terminal. To properly logout in SSH, you must either:
- Reattach with `ssh -t user@host` (force reattach), or
- Run `logout` if the SSH session is tied to a desktop environment.
- Killing the wrong processes. Using `pkill -u john` can delete sudoers or other critical system files if misused. Always verify the PID first (`ps -u john`).
Tips
-
Always check `whoami` before killing.
If unsure who owns a process, run `ps aux` and filter by username. Example:
ps aux | grep -i john - Use `sudo su` carefully. If you’ve switched to `sudo` and forgot, exit with `exit` or `logout`—but be aware you won’t be logged out of your system, only the `sudo` session.
- For persistent sessions, log out at the desktop level. If your terminal keeps restarting (e.g., in X11), use GUI logout or `pkill -u $USER` to terminate all apps.
- Test `logout` in a new session. To confirm `logout` works, open another terminal and type `whoami`. If the session appears inactive, it’s working.
-
Disable auto-login if unsure.
If you’re frequently logging out, check your desktop environment’s startup settings to avoid accidental auto-login:
- On GNOME: Settings > Sessions > Disable "Auto-login".
- On KDE: System Settings > Session and Startup > Login Options.
Troubleshooting
If logging out doesn’t work as expected, follow this order:
- Check if the terminal is truly dead. Press Ctrl+Alt+F1-F6 to switch to a TTY (virtual console) and log in. If you’re here, your session is unresponsive.
-
Restart the desktop environment.
If you’re in a GUI, force a restart:
- Press Alt+F2, type `r`, then Enter (restart).
- Or run `sudo systemctl restart gdm` (for GNOME Display Manager).
-
Delete the session file (last resort).
If nothing works, delete the current session entry:
- Find your session ID:
echo $XDG_SESSION_ID - Delete it:
rm -f .Xsession-
- Find your session ID:
-
Reboot the system.
If all else fails, shut down normally and restart:
- Press Alt+SysRq, then
s(shutdown). - Wait for cleanup, then boot again.
- Press Alt+SysRq, then