53

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.

pa4080
  • 30,621

1 Answers1

85

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:

pa4080
  • 30,621