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-max 146013

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

[root@centos ~] # cat / proc/sys/fs/file-max 149219 [root@debian ~] # cat / proc/sys/fs/file-max 73906

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-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) 5731 max locked memory (kbytes,-l) 64 max memory size (kbytes) -m) unlimited open files (- n) 1024 pipe size (512 bytes,-p) 8 POSIX message queues (bytes,-Q) 819200 real-time priority (- r) 0 stack size (kbytes,-s) 8192 cpu time (seconds,-t) unlimited max user processes (- u) 5731 virtual memory (kbytes) -v) unlimited file locks (- x) unlimited

CentOS system

Shashi@centos ~} $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) 5901 max locked memory (kbytes,-l) 64 max memory size (kbytes) -m) unlimited open files (- n) 1024 pipe size (512 bytes,-p) 8 POSIX message queues (bytes,-Q) 819200 real-time priority (- r) 0 stack size (kbytes,-s) 8192 cpu time (seconds,-t) unlimited max user processes (- u) 5901 virtual memory (kbytes) -v) unlimited file 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:

Shashi@Ubuntu ~} $ulimit-Hn 1048576 shashi@Ubuntu ~} $ulimit-Sn 1024

How do I fix the problem of reaching the maximum number of files?

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=100000 fs.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.conf fs.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-max 100000

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

[root@ansible ~] # more / proc/sys/fs/file-nr 1216 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

Limits-conf-linux-part1

Limits-conf-linux-part2 assumes that 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 user linuxtechi hard nofile 4096 # soft limit for max opened files for linuxtechi user linuxtechi soft nofile 1024 # hard limit for max number of process for oracle user oracle hard nproc 8096 # soft limit for max number of process for oracle user oracle 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-H 4096 ~] $ulimit-n-S 1024 ~] # su-oracle ~] $ulimit-H-u 8096 ~] $ulimit-S-u 4096

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