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

  1. Open a terminal window. If you are already in a terminal session, proceed to the next step.
  2. 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.
  3. If logout does 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 $USER with your username if needed.
  4. To log out a different user (requires sudo), first list active sessions with: who or w. Identify the target user and their terminal (e.g., pts/0).
  5. Then run: sudo pkill -KILL -u username, replacing username with the actual username. This logs out the specified user immediately.
  6. Alternatively, for a more controlled logout of another user, use: sudo skill -KILL -u username if the skill command is available (part of procps).
  7. After executing the command, verify the user is logged out by running who again and confirming their session is no longer listed.

Common Mistakes

  • Using logout in 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 -KILL terminates 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 logout with exit; while similar, exit only ends the current shell, not necessarily the entire session in graphical environments.
  • Using killall -u username without the -KILL flag, which may only send a termination signal that processes can ignore or catch.

Tips

  • Prefer logout for 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 wall to send a message before running the logout command.
  • For automation or scripting, pkill -KILL -u username is reliable but always double-check the username to avoid logging out the wrong account.
  • After logging out a user, check system logs with journalctl if 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 w to see active processes and login times.
  • If logout fails with "not login shell", switch to pkill -KILL -u $USER for 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 run pkill -9 -u username to 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.