How do I get the list of the active login sessions?

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.


Here are few alternatives to get the list of the current session:

  • 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 the process numbers and then sudo kill -9 <session-process-number>:

$ 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   # this command will kill the third session from the above list

Additionally you could kill all processes of a specific user (reference):

sudo pkill -9 -u <username>

See also:

  • How do I find who is logged-in as root?

  • How do I list logged-in users without duplicates?