Logging out a user from the command line in Linux ends the current session securely and returns the system to a login prompt or switches to another user. This is useful for remote servers, shared machines, or when you need to free up resources without rebooting. No special tools are needed beyond access to a terminal.
What You'll Need
- Access to a Linux terminal (local or via SSH)
- Sufficient permissions to log out the target user (usually your own user or sudo rights for others)
Step-by-Step
- Open a terminal window. If you are already in a terminal session, proceed to the next step.
- To log out your current user, run the command:
logout. This works in most login shells and will end the session, returning you to the display manager or login prompt. - If
logoutdoes not work (for example, in a non-login shell or graphical terminal), use:pkill -KILL -u $USER. This sends a termination signal to all processes owned by your user, effectively logging you out. Replace$USERwith your username if needed. - To log out a different user (requires sudo), first list active sessions with:
whoorw. Identify the target user and their terminal (e.g., pts/0). - Then run:
sudo pkill -KILL -u username, replacingusernamewith the actual username. This logs out the specified user immediately. - Alternatively, for a more controlled logout of another user, use:
sudo skill -KILL -u usernameif theskillcommand is available (part of procps). - After executing the command, verify the user is logged out by running
whoagain and confirming their session is no longer listed.
Common Mistakes
- Using
logoutin a graphical terminal emulator (like GNOME Terminal) where it may not be recognized as a login shell, leading to a "not login shell" error. - Forgetting that
pkill -KILLterminates all user processes abruptly, which can cause data loss if applications haven't saved work. - Attempting to log out another user without sudo privileges, resulting in a permission denied error.
- Confusing
logoutwithexit; while similar,exitonly ends the current shell, not necessarily the entire session in graphical environments. - Using
killall -u usernamewithout the-KILLflag, which may only send a termination signal that processes can ignore or catch.
Tips
- Prefer
logoutfor your own session when in a pure text console (like Ctrl+Alt+F2) as it is clean and designed for this purpose. - In graphical environments, consider using the desktop environment's logout option via menu for a safer session close that saves state.
- If you need to log out a user remotely and want to warn them first, use
wallto send a message before running the logout command. - For automation or scripting,
pkill -KILL -u usernameis reliable but always double-check the username to avoid logging out the wrong account. - After logging out a user, check system logs with
journalctlif you suspect issues, as abrupt terminations may leave traces.
Troubleshooting
If the logout command does not seem to work:
- First, ensure you are targeting the correct user and session. Use
wto see active processes and login times. - If
logoutfails with "not login shell", switch topkill -KILL -u $USERfor your own user or add sudo for others. - If permissions are denied, verify you have sudo access or are attempting to log out your own account.
- If processes persist after
pkill, wait a few seconds and runpkill -9 -u usernameto force termination with SIGKILL. - Check if the user is running a VNC or remote desktop session; those may require stopping the specific service (e.g.,
sudo systemctl stop vncserver@:1.service). - As a last resort, rebooting the system will clear all sessions, but this should be avoided on production servers unless necessary.
Frequently Asked Questions
Can I log out a user without killing their processes?
No, logging out inherently ends the user's session, which terminates all processes associated with that session unless they are deliberately detached (like with nohup or screen).
What is the difference between logout and exit?
logout is intended to end a login shell and session, while exit ends the current shell process; in many contexts they behave similarly, but logout may fail in non-login shells.
Will using pkill to log out a user damage the system?
It will not damage the system, but it can cause unsaved data loss in applications. Always prefer graceful logout methods when possible.
How do I log out a user via SSH if I’ve lost connection?
You cannot log out a user via SSH if you have no active connection; you need another terminal or physical access to run the commands.
Is there a way to log out all users except myself?
Yes, you can loop through active users with who and pkill, but exercise extreme caution to avoid locking yourself out. A safer approach is to reboot if multiple users need clearing.
Does logging out a user free up memory?
Yes, ending a user session releases all memory and resources allocated to their processes, which can help performance on heavily loaded systems.