71

I When I logged into the machine as root and typed who to see which users are logged in, I found somebody else too logged in as root

devuser   pts/0        2011-11-18 09:55 (xxx.xxx.xxx.xxx)
root      pts/1        2011-11-18 09:56 (xxx.xxx.xxx.xxx)
testuser  pts/2        2011-11-18 14:54 (xxx.xxx.xxx.xxx)
root      pts/3        2011-11-18 14:55 (xxx.xxx.xxx.xxx)

How can I force a root user at pts/3 to logout?

1
  • 11
    You've got an even bigger issue to resolve. Disable direct root logins, and force your users to use sudo.
    – Xenoactive
    Nov 20, 2011 at 18:38

3 Answers 3

67

You terminate a session by killing its parent process, called the session leader. Find out which process it is with:

ps -dN|grep pts/3
3
  • 12
    And then kill that process using kill -9 <processid> Nov 23, 2011 at 5:05
  • 10
    Just kill <pid> should be sufficient, right? Please save the kill -9s for badly misbehaving processes that don't respond to INT, HUP, or TERM; it's kind of like the difference between shutting down a computer using the OS's menu system vs. pulling the plug on the computer.
    – Kevin E
    Jul 25, 2018 at 18:59
  • @TheDudeAbides I would try to use -15 for the TERM signal. It works for me.
    – Torrien
    Jun 11, 2021 at 21:23
40

To kill and logout a user, you can send KILL signal. Type the following command:

# skill -KILL -u vivek

Check these links for more information:

7
  • 10
    what if I am logged in as the same user? Nov 18, 2011 at 10:37
  • these command works for super user.. you must log in as super user.. network operating systems follow this approach for security.. i think those links are also saying same to login as admin.. Nov 18, 2011 at 10:44
  • it kicked me out as sudo su - too, even though i was root, but i guess i wasn't in the end. going back into the server showed me as the only one. i had 4 instances of myself, i guess I would say.
    – pjammer
    Sep 21, 2012 at 18:28
  • 5
    @Mithun You can use -t <terminal name> instead of -u.
    – Melebius
    Jun 3, 2014 at 6:42
  • 5
    The cyberciti link now says "WARNING! These tools are obsolete, unportable and it is here due to historical reasons. Consider using the killall, pkill, and pgrep commands instead as follows." pkill -KILL -u vivek works just as well.
    – EM0
    Jul 23, 2018 at 12:06
0

Improving a bit Fabel's answer above:

\# *for pid in $(for ptsn in $(w | grep **user_name** | grep pts | awk '{print $2}'); do ps -dN | grep "$ptsn " | awk '{print $1}' ; done); do kill -9 $pid; done*

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .