Change the maximum file descriptor of CentOS and maximum file descriptor of centos

Source: Internet
Author: User

Change the maximum file descriptor of CentOS and maximum file descriptor of centos

File descriptors are a type of system resources. Linux manages system resources in a hierarchical manner.

System-level management:/proc/sys/fs/file-max sets the maximum number of files that can be opened by the system.
You can view the value as follows:
[Root @ # panda ~] # Cat/proc/sys/fs/file-max
655351
[Root @ # panda ~] # Sysctl fs. file-max
Fs. file-max = 655351
[Root @ # panda ~] #
You can modify the value as follows:
[Root @ # panda ~] # Echo 655300>/proc/sys/fs/file-max
[Root @ # panda ~] # Sysctl fs. file-max
Fs. file-max = 655300
[Root @ # panda ~] # Sysctl-w fs. file-max = 655350
Fs. file-max = 655350
[Root @ # panda ~] # Sysctl fs. file-max
Fs. file-max = 655350
[Root @ # panda ~] #
The value modified in the preceding method takes effect immediately but becomes invalid after the machine is restarted. The following method can be used for permanent modification:
[Root @ # panda etc] # echo fs. file-max = 524288>/etc/sysctl. conf
[Root @ # panda etc] # sysctl fs. file-max
Fs. file-max = 655350
[Root @ # panda etc] # sysctl-p
Fs. file-max = 524288
[Root @ # panda etc] # sysctl fs. file-max
Fs. file-max = 524288
[Root @ # panda etc] #
After the sysctl-p command is executed, modifications in sysctl. conf can take effect immediately.
Another question is how big can file-max be set? Of course, you can set it to a larger value, but the larger the number of file descriptors opened at the same time, the larger the memory overhead. How can we set a reasonable ceiling? An empirical algorithm requires 4 MB of memory for 256 fd servers. For example, 8 GB memory, 8*1024/4*256 = 524288.

User-level management:/etc/security/limit. conf allows you to add resource usage restrictions.
Set the format:
Username | @ groupname type resource limit
Username | @ groupname: Set the user name to be restricted. The group name is preceded by @ and the user name. You can also use wildcard * to restrict all users.
Type: soft, hard, and-. soft indicates the setting value that takes effect for the current system. Hard indicates the maximum value that can be set in the system. Soft cannot be more restrictive than hard. -Indicates that both soft and hard values are set.
Resource: there are multiple resource types. Nofile is the maximum number of opened files.
For example, *-nofile 655350 sets the soft and hard values of the maximum number of files opened by all users to 655350.
Modifications to/etc/security/limit. conf will take effect in the new session period.

Session-level management: run the ulimit command to view and set resource limits for the current session period

Ulimit-Sn: Maximum file descriptor soft limit for viewing sessions
Ulimit-Hn: view the hard limit of the maximum file descriptor during a session. The hard limit is the soft limit.
Ulimit-Sn 2046 limits the number of files that can be opened during the current session period to 2046 (if one process opens 2046 files, all other processes will fail to call files ).
The default value 1024 is displayed on the ulimit-Sn. All modifications made with the ulimit will expire after the session ends. So how can we make the session-level resource restriction settings take effect permanently? You can run the ulimit command in the initialization process when the session is established, so that the command is automatically executed during each session to modify the settings. For example, add a ulimit-Sn 2046 line to. bashrc in the user directory. To make it take effect for all users, add a line of command ulimit-Sn 2046 in/etc/profile. It is easy to ignore that, if we have set user-level restrictions, the initial value of the resource restrictions for the new session period (as viewed by ulimit-Sn) is the user-level limit value (/etc/security/limit. value set in conf.

Process-level management: processes can call getrlimit and setrlimit to set and modify resource limits.
For example:
Rlimit tmp = {4095,4096 };
Setrlimit (RLIMIT_NOFILE, & tmp); // you can set the maximum number of files opened by a process to OR.

Summary: The maximum file descriptor that a single process can use is determined by the system restriction, user restriction, session duration system, and the smallest process restriction value. If we deploy our own network service on a machine, we only need to follow the steps below to modify it:
1. Calculate fdmax = physical memory size (MB)/4*256 assuming that the memory is 8 GB, fdmax = 524288
2. Run the command: sysctl-w fs. file-max 524288.
3. Execute the command: sysctl-p
4. Execute the command: echo * soft nofile 524286>/etc/security/limit. conf
5. Execute the command: echo * hard nofile 524287>/etc/security/limit. conf
5. End the current session period
6. Start a new session
7. Start our services
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.