Linux ulimit command

Source: Internet
Author: User

When deploying applications in Linux, sometimes the Socket/File: Can't open so many files problem occurs, for example, Squid acts as a proxy, when the number of files opened to 900 is large, the speed will be very fast, and the web page may not be opened.

In fact, Linux has a file handle limit, and Linux is not very high by default, generally 1024, it is easy to use production servers to reach this number.

View Method

We can use ulimit-a to view all limit values.

[Root @ centos5 ~] # Ulimit-
Core file size (blocks,-c) 0
Data seg size (kbytes,-d) unlimited
Max nice (-e) 0
File size (blocks,-f) unlimited
Pending signals (-I) 4096
Max locked memory (kbytes,-l) 32
Max memory size (kbytes,-m) unlimited
Open File (-n) 1024
Pipe size (512 bytes,-p) 8
POSIX message queues (bytes,-q) 819200
Max rt priority (-r) 0
Stack size (kbytes,-s) 10240
Cpu time (seconds,-t) unlimited
Max user processes (-u) 4096
Virtual memory (kbytes,-v) unlimited
File locks (-x) unlimited | <

"Open files (-n) 1024" is the Linux operating system's limit on the number of file handles opened by a process (including the number of opened sockets, can affect the number of concurrent connections of MySQL ). this value can be modified using the ulimit command, but the value modified by the ulimit command is only valid for the current user's current use environment. The value will expire after the system is restarted or the user exits.

The total system limit is here./proc/sys/fs/file-max. You can view the current value through cat and modify/etc/sysctl. conf.

In addition,/proc/sys/fs/file-nr shows the number of file handles currently used by the entire system.

When you look up the file handle problem, there is also a very practical program lsof. You can easily see the handles opened by a process. You can also see which process occupies a file/directory.

Modification Method
To make the value of the modified ulimits take effect permanently, you must modify the configuration document and place the ulimit modification command in/etc/profile. This method is inconvenient, another method is to modify/etc/sysctl. conf. I modified and tested it, but it will not change the user's ulimits-. Only the value of/proc/sys/fs/file-max has changed. (I failed to put the command in rc. local)

I think the correct method is to modify/etc/security/limits. conf.
There are detailed annotations, such

* Soft nofile 32768
* Hard nofile 65536

You can also directly run the following shell. Just copy it to your terminal and run it.
Echo-ne"
* Soft nofile 65536
* Hard nofile 65536
">>/Etc/security/limits. conf

You can change the file handle restriction to soft 32768 and hard 65536. the domain at the beginning of the configuration file is set as an asterisk to represent the global, you can also make different restrictions for different users

Note. in this case, the hard limit is the actual limit, while the soft limit is the warnning limit, and only the warning is made. in fact, the ulimit command itself has the hardware and software settings. Adding-H is hard, and adding-S is soft.
Soft restrictions are displayed by default. If the ulimit command is not added during modification, the two parameters are changed together.

Effective

After the modification, you can see it again. (my system is Centos5.1. the modification takes effect immediately after you log on again. You can use ulimit-a to check and confirm .)

If you use squid, add ulimit-HSn 65535 to the/etc/init. d/squid file. In addition, add max_filedesc 16384 to squid. conf.

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.