Linux learning route 2 [limit resource setting]

Posted by EdN on Wed, 09 Feb 2022 10:52:31 +0100

preface

The limit parameter in Linux can generally be set by ulimit command or editing / etc / security / limits Conf reloads to make it effective
ulimit is more direct, but it is only valid in the current session. Limits Conf can make the user take effect in the next login according to the user and restrictions

1, ulimit command

1.1ulimit command to view user status

ulimit -a displays the current user process restrictions.

[angel@linux ~]$ ulimit -a
core file size             (blocks, -c)  1024
data seg size              (kbytes, -d)  unlimited
scheduling priority                (-e)  0
file size                  (blocks, -f)  unlimited
pending signals                    (-i)  unlimited
max locked memory          (kbytes, -l)  unlimited
max memory size            (kbytes, -m)  unlimited
open files                         (-n)  819200
pipe size               (512 bytes, -p)  8
POSIX message queues        (bytes, -q)  unlimited
real-time priority                 (-r)  0
stack size                 (kbytes, -s)  unlimited
cpu time                  (seconds, -t)  unlimited
max user processes                 (-u)  4096
virtual memory             (kbytes, -v)  unlimited
file locks                         (-x)  unlimited

Other parameters:

-H set the hard resource limit
-S sets the soft resource limit
-a displays all current resource limits
-c size: set the maximum value of core file Unit: blocks
-d size: set the maximum value of the data segment Unit: kbytes
-Size: set the maximum value of. F for file creation Unit: blocks
-l size: set the maximum value of locking process in memory Unit: kbytes
-m size: sets the maximum amount of resident memory that can be used Unit: kbytes
-n size: sets the maximum value of file descriptors that the kernel can open at the same time Unit: n
-p size: sets the maximum value of the pipeline buffer Unit: kbytes
-s size: sets the maximum value of the stack Unit: kbytes
-t size: sets the maximum CPU usage time Unit: seconds
-v size: set the maximum value of virtual memory Unit: kbytes
-U < number of programs > the maximum number of programs that users can open

1.2 ulimit command settings

Set directly in the current session through ulimit, and only the current session will take effect.

[angel@linux ~]$ ulimit -s 
unlimited
[angel@linux ~]$ ulimit -s 819200
[angel@linux ~]$ ulimit -s 
819200

2, Modify the configuration file to permanently take effect

2.1 modify limit settings

Examples are as follows:

  [angel@linux ~]# tail -n 20 /etc/security/limits.conf
  #<domain>    <type> <item>  <value>
  #
  *             soft  core          1024       #Number of soft processes
  *             hard  core          1024       #Number of hard processes
   
  *             soft  nproc         unlimited  #Number of soft processes
  *             hard  nproc         unlimited  #Number of hard processes
  
  *             soft  memlock       unlimited  #soft max locked-in-memory address space
  *             hard  memlock       unlimited  #hard max locked-in-memory address space
   
  *             soft  stack         unlimited  #soft max stack size
  *             hard  stack         unlimited  #hard max stack size 
  
  *             soft  nofile        819200    #Number of soft file handles
  *             hard  nofile        819200    #Number of hard file handles 
  *             soft  sigpending    unlimited  #soft pending signals
  *             hard  sigpending    unlimited  #hard pending signals
   
  *             soft  msgqueue      unlimited  #soft POSIX message queue
  *             hard  msgqueue      unlimited  #hard POSIX message queue
  # End of file
  [angel@linux ~]$

**Note: * refers to all users. At the same time, you can also specify specific user names, such as root and angel

The difference between "soft" and "hard"

soft  xxx  : Represents the setting of warning. You can exceed this setting value, but there will be a warning after exceeding it.
 
hard  xxx  : Represents a strict setting, and it is not allowed to exceed the set value.

For example, if soft is set to 2048 and hard is set to 4096, you can use it freely when the number of users is 1 ~ 2048. A warning message will appear when 2048 ~ 4096, and an error will be reported when it is greater than 4096.

The difference between "nproc" and "nofile"

nproc  : Is the operating system level limit on the number of processes created by each user
 
nofile : Is the limit on the number of files that can be opened by each process

2.2 modify the system configuration

Open VIM / etc / security / limits d/90-nproc. Conf sets the limit number. The first column represents users and * represents all users

*              soft      nproc       unlimited
*              hard      nproc       unlimited
*              soft      nofile      unlimited
*              hard      nofile      unlimited
root           soft      nproc       unlimited
root           hard      nproc       unlimited
root           soft      nofile      unlimited
root           hard      nofile      unlimited

soft nproc: the maximum number of processes available to a single user (if it exceeds, it will be warned);
hard nproc: the maximum number of processes available to a single user (an error will be reported if it exceeds);
soft nofile: the maximum number of file descriptors that can be opened (warning will be given if it is exceeded);
hard nofile: the maximum number of file descriptors that can be opened (an error will be reported if it exceeds);

Restart effective

[angel@linux ~] reboot

Topics: Linux Operation & Maintenance bash server