Linux / UNIX: Kill User Session

Posted on in Categories , last updated January 8, 2011

How do I kill all users session under Linux or UNIX operating system using command prompt?

You need to use the pkill command which will look up or signal processes based on name. It can send the specified signal (such as KILL) to each process. Open a command-line terminal (select Applications > Accessories > Terminal), and then type the following commands as root user:
$ sudo pkill -9 -u username
OR
# pkill -9 -u username
To list all users pids, enter:
$ pgrep -u username
OR better try:
$ ps -fp $(pgrep -d, -u userNameHere)

Example: Kill Unix / Linux User Session

In this example, list all process owned by a user called lighttpd, enter:
# ps -fp $(pgrep -d, -u lighttpd)
Sample outputs:

UID        PID  PPID  C STIME TTY          TIME CMD
lighttpd   4703     1  0 04:20 ?        00:01:07 /usr/sbin/lighttpd -f /user/local/etc/.myconf/lighttpd/master.example.com.conf
lighttpd   4705  4703  0 04:20 ?        00:00:00 /usr/bin/php-cgi
lighttpd   4708  4703  0 04:20 ?        00:00:00 /usr/bin/php-cgi
lighttpd   4710  4703  0 04:20 ?        00:00:00 /usr/bin/php-cgi
lighttpd   4712  4703  0 04:20 ?        00:00:00 /usr/bin/php-cgi
lighttpd   4714  4703  0 04:20 ?        00:00:00 /usr/bin/php-cgi
lighttpd   4715  4703  0 04:20 ?        00:00:00 /usr/bin/php-cgi
lighttpd   4716  4710  0 04:20 ?        00:00:07 /usr/bin/php-cgi
lighttpd   4718  4705  0 04:20 ?        00:00:00 /usr/bin/php-cgi
lighttpd   4719  4708  0 04:20 ?        00:00:02 /usr/bin/php-cgi

To kill all process owned by lighttpd user, enter:
# pkill -9 -u lighttpd
Please note that above command will also logout lighttpd user.

Getting help

Anytime you need assistance with Linux / UNIX pkill command-line, turn to the man page first. It will give you detailed information, parameters and switches for pkill command. For example, man pkill opens the man page for the pkill command:
$ man pkill

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

5 comment

    1. Which OS do you use?

      [root@squidhost ~]# ps -fp $(pgrep -d, -u root )
      UID PID PPID C STIME TTY TIME CMD
      root 1 0 0 04:06 ? 00:00:02 init [3]
      root 2 1 0 04:06 ? 00:00:00 [migration/0]
      etc…..

Leave a Comment