Linux: Log Everyone Out Of The System

I can login as root user on Debian or Ubuntu/RHEL/CentOS Linux based system. I need to log everyone off (all ssh users) to install new kernel and/or hardware. How do I do this on Linux? What is the best way to logout ALL USERS remotely over the ssh based session in Linux like operating systems?

You need to use the following commands:
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements pkill
Est. reading time 10 minute
a] who or w command – Show who is logged on and what they are doing.

b] pkill command – Kill user session and forcefully logout of the system.

c] shutdown command – Arranges for the system to be brought down in a safe way.

Examples

Use the who command to see list of logged in users as follows:
# w
OR
# who
Sample outputs:

root     pts/0        Jul 29 13:53 (10.1.6.120)
nixcraft pts/1        Jul 29 12:30 (10.1.6.121)
sailee   pts/2        Jul 29 12:33 (10.1.6.121)

To force and logout nixcraft and sailee user, enter:
# pkill -KILL -u nixcraft
# pkill -KILL -u sailee

Alternatively, just try bash and friends kung-fu and save time:

 ### warning must be run as root or via sudo ###
who | awk '!/root/{ cmd="/sbin/pkill -KILL -u " $1; system(cmd)}'

OR

 ### warning must be run as root or via sudo ###
### Safe version :) ###
who | awk '$1 !~ /root/{ cmd="/sbin/pkill -KILL -u " $1; system(cmd)}'

Finally, you can shutdown the system as follows:
# shutdown -h now

Instead of killing all users one by one you can type the following shutdown command with the warning message:
# shutdown -h +10 "Server is going down for maintenance in 10 minute. Please save ALL your work ASAP and logout of the system."

Please note that this method will not work with ftp/smtp/pop3 and all other user accounts on the server. I recommend that you set maintenance windows for your server when network traffic is at a minimum or when users/client computers were not engaged in other activities on the server. For example, week-end or the period between midnight and 3:00 a.m. can be set as maintenance windows for your system.

Recommended readings
  • See man pages – shutdown(8)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 6 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig 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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
6 comments… add one
  • Hamid Teymouri Jul 29, 2013 @ 20:31

    Thank you….I enjoyed reading this article tonight :D….

  • Morteza Jul 30, 2013 @ 4:31

    Thanks. it’s useful.

  • OmiPenguin Jul 30, 2013 @ 10:14

    Can someone explain what this kungfu method is doing. Specially system(cmd)}’ and why $1 is added in in the beginning of second Kungfu command

  • bmclark Jul 30, 2013 @ 16:30

    Couldn’t you just send out the message and change the runlevel?

  • Cicuta Jul 30, 2013 @ 18:42

    I have done maintenance of UNIX systems for a long time and I have always done it after 6 PM during the week or on weekends; however, I always send a wall message which is advertised to every user letting them know about the system maintenance and to save everything they want and log out, say…30 minutes before the schedule shutdown. I also coordinate with the other departments; so they take proper action. Then, when the time comes to take action I use init 0 which does an orderly shutdown immediately.

    All you need is a wall message an init 0.

    • bmclark Jul 30, 2013 @ 20:28

      That was, exactly, my thought. Glad to know it works in practice!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum