LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 04-02-2005, 07:52 PM   #1
houler
Member
 
Registered: Mar 2005
Distribution: Slackware 10.1, Kernel 2.6.14.4 (custom)
Posts: 166

Rep: Reputation: 30
How do I limit the amount of processes a user can run?


for eg.

Say I (root) want user "kung" to only run 2 processes, and I want user "foo" to run 5 processs. How do I do this on Slackware 10?

I know in some distros of linux you can do this by editing /etc/security/limits.conf but slackware doesn't seem to have this feature
 
Old 04-02-2005, 08:08 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Use the ulimit command to set both the hard and soft limit for those users in the system profile for the shell used by those users. For example (of course these process limits are unreasonably small, but consistent with your question):

Code:
if [ "`/usr/bin/id -un`" == "kung" ]
then
   ulimit -u 2
fi
if ["`/usr/bin/id -un`" == "foo" ]
then
   ulimit -u 5
fi
The hard limit will prevent the user from increasing the limit set.
 
Old 04-02-2005, 08:15 PM   #3
houler
Member
 
Registered: Mar 2005
Distribution: Slackware 10.1, Kernel 2.6.14.4 (custom)
Posts: 166

Original Poster
Rep: Reputation: 30
Thanks for your reply.

BTW what's a good ulimit number for "root"? I just read an article here about a forkbomb crashing many linux distros that didn't have ulimit set to a sufficient number.


EDIT:

I'm sort of a noob...what file should I save this to and where exactly should I put it? permissions?


Shell is bash.

Last edited by houler; 04-02-2005 at 08:19 PM.
 
Old 04-02-2005, 08:32 PM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
For bash, you can place the code in /etc/profile. A good limit is high enough not to interfere with your normal activity (whatever that is). For my machines, I use a limit of 256 for all users, including root. Smaller machines can probably get by with a lower value, while large servers will need a correspondingly large process limit. One size does not fit all.
 
Old 04-02-2005, 08:43 PM   #5
houler
Member
 
Registered: Mar 2005
Distribution: Slackware 10.1, Kernel 2.6.14.4 (custom)
Posts: 166

Original Poster
Rep: Reputation: 30
I see...Ok I modified the code snippet and put it in the last part of /etc/profile

# ulimit for user
if [ "`/usr/bin/id -un`" == "kung" ]
then
ulimit -u -H 2
fi

I'm running slack on a:

466Mhz Intel Celery Proc w/ 128MB of ram


--EDIT--

I guess it's trial and error...

Last edited by houler; 04-02-2005 at 08:55 PM.
 
Old 04-03-2005, 12:44 AM   #6
houler
Member
 
Registered: Mar 2005
Distribution: Slackware 10.1, Kernel 2.6.14.4 (custom)
Posts: 166

Original Poster
Rep: Reputation: 30
I seem to be having trouble with this script. It won't run, when I login as the user. It won't even run when I execute it as the user.


Another weird thing:

I can't find the executable for ulimit anywhere. I did a find / -name ulimit, which ulimit, and a whereis ulimit. I can't find the file! But when I run ulimit by itself it executes just fine.
 
Old 04-03-2005, 05:27 AM   #7
SiegeX
Member
 
Registered: Jul 2004
Location: Silicon Valley, CA
Distribution: Slackware
Posts: 171

Rep: Reputation: 38
Quote:
Originally posted by houler

I can't find the executable for ulimit anywhere. I did a find / -name ulimit, which ulimit, and a whereis ulimit. I can't find the file! But when I run ulimit by itself it executes just fine.
This is because 'ulimit' is a bash builtin not an executable. You can check this by running the command type ulimit. As far as the code goes, try the following one, its very similar but I dont rely on the presence of the 'id' binary to make it work as the $USER environment var does the same thing and is guarenteed to be there if you run bash.

Code:
if [[ "$USER" == "root" ]]; then
   ulimit -u 512
else
   ulimit -u 256
fi
As you can probably tell this allows you to set root's ulimits a bit higher from non-root users. Just toss that into /etc/profile then re-login for it to take effect or simply run source /etc/profile if you dont want to log out.
 
Old 04-03-2005, 08:11 AM   #8
egag
Senior Member
 
Registered: Jul 2004
Location: Netherlands
Distribution: Slackware
Posts: 2,721

Rep: Reputation: 53
limiting the total number of processes won't help.
say, you limited the amount of processes for yourself to 256.

after you start the forkbomb, there's no way to stop it anymore,
because you cannot do a "killall " or " su " to root to stop it ( " no more resources " )

..or is there a way... ?

