Ulimit command
Function Description: Controls the resources of the shell program.
Syntax: ulimit [-ahs][-c <core file Upper limit >][-d < data section size >][-f < file size >][-m < memory size >][-n < files >][-p < Buffer size >][-s < stacking size >][-t <cpu time >][-u < program number >][-V < virtual memory size
Supplemental Note: Ulimit is a shell-built directive that can be used to control the resources of shell execution programs.
Parameters
-a displays the current resource constraint settings.
-c <core File upper limit > set the maximum value of core file, the unit is block.
-D < data section size > The maximum value of the data section of the program, in kilobytes.
-F < file size > The largest file the shell can establish, in blocks.
-H set the hard limit of the resource, which is the limit set by the administrator.
-m < memory size > Specifies the maximum amount of memory that can be used, in kilobytes.
-N < number of files > Specify the maximum number of files that can be opened at the same time.
-p < buffer size > Specifies the size of the pipe buffer, in units of 512 bytes.
-s < stacking size > Specifies the upper bound of the stack in kilobytes.
-S sets the flexibility limit for resources.
-T <CPU time > Specify the maximum CPU usage time in seconds.
-U < number of programs > The maximum number of programs that users can open.
-v < virtual memory size > Specify the maximum amount of virtual memory that can be used, in kilobytes.
Example
When writing a program in Linux, if the program is larger, often encounter "segment error" (segmentation fault) Such a problem, this is mainly due to the Linux system's initial stack size (stack sizes) is too small, generally 10M. I usually set the stack size to 256M, so there is no error. The command is:
Ulimit-s 262140
If you want the system to automatically remember this configuration, edit the/etc/profile file, under the "Ulimit-s-C 0 >/dev/null 2>&1" line, add "Ulimit-s 262140" to save the reboot system.
1] In RH8 's environmental file/etc/profile, we can see how the system configures Ulimit:
#grep Ulimit/etc/profile
Ulimit-s-C 0 >/dev/null 2>&1
This statement sets the size of the software resources and the core file
2] If we want to make some restrictions on the size of the file created by the shell, such as:
#ll h
-rw-r--r--1 Lee Lee 150062 July 02:39 H
#ulimit-F #设置创建文件的最大块 (one = 512 bytes)
#cat H>NEWH
File size limit exceeded
#ll NEWH
-rw-r--r--1 Lee Lee 51200 November 8 11:47 NEWH
The size of file h is 150062 bytes, and the size of the creation file we set is 512 bytes x100 block = 51200 bytes
The system will, of course, generate 51200-byte NEWH files based on your settings.
3] You can put the ulimit you want to set in the/etc/profile environment file, like instance 1].
Use
Set or report user resource limits.
Grammar
Ulimit [-h] [s] [A] [-c] [-d] [-f] [-m] [-n] [-S] [-t] [Limit]
Describe
The Ulimit command sets or reports user process resource limits, as defined by the/etc/security/limits file. The file contains the following default value limits:
Fsize = 2097151
Core = 2097151
CPU =-1
data = 262144
RSS = 65536
stack = 65536
Nofiles = 2000
These values are used as default values when new users are added to the system. When you add a user to the system, the above values are set by the Mkuser command or changed by the Chuser command.
The limit is divided into soft or rigid. The Ulimit command allows the user to change the soft limit to the maximum set value of the hard limit. To change the resource hard limit, you must have root user rights.
Many systems do not include one or more of these limits. The limit of a particular resource is set when the Limit parameter is specified. The value of the Limit parameter can be a number in a specified cell in each resource, or a value of unlimited. To set a specific ulimit to unlimited, you can use the word Unlimited.
Note: Setting the default limit in the/etc/security/limits file sets the system width limit, not just the user's required limit when creating the user.
When the Limit parameter is omitted, the current resource limit is printed. The soft limit is printed unless the user specifies the-H flag. When a user specifies more than one resource, the limit name and cell are printed before the value. If an option is not given, the-f flag is assumed.
Because the Ulimit command affects the current shell environment, it is provided as a Shell general built-in command. If the command is invoked in a stand-alone command execution environment, it does not affect the file size limit of the caller's environment. This is the case in the following example:
Nohup ulimit-f 10000
ENV Ulimit 10000
Once the hard limit is reduced through the process, it cannot be increased without root privilege, even if it is returned to the original value.
For more information on user and system resource limits, see Extensions, se in AIX 5L Version 5.3 Technical Reference:base operating system and Volume Getrlimit 1 Trlimit or Vlimit subroutine.
Sign
-A lists all current resource limits.
-c Specifies the size of the core dump in 512-byte blocks.
-d Specifies the size of the data region in K-byte units.
-F Sets the file size limit (in blocks) when using the Limit parameter, or reports the file size limit when no parameters are specified. The default value is the-f flag.
-h Specifies the hard limit for setting a given resource. You can increase the hard limit if the user has root user rights. Any user can reduce the hard limit.
-m specifies the size of the physical storage in K-byte units.
-n Specifies the limit of the number of file descriptors that a process can hold.
-s Specifies the size of the stack in K-byte units.
-s specifies that the soft limit is set for the given resource. The soft limit can be increased to the value of the hard limit. If the-H and-s flags are not specified, the limit applies to both.
-t specifies the number of seconds per process to use.
Exit status
Returns the following exit values:
0 completed successfully.
>0 rejects a request for a higher limit, or an error occurs.
Example
To set the file size limit to 51,200 bytes, enter:
Ulimit-f 100
Ulimit-a is used to display current user process restrictions.
Linux for each user, the system limits its maximum number of processes. To improve performance, you can set the maximum number of processes for each Linux user based on device resources, and I set the maximum number of processes for a Linux user to 10,000:
Ulimit-u 10000
For Java applications that require many socket connections and keep them open, it is a good idea to modify the number of files that each process can open by using Ulimit-n XX, which defaults to 1024.
Ulimit-n 4096 increases the number of files that can be opened per process to 4096, and defaults to 1024
Some important settings for other suggestions that are set to unrestricted (unlimited) are:
Length of data segment: Ulimit-d Unlimited
Maximum memory size: Ulimit-m Unlimited
Stack size: Ulimit-s Unlimited
CPU Time: Ulimit-t Unlimited
Virtual Memory: Ulimit-v Unlimited
Temporarily, it is appropriate to log on to a shell session through the Ulimit command.
Permanently, by adding a corresponding Ulimit statement to a file read by the login shell, a shell-specific user resource file, such as:
1, the maximum number of processes to unlock the Linux system and maximum file open limit:
Vi/etc/security/limits.conf
# Add the following line
* Soft Noproc 11000
* Hard Noproc 11000
* Soft Nofile 4100
* Hard Nofile 4100
Description: * representative for all users
Noproc represents the maximum number of processes
Nofile is the maximum number of file open
2, let SSH accept login program login, easy to SSH client view ulimit-a resource restrictions:
A, Vi/etc/ssh/sshd_config
Change the value of Userlogin to Yes and remove the # annotation
b, restart the SSHD service:
/etc/init.d/sshd restart
3, modify all Linux user's environment variable file:
Vi/etc/profile
Ulimit-u 10000
Ulimit-n 4096
Ulimit-d Unlimited
Ulimit-m Unlimited
Ulimit-s Unlimited
Ulimit-t Unlimited
Ulimit-v Unlimited
/**************************************
Sometimes in the program to open a number of files, analysis, the general default number of systems is 1024, (with ULIMIT-A can see) for normal use is enough, but for the program, it is too little.
Modify 2 files.
1./etc/security/limits.conf
Vi/etc/security/limits.conf
Plus:
* Soft Nofile 8192
* Hard Nofile 20480
2./etc/pam.d/login
Session required/lib/security/pam_limits.so
**********
Also ensure that the/etc/pam.d/system-auth file has the following content
Session required/lib/security/$ISA/pam_limits.so
This line ensures that this restriction is enforced by the system.
***********
3. Bash_profile for general users.
#ulimit-N 1024
Re-login OK
-------------
For Solaris
In fact, in the system there is such a command ulimit, the following is the result of ulimit-a execution:
Time (seconds) Unlimited
File (blocks) Unlimited
Data (Kbytes) Unlimited
Stack (Kbytes) 8192
Coredump (Blocks) Unlimited
Nofiles (descriptors) 1024
Memory (Kbytes) Unlimited
Where nofiles is the variable value of the file descriptor, which is affected by the Rlim_fd_cur parameter and can be modified with the ulimit-n number command. But no matter how it changes, the procedure still cannot break through the fd=256 limit. The following information can be found in the Solaris tunable Parameters Reference Manua Book:
A 32-bit program using standard I/O is limited to 256 file descriptors.
A 64-bit program using standard I/O can use up to 2 billion descriptors.
This means that the 32-bit program is not able to break through this limitation, only 64-bit programs to use up to 200 million file descriptors, Sun's hardware and software in the early implementation of the 64-bit architecture, now the only solution is to compile the program into 64-bit program, in order to generate 64-bit program, You have to have 64-bit compilers (not really), If you go to www.sunfreeware.com download 64-bit compiler gcc, there is no special note on the site is 64-bit GCC, but there will be an unexpected gain, that is, the software's instructions that as long as the use of GCC compiled with-M64 option to generate 64-bit programs.
So with gcc-m64 to build a 64-bit program, with Ulimit-n 102400 to set the number of FD into a large case, all problems solved, no longer the file descriptor is not enough.
The Rlimi_fc_max and rlim_fd_cur formats are set in the/etc/system file as follows:
* Set hard limit on file descriptors
Set Rlim_fd_max = 4096
* Set soft limit on file descriptors
Set rlim_fd_cur = 1024
The command ulimit uses the following format:
Usage:ulimit [-HSACDFNSTV] [limit]
Ulimit-a is the set value that displays the parameters, Ulimit-n is used to set the maximum value of the FD.
*************************************************
Modify file Descriptor Restrictions
Solaris has two parameters that govern the file descriptor that the process can open: Rlim_fd_max,rlim_fd_cur. The former modification is a hard setting, the modification requires permissions, the latter is a soft setting, the user can limit or setrlimit () modification, the maximum value can not exceed the former. In general, we modify these two parameters in the/etc/system.
Set Rlim_fd_max = 65535
Set rlim_fd_cur = 65535
==========================
Ulimit the resources used by the shell startup process.
You can use this command to see how resources are consumed by a process.
How to use: Ulimit [-ACDFHLMNPSSTVW] [size]
-H Set hardware resource limits.
-S sets the software resource limit.
-a displays all current resource restrictions.
-C Size: Sets the maximum value for the core file. Unit: Blocks
-D Size: Sets the maximum value for the data segment. Unit: Kbytes
-F Size: Sets the maximum value for creating a file. Unit: Blocks
-L Size: Sets the maximum value to lock a 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 number of file descriptors that the kernel can open at the same time. Unit: N
-P Size: Sets the maximum value of the pipe buffer. Unit: Kbytes
-S size: Sets the maximum value for the stack. Unit: Kbytes
-T size: Sets the maximum CPU usage time limit. Unit: Seconds
-V Size: Sets the maximum value for virtual memory. Unit: Kbytes 5
1] In RH8 's environmental file/etc/profile, we can see how the system configures Ulimit:
#grep Ulimit/etc/profile
Ulimit-s-C 0 >/dev/null 2>&1 (output redirection, normal output and exception output are ignored)
This statement sets the size of the software resources and the core file
2] If we want to make some restrictions on the size of the file created by the shell, such as:
#ll h
-rw-r--r--1 Lee Lee 150062 July 02:39 H
#ulimit-f #设置创建文件的最大块 (one = 512 bytes)
#cat h>newh
Fil E size limit exceeded
#ll newh
-rw-r--r--1 Lee Lee 51200 November 8 11:47 newh
file h is 150062 bytes, and the size of the creation file we set is 512 bytes x100 block = 51200 bytes
Of course the system will generate 51200 bytes of NEWH files based on your setup.
3] You can put the ulimit you want to set in the/etc/profile environment file, like instance 1.
If set for all users, it can be set at/etc/security/limits.conf.