Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set ulimit and file descriptor limits on a Linux server

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to set ulimit and file descriptor limits on the Linux server", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to set ulimit and file descriptor limits on the Linux server" this article.

View the limit on the number of files currently open on any Linux system

To get a limit on the number of open files on the Linux server, execute the following command

[root@ubuntu ~] # cat / proc/sys/fs/file-max146013

The above number indicates that the user can open '146013' files in each user login session.

[root@centos ~] # cat / proc/sys/fs/file-max149219 [root@debian ~] # cat / proc/sys/fs/file-max73906

This clearly shows that each Linux operating system has different limits on the number of files to be opened. This is based on the dependencies and applications running in their respective systems.

Ulimit command

As the name implies, ulimit (user restrictions) is used to display and set resource limits for logged-in users. When we run the ulimit command with the-an option, it prints all resource limits for the logged-in user. Now let's run ulimit-an on Ubuntu/Debian and CentOS systems

Ubuntu / Debian system shashi@Ubuntu ~} $ulimit-acore file size (blocks,-c) 0data seg size (kbytes,-d) unlimitedscheduling priority (- e) 0file size (blocks,-f) unlimitedpending signals (- I) 5731max locked memory (kbytes,-l) 64max memory size (kbytes -m) unlimitedopen files (- n) 1024 pipe size (512 bytes,-p) 8POSIX message queues (bytes,-Q) 819200real-time priority (- r) 0stack size (kbytes,-s) 8192cpu time (seconds,-t) unlimitedmax user processes (- u) 5731virtual memory (kbytes -v) unlimitedfile locks (- x) unlimitedCentOS system shashi@centos ~} $ulimit-acore file size (blocks,-c) 0data seg size (kbytes,-d) unlimitedscheduling priority (- e) 0file size (blocks,-f) unlimitedpending signals (- I) 5901max locked memory (kbytes,-l) 64max memory size (kbytes -m) unlimitedopen files (- n) 1024pipe size (512 bytes,-p) 8POSIX message queues (bytes,-Q) 819200real-time priority (- r) 0stack size (kbytes,-s) 8192cpu time (seconds,-t) unlimitedmax user processes (- u) 5901virtual memory (kbytes -v) unlimitedfile locks (- x) unlimited

As we can see here, different operating systems have different restriction settings. All of these restrictions can be configured / changed using the ulimit command.

To display a single resource limit, you can pass specific parameters in the ulimit command, some of which are listed below:

Ulimit-n-> shows the limit on the number of files open

Ulimit-c-> displays the core dump file size

Umilit-u-> shows the maximum number of user processes for logged-in users

Ulimit-f-> shows the maximum file size that a user can have

Umilit-m-> displays the maximum memory size of the logged-in user

Ulimit-v-> displays the maximum memory size limit

Check the hard and soft limits on the number of files opened by the logged-in user using the following command:

How does shashi@Ubuntu ~} $ulimit-Hn1048576shashi@Ubuntu ~} $ulimit-Sn1024 fix the problem of reaching the maximum file count limit?

Let's assume that our Linux server has reached the maximum number of open files, and we want to extend this limit on a system-wide basis, for example, we want to set 100000 to the limit of the number of open files.

Root@ubuntu~] # sysctl-w fs.file-max=100000fs.file-max = 100000

The above changes will take effect until the next restart, so to make them persist after the restart, edit the file / etc/sysctl.conf and add the same parameters

Root@ubuntu~] # vi / etc/sysctl.conffs.file-max = 100000

Save the file and exit.

Run the following command to make the above changes take effect immediately without logging out and restarting.

Root@ubuntu~] # sysctl-p

Now verify that the new changes take effect.

Root@ubuntu~] # cat / proc/sys/fs/file-max100000

Use the following command to find out the number of file descriptors currently in use:

[root@ansible ~] # more / proc/sys/fs/file-nr1216 0 100000

Note: the command sysctl-p is used to commit changes without restarting and logging out.

Set user-level resource limits through limit.conf files

The / etc/sysctl.conf file is used to set system-wide resource limits, but if you want to set resource limits for specific users such as Oracle, MariaDB, and Apache, you can do so through the / etc/security/limits.conf file.

The example limits.conf is as follows

Root@ubuntu~] # cat / etc/security/limits.conf

Suppose we want to set hard and soft limits on the number of open files for linuxtechi users, and edit the file / etc/security/limits.conf and add the following lines for oracle users to set hard and soft limits on the number of open processes:

# hard limit for max opened files for linuxtechi userlinuxtechi hard nofile 4096# soft limit for max opened files for linuxtechi userlinuxtechi soft nofile 1024 # hard limit for max number of process for oracle useroracle hard nproc 8096# soft limit for max number of process for oracle useroracle soft nproc 4096

Save the file and exit.

Note: if you want to impose resource restrictions on a group rather than a user, you can also enter @ instead of the user name through the limits.conf file, and the rest are the same, as shown in the following example

# hard limit for max opened files for sysadmin group@sysadmin hard nofile 4096 # soft limit for max opened files for sysadmin group@sysadmin soft nofile 1024

Verify that the new changes take effect:

~] # su-linuxtechi~] $ulimit-n-H4096~] $ulimit-n-S1024 ~] # su-oracle~] $ulimit-H-u8096~] $ulimit-S-u4096

Note: the other major command used is lsof, which can be used to find out how many files are currently open, which is very helpful to administrators.

The above is all the contents of the article "how to set ulimit and file descriptor limits on Linux servers". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report