LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices

Reply
 
LinkBack Search this Thread
Old 11-15-2006, 05:09 PM   #1
Skywing
LQ Newbie
 
Registered: Mar 2005
Distribution: SuSE 10.1
Posts: 16

Rep: Reputation: 0
Log off all users other than root


Hey everyone, I'm having a bit of trouble with this task ive been given. I basically need a way to log out every user logged into the system other than root, I dont need it to target any set of specific users, just every single one OTHER than root. All the users are using BASH and this is in RHEL4 if that makes any difference, and I'm sure that this can be done with a simple script but I've been killing myself over trying to get this done. Can anyone PLEASE be of some assistance?
 
Old 11-15-2006, 05:30 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Task or homework?
 
Old 11-15-2006, 05:40 PM   #3
Skywing
LQ Newbie
 
Registered: Mar 2005
Distribution: SuSE 10.1
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by digiot
Task or homework?
task, unfourtinately it has been passed down to me since im the only person that has even the slightest bit of Linux knowledge. After searching around for a bit would doing

Code:
skill -KILL -v /dev/pts/*
do what im looking for?
 
Old 11-15-2006, 05:58 PM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You could get a list of logged in users using the "who -u" command.
 
Old 11-15-2006, 06:01 PM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by Skywing
task, unfourtinately it has been passed down to me since im the only person that has even the slightest bit of Linux knowledge.
Ah, okay.

Quote:
After searching around for a bit would doing

Code:
skill -KILL -v /dev/pts/*
do what im looking for?
Don't think so - you're looking to log people out, which would usually be from a tty and there's nothing there to exclude root.

To be honest, I don't know the answer. I feel like there should be something simple and direct like the skill example. Best thing I can think of (excuse my horrible awk) would be
Code:
#!/bin/bash
ps aux | awk '
! /^root/ {
    if ($11 ~ /-bash/)
        print $2
}
' | xargs kill # maybe with a '-9'
Untested and kinda rude, but I think it might do it. Probably a way to do it with just ps flags and kill.

-- Just saw jschiwal's post in the preview screen - so maybe who -u | awk '! /root/{ print $6 } | xargs kill
 
Old 11-15-2006, 06:12 PM   #6
Skywing
LQ Newbie
 
Registered: Mar 2005
Distribution: SuSE 10.1
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by digiot
Ah, okay.



Don't think so - you're looking to log people out, which would usually be from a tty and there's nothing there to exclude root.

To be honest, I don't know the answer. I feel like there should be something simple and direct like the skill example. Best thing I can think of (excuse my horrible awk) would be
Code:
#!/bin/bash
ps aux | awk '
! /^root/ {
    if ($11 ~ /-bash/)
        print $2
}
' | xargs kill # maybe with a '-9'
Untested and kinda rude, but I think it might do it. Probably a way to do it with just ps flags and kill.

-- Just saw jschiwal's post in the preview screen - so maybe who -u | awk '! /root/{ print $6 } | xargs kill
alright, thanks guys, ill give these a try and report back.
 
Old 11-15-2006, 07:07 PM   #7
Skywing
LQ Newbie
 
Registered: Mar 2005
Distribution: SuSE 10.1
Posts: 16

Original Poster
Rep: Reputation: 0
well i tried those and none worked, but i found another possible solution, if i do the following

Code:
ps -ef | grep -v root
itll list every process that ISNT being run by root, so how can i go about telling the system to kill every process that it finds isnt being run by root?
 
Old 11-15-2006, 07:31 PM   #8
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Well, the PID is in the second field, so extract that and kill it. Not sure it's necessary to kill every process, since kicking out the parent should take down the rest, but either way.

I have a couple-three boxes I could test it on, but I've got too many things open I don't want to lose my place in. What happened with the above commands? Because that's essentially what they do - isolate the PIDs and attempt to kill them. How they failed might be instructive. Failed with 'kill -9', too?
 
Old 11-15-2006, 07:58 PM   #9
Skywing
LQ Newbie
 
Registered: Mar 2005
Distribution: SuSE 10.1
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by digiot
Well, the PID is in the second field, so extract that and kill it. Not sure it's necessary to kill every process, since kicking out the parent should take down the rest, but either way.

I have a couple-three boxes I could test it on, but I've got too many things open I don't want to lose my place in. What happened with the above commands? Because that's essentially what they do - isolate the PIDs and attempt to kill them. How they failed might be instructive. Failed with 'kill -9', too?
crap, forgot to try with the -9

im a bit of a n00b when it comes to scripting but how do i go about extracting and killing the second field?
 
Old 11-15-2006, 08:40 PM   #10
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
There are probably a zillion ways - one is basically like I had it above (since awk is a bit large but very handy with tabular data due to its field handling):
ps h -ef | grep -v root | awk '{ print $2 }' # print the second field of the input records (lines)
or
ps -ef | egrep -v 'root|UID' | awk '{ print $2 }'
or dispensing with grep
ps h -ef | awk '! /^root/{ print $2 }'
which gets us basically back to where we were. Could also use various 'cut's and 'sed's and so on, I guess. If that doesn't get it, maybe this isn't the right route at all - something to do with tackling getty or init directly. Why does root need to stay logged on? A lot more would go offline than just the users, but just doing a 'telinit 1' almost seems like the handiest, though a bit overkill.
 
Old 11-15-2006, 08:42 PM   #11
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
If you are looking at processes, be sure you don't also kill system process. There are also system users.
You can use the "users" command to get a list of users who are currently logged in. The names are not uniq however, as a user may have more than one shell going.
This will give you a list of unique non-root users:
users | tr ' ' '\n' | sort | grep -v root | uniq

I think that if you sent a SIGHUP signal to the user process with a pts or tty in the processes list that would do what you want.

This will supply a list of such processes:
for username in $(users |tr ' ' '\n' |grep -v root|sort|uniq); do echo "user: $username"; ps -u $username; done | grep -e pts -e tty | cut -f2 -d' '

This might do the trick:
Code:
for process in $( for username in $(users |tr ' ' '\n' |grep -v root|sort|uniq); do ps -u $username; done | grep -e pts -e tty | cut -f2 -d' ' );
do
  kill -SIGHUP $process
done
 
Old 11-15-2006, 09:01 PM   #12
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by jschiwal
If you are looking at processes, be sure you don't also kill system process. There are also system users.
Good catch. I was thinking of that originally when I was trying to kill '-bash' login shell processes. That would remove system processes from the equation. But as the thread wandered, I forgot.
 
Old 11-16-2006, 08:04 AM   #13
Skywing
LQ Newbie
 
Registered: Mar 2005
Distribution: SuSE 10.1
Posts: 16

Original Poster
Rep: Reputation: 0
wow, thanks for all the help guys, I really appreciate it, I will give all these a try once I get back to the location tonight and report back results.
 
Old 11-16-2006, 02:08 PM   #14
Skywing
LQ Newbie
 
Registered: Mar 2005
Distribution: SuSE 10.1
Posts: 16

Original Poster
Rep: Reputation: 0
I havent tested it yet, but I figured I would ask anyway, would the following suffice?

Code:
ps -ef | grep bash | grep -v grep | grep -v root | awk '{system("kill -n 9" $2)}'
 
Old 11-16-2006, 02:50 PM   #15
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
That would leave you open to killing system processes as jschiwal pointed out. Also, if you have pgrep, try using that. It never matches itself, so you don't have to 'grep -v grep'. Also, awk can match ^root, so you could dispense with that 'grep -v'. And awk will concatenate the system function - think it'd need to be "kill -n 9 " (note space).

All this is dancing around the same thing, though - if any of this would work, I don't see why the first suggestion wouldn't. And if it didn't, it seems likely these would fail, too.

Which got me to thinking: I just created a user 'test' and logged 'him' into a second X session and ran my first script as root, just modified to spare both me and root.
Code:
#!/bin/bash
ps aux | awk '
! /^root|^j/ {
    if ($11 ~ /-bash/)
        print $2
}
' | xargs kill -9
I'm still here and root's still here, but 'test' is knocked out. So you must not have been running the script as root or made a typo or something, because it works here.

-- Actually, a plain 'kill' doesn't seem to work, but a 'kill -9' is probably literally overkill. It left an X lock file behind on one test. (I was running X just for a simple way to have multiple processes to knock out above the login shell.) So this may be better (final take (I hope )):
Code:
ps aux | awk '! /^root/{ if ($11 ~ /-bash/) print $2 }' | xargs kill -1
Seems to work and seems to let processes clean up.

Last edited by slakmagik; 11-16-2006 at 03:34 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
No Users Can Log In; Only Root Can JWilliamCupp Linux - Software 8 02-07-2012 12:54 AM
root users won't log out aubrey-calm2 Linux - Newbie 2 04-08-2006 01:48 AM
root can log in, but not users walterbyrd Debian 7 02-08-2006 06:17 AM
Users can't log in unless part of the root group jeffreybluml Linux - Newbie 3 12-02-2004 07:24 PM
non-root users can't log in (RH9) skullmunky Linux - Security 1 11-17-2004 11:41 AM


All times are GMT -5. The time now is 06:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration