Modify the maximum number of Linux file handles

Source: Internet
Author: User
Tags posix

---View the default maximum number of file handles, the system defaults to 1024

# Ulimit-n

1024

----See how many handles the current process has opened

# Lsof-n|awk ' {print $} ' |sort|uniq-c|sort-nr|more

131 24204

57 24244

57 24231 .....

Where the first column is the number of open handles and the second column is the process ID.

You can view the process name based on the ID number.

# PS Aef|grep 24204

Nginx 24204 24162 99 16:15? 00:24:25/usr/local/nginx/sbin/nginx-s

Linux has hard limits and soft limits. These two parameters can be set by Ulimit. To do this, run the following command as the root user:

# ULIMIT-HSN 4096

In the above command, h specifies the hard size, s specifies the soft size, and n indicates the maximum number of open file handles that are set for a single process. Personally think it's best not to exceed 4096, after all, the more open file handle number of response time will certainly be slower. When the number of handles is set, the default value is restored after the system restarts. If you want to save it permanently, you can modify the. bash_profile file, and you can modify the/etc/profile to add the above command to the last.

It is known that when Linux servers are tuned in large concurrency, it is often necessary to pre-tune the Linux parameters, where modifying the maximum number of Linux file handles is one of the most frequently modified parameters.

You can query Linux-related parameters by executing ulimit-a in Linux, as follows:

[Email protected] ~]# ulimit-a
Core file size (blocks,-c) 0
Data seg Size (Kbytes,-D) Unlimited
Scheduling Priority (-e) 0
File size (blocks,-f) Unlimited
Pending signals (-i) 256324
Max locked Memory (Kbytes, L) 64
Max memory Size (Kbytes,-m) unlimited
open Files (-N) 1024x768
Pipe Size (bytes,-p) 8
POSIX message queues (bytes,-Q) 819200
Real-time priority (-R) 0
Stack size (Kbytes,-s) 10240
CPU time (seconds,-t) unlimited
MAX User Processes (-u) 256324
Virtual Memory (Kbytes,-V) Unlimited
File locks (-X) Unlimited

By default, the maximum number of file handles for Linux is 1024. When your server reaches its limit in large concurrency, it will report "Too many open files".

So how do I change the number of Linux max file handles? It's actually very simple:

1, Ulimit-n 2048

This command modifies the maximum number of Linux file handles and modifies the state of the changes using ulimit-a later, such as:

[Email protected] ~]# ulimit-n 2048
[Email protected] ~]# ulimit-a
Core file size (blocks,-c) 0
Data seg Size (Kbytes,-D) Unlimited
Scheduling Priority (-e) 0
File size (blocks,-f) Unlimited
Pending signals (-i) 256324
Max locked Memory (Kbytes, L) 64
Max memory Size (Kbytes,-m) unlimited
open Files (-N) 2048
Pipe Size (bytes,-p) 8
POSIX message queues (bytes,-Q) 819200
Real-time priority (-R) 0
Stack size (Kbytes,-s) 10240
CPU time (seconds,-t) unlimited
MAX User Processes (-u) 256324
Virtual Memory (Kbytes,-V) Unlimited
File locks (-X) Unlimited

However, this method is only valid for the current process. Reopen a shell or open a process, and you'll see that the parameter is still a number before the Ulimit-n xx changes. So is there a way to do it once and for all?

Of course! That is to modify the system parameters.

2, modify the Linux system parameters. vi/etc/security/limits.conf add

* Soft Nofile 65536

* Hard Nofile 65536

Modify save later, log off the current user, login again, execute ulimit-a, OK, the parameters are in effect:

[Email protected] ~]# ulimit-a
Core file size (blocks,-c) 0
Data seg Size (Kbytes,-D) Unlimited
Scheduling Priority (-e) 0
File size (blocks,-f) Unlimited
Pending signals (-i) 256600
Max locked Memory (Kbytes, L) 64
Max memory Size (Kbytes,-m) unlimited
open Files (-N) 65536
Pipe Size (bytes,-p) 8
POSIX message queues (bytes,-Q) 819200
Real-time priority (-R) 0
Stack size (Kbytes,-s) 10240
CPU time (seconds,-t) unlimited
MAX User Processes (-u) 256600
Virtual Memory (Kbytes,-V) Unlimited
File locks (-X) Unlimited

Article reproduced please specify the source: http://www.cnblogs.com/likehua/p/3831331.html

Modify the maximum number of Linux file handles

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.