3

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
  • 852
  • 2
  • 12
  • 21

2 Answers2

2

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
  • 293,910
  • 41
  • 570
  • 710
  • 1
    I am not familiar with `awk`, what is that doing? I assume `$1` refers to the first field / the username – AJJ Oct 15 '17 at 21:10
  • Yes it gets you the 1st column from the command before it (in this case who, so the user) – Rinzwind Oct 15 '17 at 21:14
  • What happens if a user has several sessions opened? How about `who | awk '$1 !~ /root/ && l != $1 { l = $1; cmd="/sbin/pkill -KILL -u " $1; system(cmd) }`? – dessert Oct 15 '17 at 21:15
  • 1
    @Rinzwind If I shut down the machine then I can't do anything in there either. What I wanted to do was create `/etc/nologin` file so PAM would prevent logins, and then use `wall MESSAGE` to send a warning message to everyone, then kill all other users. Then I could delete the `nologin` file to re-enable logins. – AJJ Oct 15 '17 at 21:36
  • "Mind that...." Well said! – Elder Geek Oct 16 '17 at 15:47
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
  • 852
  • 2
  • 12
  • 21
  • `who | awk 'l!=$1{l=$1;print $1}'` ( or `who | awk '{print $1}' | uniq`) sorts out usernames that occur multiple times and only outputs them, so no need for `REST`. – dessert Oct 15 '17 at 21:43
  • Looks like I need to learn `awk` – AJJ Oct 15 '17 at 21:45
  • It's worth the time, guaranteed. – dessert Oct 15 '17 at 21:47
  • 1
    I'd rather do `echo -n "Disconnecting ${NAME} …"; killall … && echo " Success." || echo " Fail."`, the output is much clearer this way and it saves an `if` statement. – dessert Oct 15 '17 at 21:51