Force logout a user

62

26

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?

Mithun Sreedharan

Posted 2011-11-18T09:51:18.337

Reputation: 1 485

9You've got an even bigger issue to resolve. Disable direct root logins, and force your users to use sudo. – Xenoactive – 2011-11-20T18:38:34.590

Answers

57

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

Fabel

Posted 2011-11-18T09:51:18.337

Reputation: 1 093

5Just 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. – TheDudeAbides – 2018-07-25T18:59:43.433

10And then kill that process using kill -9 <processid> – Mithun Sreedharan – 2011-11-23T05:05:36.690

26

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:

Niranjan Singh

Posted 2011-11-18T09:51:18.337

Reputation: 1 653

Make sure that the user doesn't have the same UID as the root user or else you'll kick yourself out, too. – Steropes – 2014-10-15T00:24:57.523

@pjammer It's not that you "weren't root after all". Consider what you are doing. You are logging in as user joe, this creates a shell process, say it's bash with pid 123. Then you spawn the sudo process which spawns a su process, which spawns a login shell for user root. On that shell you are calling a command that kills all processes belonging to user joe, including PID 123 and all it's children: sudo, su, the login shell for root. Once you understand what it is you are doing you'll see it's not possible by definition. Of course, there are alternatives as Melebius pointed out. – GnP – 2015-10-15T18:42:37.860

2The 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 – 2018-07-23T12:06:59.450

6what if I am logged in as the same user? – Mithun Sreedharan – 2011-11-18T10:37:27.263

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.. – Niranjan Singh – 2011-11-18T10:44:34.377

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 – 2012-09-21T18:28:49.490

5@Mithun You can use -t <terminal name> instead of -u. – Melebius – 2014-06-03T06:42:08.947

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*

collisio

Posted 2011-11-18T09:51:18.337

Reputation: 1