125

Sometimes when there are too many users logged in it can cause my computer to become very slow and laggy due to low amount of available RAM. I would like to be able to quickly log out the other users from the command line instead of manually switching into each user and logging them out.

Is this possible?

muru
  • 207,970
Isaiah
  • 60,830

4 Answers4

130

this is one answer

who -u

that give you the PID

Then you can kill the user session.

kill "pid"
David Foerster
  • 36,900
  • 56
  • 98
  • 152
hhlp
  • 42,932
  • 4
    This works but it has some strange side effects. – Isaiah Nov 08 '10 at 23:16
  • 4
    @DoR ...which are..? – Oli Nov 08 '10 at 23:48
  • 4
    @Oli Such as GDM restarting, and trying to switch to a user that I killed not working. – Isaiah Nov 08 '10 at 23:56
  • 3
    @AlvinRow If you execute ps auxf then you notice (left-most column has effective username) that this method doesn't kill all the processes which are executed by the current user (so probably you aren't logged out). The method of @precise seems to attend to this problem, though I don't feel comfortable with sending SIGKILL. – Dor Sep 29 '16 at 23:13
  • 1
    I'm surprised there isn't something like with shutdown so it can give warning (allowing work to be saved) before forcibly logging out. That would optionally just switch to the login as if changing sessions, leaving the user session running in the background. – pbhj Nov 25 '18 at 19:51
  • 1
    @pbhj about notifications, consider to see this answer https://askubuntu.com/a/967130/970554 – Manuel Jordan Apr 21 '22 at 20:36
  • As of Ubuntu 22, this worked perfectly to remote into a desktop with ssh, kill the active 'local' desktop session I had not logged out of before leaving work, and then being able to access the desktop via RDP, which I could not before doing so – Douglas B May 15 '24 at 04:19
55

You may use who to check which users are logged in:

who

You can log-out the user by sending the KILL signal to the user-process with:

sudo pkill -KILL -u <username>

(which is same as sudo pkill -9 -u <username>)

example:

sudo pkill -9 -u guest-2Rw4Lq

(to kill a guest session user named guest-2Rw4Lq)

Note (kudos to pbhj): If you get locked in a console, use Ctrl+Alt+F7 to get back to the GUI.

Lorenz Keel
  • 9,551
rusty
  • 16,927
  • 2
    This worked best for me. Simply running kill "pid" left a ton of processes by the user still running, where this killed them all. – Matt Baer Aug 30 '15 at 18:42
  • 1
    Why SIGKILL and not the default SIGTERM ? The SIGKILL isn't healthy.. – Dor Sep 29 '16 at 23:06
  • sudo pkill -KILL -u <username> for me switched me to the first console session; I thought it killed my current session but ctrl+alt+F7 brought me back to the current graphical session. – pbhj Mar 08 '17 at 20:22
  • -1: This command somehow triggered a higher priority process that never died, I had to restart the machine to kick the user. – RSFalcon7 Nov 13 '19 at 14:57
10
who -u


> adam     ttys000  Aug  4 09:22   .       91228 

then

sudo kill 'PID number'
sudo kill 91228

PID (process ID) is the four or five digit number at the end of the user readout (91228)

adm
  • 211
0

I use the following, it has only one side-effect - it bumps you into the "switch user" login screen,
however, it doesn't log you out :-)

loginctl list-sessions

to list session IDs

loginctl terminate-session TARGET_SESSION_ID

to terminate the other session (replace TARGET_SESSION_ID with the session id you want to terminate from the "SESSION" column from the list-sessions sub-command.

jave.web
  • 452