50

Nov 9 17:55:46 swi007 systemd-logind[927]: New session 27 of user swi007.

I want to see the list of active login session in ubuntu server and would like to close the specific session in my server.

1 Answer 1

80

Here are few alternatives how to get the list of the current active sessions:

  • The command who - display who is on the system:

      $ who
      spas     pts/1        2017-11-05 21:43 (tmux(1597).%0)
      spas     pts/14       2017-11-09 13:02 (192.168.100.110)
      guest    tty2         2017-11-09 16:15 (:1)
    
  • The command w - displays information about the users currently on the machine:

      $ w
       16:16:12 up 3 days, 18:33,  9 users,  load average: 4,33, 2,79, 2,44
      USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
      spas     pts/1    tmux(1597).%0    нд21    3days  3days  3days tmux new-session -d -s my-tmux-session
      spas     pts/14   192.168.100.110  13:02    1.00s  0.29s  0.01s sshd: spas [priv]
      guest    tty2     :1               16:15    3days  1.27s  0.17s /sbin/upstart --user
    
  • The command last lastb - show a (history) listing of last logged in users. It provides few analytical options as --until, --since, etc.

  • The command users - print the usernames of users currently logged-in to the current host. It has really limited usage:

      $ users
      guest spas spas
    

To kill a specific session you could use who -u (or who -a) to print a column with the process identificators (PIDs) and then you can use sudo kill -9 <PID>:

$ who -u
spas     pts/1        2017-11-05 21:43  old         1597 (tmux(1597).%0)
spas     pts/14       2017-11-09 13:02   .         31822 (192.168.100.110)
guest    tty2         2017-11-09 16:15  old         2225 (:1)

$ sudo kill -9 2225   # force kill the third session from the above list

In addition you could kill all processes of a specific user by the following command:

sudo pkill -9 -u <username>

See also:

5
  • how to kill specific session Nov 9, 2017 at 14:20
  • @zapoxkrishna, check the update.
    – pa4080
    Nov 9, 2017 at 14:29
  • you missed the last command which as well list all active sessions :)
    – Videonauth
    Nov 9, 2017 at 14:48
  • Thank you, @Videonauth! Indeed I've never used this command. I've updated the answer.
    – pa4080
    Nov 9, 2017 at 14:58
  • 1
    For killing all processes I sorta like the slay command, but yours will do. Alas with systemd (and its logind) the meaning of login session seems to be somewhat different, so you should probably amend this to mention loginctl by now. Sep 2, 2020 at 16:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .