File description is a kind of system resource. Linux management of system resources is a hierarchical level of management.
System-Level management:/proc/sys/fs/file-max sets the maximum number of file descriptions that can be used by the system.
You can view this value in the following ways:
[Email protected] #panda ~]# Cat/proc/sys/fs/file-max
655351
[Email protected] #panda ~]# sysctl Fs.file-max
Fs.file-max = 655351
[Email protected] #panda ~]#
You can modify this value in the following ways:
[Email protected] #panda ~]# echo 655300 >/proc/sys/fs/file-max
[Email protected] #panda ~]# sysctl Fs.file-max
Fs.file-max = 655300
[Email protected] #panda ~]# sysctl-w fs.file-max=655350
Fs.file-max = 655350
[Email protected] #panda ~]# sysctl Fs.file-max
Fs.file-max = 655350
[Email protected] #panda ~]#
The value modified in the above method takes effect immediately, but the machine restarts and then fails. Permanent changes can be made in the following ways:
[Email protected] #panda etc]# echo fs.file-max=524288 >>/etc/sysctl.conf
[Email protected] #panda etc]# sysctl Fs.file-max
Fs.file-max = 655350
[Email protected] #panda etc]# sysctl-p
Fs.file-max = 524288
[Email protected] #panda etc]# sysctl Fs.file-max
Fs.file-max = 524288
[Email protected] #panda etc]#
Changes in sysctl.conf after the sysctl-p command are executed to take effect immediately.
There is one more question is File-max maximum can set how big? Of course you can set it up, but the more file descriptors you open at the same time, the greater the memory overhead. How to set a reasonable limit? An empirical algorithm is 256 FD requiring 4 m of memory. For example 8G memory, 8*1024/4*256=524288.
User-level management:/etc/security/limit.conf can add restrictions on users ' use of resources
Set the format to:
username| @groupname Type Resource limit
username| @groupname: Sets the user name that needs to be restricted, the group name is preceded by the @ and the user name differs. You can also use the wildcard character * to restrict all users.
Type: Soft,hard and-,soft refer to the setting values that are currently in effect for the system. Hard indicates the maximum value that can be set in the system. The soft limit cannot be higher than the hard limit. Soft and hard values are also set.
Resource: There are a variety of resource types. Nofile is the maximum number of open files.
For example: *-nofile 655350 Sets the maximum number of open files for all users soft and hard are set to 524287
Changes to the/etc/security/limit.conf will take effect during the new session
Session-Level management: Ulimit command to view and set resource limits for the current session period
ULIMIT-SN viewing session duration Maximum file descriptor soft limit
Ulimit-hn View Session duration maximum file descriptor hard limit, hard limit is the upper limit of soft.
ULIMIT-SN 2046 Limit the number of files that can be opened in the current session period to 2046 (if one of the processes has 2046 open, the other process will fail to hit the file).
ULIMIT-SN See the default of 1024, changes made with Ulimit will expire after the end of the session period. How do you make the session-level resource limit settings permanent? You can put the Ulimit command in the process of initializing the session when it is established so that the command modification settings are automatically executed for each conversation period. For example, add a row ulimit-sn 2046 to the. BASHRC in the user directory, and to take effect for all users, you can add a row of commands to Ulimit-sn 2046 in/etc/profile. One thing that is easy to overlook is that if we set a user-level limit, the initial value of the resource limit for the new session period (ULIMIT-SN) is inherited from the user-level limit (the value set in/etc/security/limit.conf).
Process-level management: Processes can invoke Getrlimit and setrlimit to set and modify the limits of the corresponding resource.
For example:
Rlimit tmp={4095,4096};
Setrlimit (RLIMIT_NOFILE,&TMP); The soft and hard limits for setting the number of open files for a process are 4095,4096
Summary: The maximum number of file descriptors that can be used by a single process is determined by the system limit, user limit, session limit, and the smallest of the process limit values. If we deploy our own network service on a single machine, we just need to follow the steps below to modify it:
1. Calculate Fdmax = physical memory Size (m)/4 * 256 assuming memory is 8g,fdmax=524288
2. Execution command: sysctl-w fs.file-max 524288
3. Execution command: sysctl-p
4. Execute command: Echo * Soft nofile 524286 >>/etc/security/limit.conf
5. Execute command: Echo * hard Nofile 524287 >>/etc/security/limit.conf
5. End the current session period
6. Start a new session
7. Start our Services
CentOS Maximum file description limit change