Now let's look at managing user resource limits. And Linux,
when we have a lot of users logging into the system,
we may want to impose limits on what resources they can use.
That way.
We can ensure for example,
that a user does not use 80% of the CPU leaving very little despair for others.
Now to set such a limit, we can edit this file and it's located at Etsy security
limits.com.
We'll see a lot of comments in that file explaining how we should use it,
the syntax required and so on.
Now, if we move further down in the file, we'll see the syntax for setting a limit
and we can observe that the syntax is
domain
type
item
and then value, let's break this down into easy to understand parts first,
the domain.
Now what can we specify here? Usually one of these three things,
a user name. In this case, we simply type the name of the user such as Trinity
or we can use a group name
to set a limit for everyone in the developers group.
We just add an at sign in front of its name.
So we'd write at developers to set such a limit for the developers group.
And the asterisk will match all
setting a limit for asterisk basically says set
this limit for every user on the system.
So it's a way to set a default limit
note.
However,
that this limit will only apply to every user that is not mentioned in this list.
A user specific limit overrides the global asterisk limit.
For example,
one asterisk limit can specify that everyone can only launch 10 processes.
But then another limit for the user. Trinity says that she can launch 20 processes.
In this case, the limit for everyone will be 10, the default one.
But for Trinity, it will be set to 20.
Next is the type field which can take three different values
hard, soft and then a dash,
a hard limit cannot be overridden by a regular user.
If a hard limit says they can only run 30 processes, they can go above that.
It's basically the top the max value of a resource that someone can use.
And here's an example for Trinity,
a soft limit. On the other hand is different
instead of an absolute maximum value. This is more like the startup limit.
The initial value for the limit when the user logs in
if a user has a soft limit of 10 max processes and a hard limit of 20.
The following happens
when they log in the limit will be set to 10 processes.
But if the user has some temporary need to increase this.
They can raise it to 1112, 15 or 20 processes.
This way, they can get a slight increase when it's absolutely required.
So they can manually raise it to anything they need, but never above the hard limit.
It basically gives them a little bit of extra breathing room when they need it.
And last, we have the dash sign,
this specifies that this is both a hard and a soft limit with this.
We're saying Trinity should be able to run 20 processes at most.
When she logs in,
she should be able to use up her entire
allocation without needing to manually raise her limit.
Next up the item value.
This decides what this limit is for.
We can have things such as
N proc
N proc sets the maximum number of processes that can be opened in a user session
size, sets the maximum file size that can be created in this user session.
The size is in kilobytes.
So 1024 here means the maximum file size is 1024 kilobytes,
which is exactly one megabyte.
CPU sets a limit for the CPU time. And this is specified in minutes
when a process uses 100% of AC pu core for one second,
it will use up one second of its allocated time.
If it uses 50% of one core for one second,
it will use up 0.5 seconds of its allocation.
Even if a process was opened three hours ago,
it might have only used two seconds of CPU time
because that's the time it actively utilized the CPU,
the rest of the time it was sitting idle, basically sleeping and not using the CPU.
If you want to see more stuff that can be limited,
just consult the user manual for this limits.com file with Man
limits.com.
Now let's test our knowledge and add a limit for our user called Trinity
to ensure that she can open up a maximum number of three processes.
So under this line, we will add this,
make sure there's no pound sign at the beginning of this line
that would make it a comment
and the Vim editor might automatically add it when
you press enter to add a new line here,
make sure to delete the preceding pound sign.
Otherwise the line will be commented and have no effect.
Let's save our file and exit.
And at this moment, only one process is permanently running in recession,
the bash shell.
So we should be able to run two or more processes.
Let's launch PS and pipe the output to the less pager.
We can see that it works and it got us to running three processes,
which is the max limit.
Now, what would happen if we tried to launch the fourth?
Let's press Q to quit the less pager and then try the following command.
Now, this would try to launch three new processes, LS grep and less
plus bash already running would total four processes
and we'll see this failing as expected,
we cannot run more than three processes.
So the three process limit is enforced correctly.
Now, if you're following along, let's type logout to exit from trinity session.
If we want to see the limits for our current session, we can type in this UIM dash A,
we have small hints between the parentheses.
For example, we can see dash U displayed for mac user processes.
That means we could type U limit dash U 5000
to lower our limit to 5000 processes. By default.
A user can only lower their limits, not raise them.
The Exception is when there are hard and soft limits.
In that case, the user can raise his or her limit all the way up to the hard value.
But only once
after that limit is raised with the UIM
command,
the next command can only lower it.
It cannot be raised the second time even if the hard limit would allow it.