- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Limiting "sudo kill" to killing only user processe...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2011 07:09 PM
тАО06-02-2011 07:09 PM
Has anyone come up with a way to allow a user to sudo kill but restricting it so it cannot kill system processes, etc? Basically I want the user to be able to kill only user processes.
Thanks
Solved! Go to Solution.
- Tags:
- sudo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2011 07:21 PM
тАО06-02-2011 07:21 PM
Re: Limiting "sudo kill" to killing only user processes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2011 07:23 PM
тАО06-02-2011 07:23 PM
Re: Limiting "sudo kill" to killing only user processes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2011 07:26 PM
тАО06-02-2011 07:26 PM
Re: Limiting "sudo kill" to killing only user processes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2011 10:01 PM
тАО06-02-2011 10:01 PM
Re: Limiting "sudo kill" to killing only user processes
The OS is set up to only let users kill processes they would have permissions to. Their own, stuff launched by their own group.
Give sudo kill they can of course do anything.
To have a granular kill, you need a script to take care of the decision to kill or not to kill.
Someone may want to write that script for you, but its a project and if I can't write the script in a few minutes, or have it in inventory, I usually refer you to a consultant (sometimes me).
I would in such a script check the process table and kill based on characteristics I find there.
I mean if you only want to kill user scripts a simple way is to check for root and other system users, those are system, and any other user have at it.
Give your users this power and they will crash something important. I can almost give you a warranty on that.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2011 10:55 PM
тАО06-02-2011 10:55 PM
Re: Limiting "sudo kill" to killing only user processes
What's your definition of a user process? Anyone that isn't root, lp or sfmdb?
Or a UID < 1000?
#!/usr/bin/sh
# Kill a list of PIDs and skip ones for users
# with UID < 1000
for pid in $*; do
uid=$(UNIX95=EXTENDED_PS ps -p $pid -ouid=)
if [ $uid < 1000 ]; then
echo "skip system process" 2>&1
continue
fi
kill $pid
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-13-2011 06:10 AM
тАО06-13-2011 06:10 AM
Re: Limiting "sudo kill" to killing only user processes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-13-2011 07:03 AM
тАО06-13-2011 07:03 AM
Re: Limiting "sudo kill" to killing only user processes
> Just need to put in "safe guards" to eliminate possible mistakes or work-arounds.
By setting UNIX95 (XPG4) behavior you have the ability to create custom 'ps' queries as the manpages document. As Dennis suggested, you could limit candidates to those whose UID is in an acceptable range. You might want to evaluate based on elapsed runtime and or combinations of parameters (e.g. uid, etime and command name).
WIth the 'UNIX95' behavior, selection by command name can be made "exactly" with the '-C' option:
# UNIX95= ps -C sh -opid -ouid= -oetime=
...which would return a list of 'sh' processes where the list consists of the 'pid', 'uid' and elapsed time without a heading (which is what the "=" suppresses. You could then further parse this output to collect a subset of pids to kill.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2011 06:07 AM
тАО07-20-2011 06:07 AM
Re: Limiting "sudo kill" to killing only user processes
Thank you for the responses. Would there be an easy way to limit the processes being killed to those being started by someone/something in a particular group (ie. only processes started by someone in group "users")? This would be a better (and safer) solution for me than limiting UID's to under 1000, etc. Thanks!!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2011 06:22 AM
тАО07-20-2011 06:22 AM
Re: Limiting "sudo kill" to killing only user processes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2011 06:23 AM
тАО07-20-2011 06:23 AM
Re: Limiting "sudo kill" to killing only user processes
@TheJuiceman wrote:Would there be an easy way to limit the processes being killed to those being started by someone/something in a particular group (ie. only processes started by someone in group "users")?
Well, again, using the UNIX95 behavior allows you to collect various views of *group* information. For that matter, the 'ps' can specify group numbers or names given in a 'gidlist' argument ('-G gidlist).
You should really look at the manpages for 'ps(1)'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2011 06:24 AM
тАО07-20-2011 06:24 AM
Re: Limiting "sudo kill" to killing only user processes
The man pages are your friend. Looking at "man (1) ps", you will see the "-G gidlist" option, which will "Select processes whose real groupd ID numbers or group names are given in gidlist"
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2011 07:23 AM
тАО07-20-2011 07:23 AM
Re: Limiting "sudo kill" to killing only user processes
The default properties of the OS protect system protect system processes from kill. I recommend a wrapper script as Patrick suggests.
Give sudo rights to the wrapper script not kill. Set the permissions very carefully on the script.
Regards,
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2011 08:40 AM
тАО07-20-2011 08:40 AM
Re: Limiting "sudo kill" to killing only user processes
Thanks. It looks like the "UNIX95= ps -G <group>" route may be the way to go for me.
How could I best write a "wrapper" so that when someone calls my "kill" script via sudo such as....
sudo kill 123 456 789 12345 ... ...
that the script will determine that processes "123", "456", etc are GID of the group I am allowing kill access and allow the kill?
Thank you again for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2011 08:46 AM
тАО07-20-2011 08:46 AM
Re: Limiting "sudo kill" to killing only user processes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2011 09:02 AM
тАО07-20-2011 09:02 AM
Re: Limiting "sudo kill" to killing only user processes
@TheJuiceman wrote:Thanks. It looks like the "UNIX95= ps -G <group>" route may be the way to go for me.
How could I best write a "wrapper" so that when someone calls my "kill" script via sudo such as....
sudo kill 123 456 789 12345 ... ...
that the script will determine that processes "123", "456", etc are GID of the group I am allowing kill access and allow the kill?
Hi:
Why do you need another script to call your script? I assume that you simply need an argument that denotes the GID of processes you want to kill. All you need do is collect the PIDs that match your criteria and issue a 'kill' for that list.
For that matter, if you are running 11.31, have a look at 'pkill(1)'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2011 09:35 AM
тАО07-20-2011 09:35 AM
Re: Limiting "sudo kill" to killing only user processes
Unfortunately, this is a 11.23 box.
You are correct. I just need a way to limit the kill command to executing against PID's that fit my GID criteria.
Example:
sudo scriptkill 123 456 7890
- Do these EXACT processes 123, 456, and 7890 exist?
- If so, do they meet the GID requirement?
- If so, perform a kill on these EXACT PID's, no wildcards or partial returns (ie. allow a kill on process "123" but not "1234" or "2123" ).
- If the criterias are not met, do not allow a kill.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2011 09:58 AM
тАО07-20-2011 09:58 AM
Re: Limiting "sudo kill" to killing only user processes
Hi (again):
@TheJuiceman wrote:You are correct. I just need a way to limit the kill command to executing against PID's that fit my GID criteria.
Example:sudo scriptkill 123 456 7890
- Do these EXACT processes 123, 456, and 7890 exist?
- If so, do they meet the GID requirement?
- If so, perform a kill on these EXACT PID's, no wildcards or partial returns (ie. allow a kill on process "123" but not "1234" or "2123" ).- If the criterias are not met, do not allow a kill
Leveraging the '-p PID' argument to first find candidates:
# ps -p <PID> -o pid= -o gid=
This gives you a list (in two columns) of PIDs and GIDs. If you like, you can fetch a list of processes like:
# ps -p 123,456,7890 -o pid= -o gid=
The "=" sign after each specification suppresses the header line making further parsing of the list easier.
Add any additional columns as necessary; walk the list; match what you want; extract the first field (column) for a PID to kill()'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2011 10:37 AM - edited тАО07-20-2011 12:08 PM
тАО07-20-2011 10:37 AM - edited тАО07-20-2011 12:08 PM
Re: Limiting "sudo kill" to killing only user processes
>- If so, do they meet the GID requirement?
Just take my script above and use "-ogid=" and then check for equality:
gid=$(UNIX95=EXTENDED_PS ps -p $pid -ogid=)
if [ "$gid" != some-magic-gid ]; then
echo "skip wrong group process" 2>&1
continue
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2011 08:58 AM
тАО07-21-2011 08:58 AM
Re: Limiting "sudo kill" to killing only user processes
You guys are the best!!!!
This is really close now. Just one last piece.....is there a way to prevent wildcards? I would like to prevent someone from executing something like "sudo scriptkill * " or "sudo scriptkill abcd". The latter returns a nasty message. The wildcard, however, is returning a lot of "interesting" possiblities....yikes!!! Is there a way to restrict the input to accept only numerical entries and no wildcards?
Thanks again!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2011 09:06 AM
тАО07-21-2011 09:06 AM
Re: Limiting "sudo kill" to killing only user processes
Apparently I should have asked this way back at the beginning of this, but do you really want general users to be able to kill of the process of other general users? I think you may end up with a revolt on your hands.
If not, then why re-write what the kill command itself already restricts them to? As is, they can kill their own processes.
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2011 09:31 AM
тАО07-21-2011 09:31 AM
Re: Limiting "sudo kill" to killing only user processes
@TheJuiceman wrote:Just one last piece.....is there a way to prevent wildcards? ... Is there a way to restrict the input to accept only numerical entries and no wildcards?
Hi:
You might use this as an intialization step to valid and otherwise reduce the input arguments to only numeric values:
#!/bin/sh typeset LIST='' for PID in $@ do if [ $(expr "${PID}" : '[0-9]*') -ne $(expr "${PID}" : '.*') ]; then echo "PID value = '${PID}' is invalid" else LIST=$(echo ${LIST} ${PID}) fi done echo "Using: ${LIST}" exit 0
This would look like:
./pidlist 123 456 a 7fff 7890 PID value = 'a' is invalid PID value = '7fff' is invalid Using: 123 456 7890
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2011 10:24 AM
тАО07-21-2011 10:24 AM
SolutionThank you for the help everyone!!!
I have a script that looks like it would work. The reason for the need for such a script is for some developers. They occassionally will need to kill a user process when testing or troubleshooting. This will allow them to do it without putting the system in harms way.
The group ID that is applicable to what is needed is "20". Below is what has been assembled by your contributions:
#!/usr/bin/sh
for pid in $*; do
if [ $(expr "${pid}" : '[0-9]*') -ne $(expr "${pid}" : '.*') ]; then
echo "Variable constraint is invalid. "
echo "You must use a numeric process ID with this command" && exit
fi
ps -p $pid >/dev/null 2>&1
[[ $? != 0 ]] && echo "Process doesn't exist: $pid" && continue
gid=$(UNIX95=EXTENDED_PS ps -p $pid -ogid= )
if [ $gid != "20" ]; then
echo "Cannot kill system process: $pid" 2>&1
continue
fi
kill $pid
done
Testing by echoing the $pid and trying various possible variables looks promising. I have it terminating if it picks up a non-numeric entry, which seems to work better than letting it continue. What do you all think? See any problems or gotchas?
Thanks again for your help!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2011 11:00 AM
тАО07-21-2011 11:00 AM
Re: Limiting "sudo kill" to killing only user processes
@TheJuiceman wrote:Thank you for the help everyone!!!
I have a script that looks like it would work.
Below is what has been assembled by your contributions:
What do you all think? See any problems or gotchas?
Hi (again):
This looks reasonable. Instead of :
[[ $? != 0 ]] && echo "Process doesn't exist: $pid" && continue
...my preference (in part for clarity) would be:
[[ $? != 0 ]] && { echo "Process doesn't exist: ${pid}"; continue; }
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2011 05:54 PM
тАО07-21-2011 05:54 PM
Re: Limiting "sudo kill" to killing only user processes
And one last refinement:
[[ $? != 0 ]] ...
vs
[[ $? -ne 0 ]] ...
The comparison != is for strings, while -ne is for numbers. For simple cases like this, the two methods produce the same results. But consider magnitude comparisons where 12 is less than 9 when comparing strings. By using the numeric comparisons (-lt -le -eq -gt -ge -ne) errors with variables that have non-numeric characters will be caught rather than producing an unpredictable result.
Bill Hassell, sysadmin