Linux / UNIX: Kill User Session

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

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • Paul Mostertman Jan 9, 2011 @ 12:02

    skill -STOP -u lighttpd will also work :-)

  • Paul Mostertman Jan 9, 2011 @ 12:07

    Also don’t forget to create a temporary /etc/nologin if you don’t want users to be able to login on the system again

  • dave Jun 27, 2012 @ 0:37

    do not follow these posts, they will require someone to go there and phsyically reboot the machine

    sudo pkill -9 -u username

  • Terry Sep 5, 2014 @ 3:46

    Should be
    ps -ef -p $(pgrep -d, -u username)

    ps -pf will cause syntax error.

    • Paul Mostertman Sep 6, 2014 @ 19:27

      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 Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.