4

I know if I type w I can get a list of all logged in users.

I also know that killall -u USERNAME kicks all processes associated with the user USERNAME (including bash / their shell, their sshd process, etc). As far as I know this is how you're supposed to "kick a user off" your server.

However I don't know how to kick off all users. Do I have to somehow w | cut -d' ' -f1 to get a list of users (and then somehow strip off the blank line and USER field and your own username) and feed that to the -u command for killall?

Or is there a better way?

AJJ
  • 902

2 Answers2

3

You can kill'm all with:

who | awk '$1 !~ /root/{ cmd="/sbin/pkill -KILL -u " $1; system(cmd)}'

You need to be root or use sudo.

Mind that killing all users can be dangerous and damage your file system. Plus annoy your users; what do you expect to happen if one of them is doing something like mysql maintenance when you kill them?

You really should use ps -ef | grep "user" or something similar to inspect their processes.

Why not warn them instead?

shutdown -h +10 "Server is going down 10 minutes. Save your work and logout."
Rinzwind
  • 309,379
1

Wrote this script too which seems to work:

#!/bin/bash

ME=$SUDO_USER

if [[ -z $ME ]] 
then
    echo "Must run script using sudo."
    exit 1
fi

who | while read NAME REST
do
    if [[ $NAME != $ME ]]
    then
        killall -u "${NAME}"
        if [[ "$?" = "0" ]]
        then
            echo "Disconnecting ${NAME} from the system..."
        else
            echo "Could not disconnect ${NAME} from the system..."
        fi
    fi
done
AJJ
  • 902