edit:.don't bother....just do a ctl-alt-f6 and login as root.

egag

Last edited by egag; 04-03-2005 at 08:19 AM.
 
Old 04-03-2005, 08:38 AM   #9
chbin
Member
 
Registered: Mar 2005
Distribution: slackware-current
Posts: 379

Rep: Reputation: 31
Quote:
you cannot do a "killall " or " su " to root to stop it ( " no more resources " )
Good point!

On a standalone desktop you really don't have to worry about it. Only usefull if you are running a server and you've given out accounts to other people. And if you don't trust the people enough then you shouldn't be giving them accounts on your box in the first place. If they do take advantage of the account you can just take the account away from them.
 
Old 04-03-2005, 08:47 AM   #10
chbin
Member
 
Registered: Mar 2005
Distribution: slackware-current
Posts: 379

Rep: Reputation: 31
Just saying becuase in order for the fork bomb to be used on your box the person has to have an account on you box. In reality the could just chuck some app like firefox in a for loop and keep opening up new ones and get the same effect. Now in reallity if that did happen I assume their account on your box would be revoked indefinetely . In they can't do any permanent damage because they can't use it to gain any kind of elevated privalages so why worry about it.
 
Old 04-03-2005, 09:12 AM   #11
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
To limit execution of a large app in a loop, you can use memory limits as well:

# No core files by default
ulimit -c 0 > /dev/null 2>&1
# 768MB virtual memory limit
ulimit -v 786432 > /dev/null 2>&1
# 384MB resident memory limit
ulimit -m 393216 > /dev/null 2>&1
# 256 max processes per user
ulimit -u 256 >/dev/null 2>&1

Killing the loops isn't a problem. Many will fail on their own once they hit the resource limit. Those that don't can be killed from another login session (the limits are per session). System performance won't be an issue, unless you've set the limit too high. If neither -H nor -S is specified, both the soft and hard limits are set. The documentation for ulimit is in the bash man page (man bash).
 
Old 04-03-2005, 09:17 AM   #12
egag
Senior Member
 
Registered: Jul 2004
Location: Netherlands
Distribution: Slackware
Posts: 2,721

Rep: Reputation: 53

@chbin
yes, that's true, but i was thinking about smth. like a forkbomb could happen
when i just try a ( bad ) script i wrote myself.
i do not have much experience with script-writing and Murphy's law being valid, it could happen.

but now i know a way to stop it....

egag

Last edited by egag; 04-03-2005 at 09:20 AM.
 
Old 04-03-2005, 09:22 AM   #13
egag
Senior Member
 
Registered: Jul 2004
Location: Netherlands
Distribution: Slackware
Posts: 2,721

Rep: Reputation: 53
@macemoneta
the script won't fail when it hits the limit, it just keeps on trying.
but limit memory could be an extra safety.

egag
 
Old 04-03-2005, 09:32 AM   #14
chbin
Member
 
Registered: Mar 2005
Distribution: slackware-current
Posts: 379

Rep: Reputation: 31
since were on the topic of ulimits, is there a way to limit the cpu time of a particular process or particular user? I'm assuming there has to be, never tried it though.
 
Old 04-03-2005, 09:41 AM   #15
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
It depends on the nature of the application. For example, this fork bomb will fail:

:(){ :|:&};:

That expands to:

Code:
xyz() {
   xyz | xyz &
}
xyz
So it's basically creating a subroutine that invokes two copies of itself recursively, then it invokes the subroutine.

When executed with a process limit, it will halt forking at the limit, and all the recursive invocations will end. True fork bombs tend not to try to do real work (since that would limit their effectiveness at filling the system with processes). The more real work (or processing delays) the less likely that the processes will self terminate. As process slot are opened the existing looping processes will fill them. You can find who's running the loop with:

/bin/ps -Afl | /bin/awk '{print $3}' | /bin/sort | /usr/bin/uniq -c | /bin/grep -v UID | /bin/sort -n

Then kill that user's processing with:

skill -u username
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I create an user to run processes (ONLY)? eantoranz Linux - Security 4 01-04-2005 07:03 PM
Limit the number of background processes per user... trendwhore Linux - Security 6 08-27-2004 01:09 PM
Is it possible to limit the amount of characters on members' profile? Mega Man X LQ Suggestions & Feedback 9 05-16-2004 01:46 PM
limit the amount of bandwidth used by certain protocols chrisfirestar Linux - Networking 4 03-22-2004 09:12 PM
Limit amount of emails on server ganninu Linux - Networking 7 07-30-2003 11:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 02:15 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration