[Linux] using Linux ulimit
1. What is ulimit?
Ulimit is a command that can set or report the current user resource limit. The ulimit command requires administrator privileges and can only be used on systems that allow shell control. In other words, it has been embedded into the shell.
2. Basic use
➜ ~ ulimit unlimited
As shown in the output, unlimited, the current user has unlimited resources to access. This means that the current user can consume all the resources supported by the current system.
➜ ~ ulimit -a -t: cpu time (seconds) unlimited -f: file size (blocks) unlimited -d: data seg size (kbytes) unlimited -s: stack size (kbytes) 8192 -c: core file size (blocks) 0 -m: resident set size (kbytes) unlimited -u: processes 7823 -n: file descriptors 1024 -l: locked-in-memory size (kbytes) 64 -v: address space (kbytes) unlimited -x: file locks unlimited -i: pending signals 7823 -q: bytes in POSIX msg queues 819200 -e: max nice 0 -r: max rt priority 0 -N 15: unlimited
-a parameter can show detailed parameters, that is, what resources we can limit. There are two types of restrictions here: Soft & hard. Hard resource limitation means physical limitation; Soft resource limits are managed by users, and the maximum value of soft is limited by hard.
System resources are defined in the * / etc/security/limits.conf * file. When we use ulimit, we are using the values defined in this file.
#* soft core 0 * #root hard core 100000 *root soft stack 86400 *root hard stack 86400 #* hard rss 10000 #@student hard nproc 20 #@faculty soft nproc 20 #@faculty hard nproc 50 #ftp hard nproc 0 #ftp - chroot /ftp #@student - maxlogins 4
3. View other resource constraints
ulimit -c # View the maximum value of the core file ulimit -d # View the maximum value of the data segment ulimit -e # View the maximum scheduling priority of the current user ulimit -s # The maximum stack size of the current user ulimit -u # Maximum number of processes for the current user ulimit -v # View the size of virtual memory ulimit -b # View the size of socket buffer ulimit -t # View the time allowed for each process to run ulimit -n # View the maximum number of file descriptors a process can have
Other commands can be viewed through – help
4. Set resource limits
Through the above content, we learned how to view some resource limit values in the current system. Now let's see how to modify them.
Note: for hard restriction, we need root permission pip
First enter the limits.conf file
vim /etc/security/limits.conf
Edit the file in the following format:
<domain> <type> <item> <value>
Can be the following values:
#Where: #<domain> can be: # - a user name # - a group name, with @group syntax # - the wildcard *, for default entry # - the wildcard %, can be also used with %group syntax, # for maxlogin limit # - NOTE: group and wildcard limits are not applied to root. # To apply a limit to the root user, <domain> must be # the literal username root. # #<type> can have the two values: # - "soft" for enforcing the soft limits # - "hard" for enforcing hard limits # #<item> can be one of the following: # - core - limits the core file size (KB) # - data - max data size (KB) # - fsize - maximum filesize (KB) # - memlock - max locked-in-memory address space (KB) # - nofile - max number of open files # - rss - max resident set size (KB) # - stack - max stack size (KB) # - cpu - max CPU time (MIN) # - nproc - max number of processes # - as - address space limit (KB) # - maxlogins - max number of logins for this user # - maxsyslogins - max number of logins on the system # - priority - the priority to run user process with # - locks - max number of file locks the user can hold # - sigpending - max number of pending signals # - msgqueue - max memory used by POSIX message queues (bytes) # - nice - max nice priority allowed to raise to values: [-20, 19] # - rtprio - max realtime priority # - chroot - change root to directory (Debian-specific) # #<domain> <type> <item> <value>
Of which:
- Core: core file size (KB)
- Data: maximum data size (KB)
- fsize: maximum file size (KB)
- memlock: maximum locked in memory address space (KB)
- nofile: the maximum number of open files
- rss: maximum resident set size (KB)
- Stack: maximum stack size (KB)
- cpu: maximum cpu time (minutes)
- nproc: maximum number of processes
- as: limit of address space (KB)
- maxlogins: the maximum number of logins of the current user
- maxsyslogins: the maximum number of logins in the current system
- Priority: the priority of the running user process
- Locks: the number of file locks that a user can hold
- sigpending: the maximum number of pending signals