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?

  • 3
    How about shutdown -h now? ;)
    – dessert
    Oct 15 '17 at 21:00
  • Do you know of kick? Something like kick tty pts/* might come in handy…
    – dessert
    Oct 15 '17 at 21:05
  • 1
    @dessert I don't want to boot myself off, just everyone else
    – AJJ
    Oct 15 '17 at 21:05
  • @dessert Any comment on the script I wrote / posted as answer? Are there any gaping weaknesses in it?
    – AJJ
    Oct 15 '17 at 21:34
  • 1
    What users are you trying to kick off ? system users such a www-data ?
    – Panther
    Oct 16 '17 at 1:55

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."
  • 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

Wrote this script too which seems to work:



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

who | while read NAME REST
    if [[ $NAME != $ME ]]
        killall -u "${NAME}"
        if [[ "$?" = "0" ]]
            echo "Disconnecting ${NAME} from the system..."
            echo "Could not disconnect ${NAME} from the system..."
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.