Summary: Challenges such as the number of open files are now commonplace in production environments. Because many applications are Based on Java and Apache, installing and configuring them can result in too many files (file descriptors) being opened. If you open a file descriptor that exceeds the limit set by the default, you may face access control problems and be hindered by the challenge of opening a file. Many production environments come to a standstill as a result.

Fortunately, on linux-based servers, you have the ulimit command, which allows you to view, set, and get the status of open files and configuration details. This command comes with a number of options through which you can set the number of files to open. The following commands are detailed with examples.

View the current limit of open files on any Linux system

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

[root@ubuntu ~]# cat /proc/sys/fs/file-max
146013
Copy the code

The numbers above indicate that the user can open ‘146013’ files per user login session.

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

This clearly shows that each Linux operating system has different open file limits. This is based on the dependencies and applications running on their respective systems.

The ulimit command

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

Ubuntu/Debian

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
Copy the code

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
Copy the code

As we can see here, different operating systems have different limitation Settings. All of these limits can be configured/changed using the ulimit command.

To display individual resource limits, specific parameters can be passed in the ulimit command, some of which are listed below:

  • ulimit -n– > Displays the maximum number of open files
  • ulimit -c– > Displays the size of the core dump file
  • umilit -u– > Displays the maximum number of processes for the logged-in user
  • ulimit -f– > Displays the maximum file size that a user can have
  • umilit -m– > Displays the maximum memory size of the login user
  • ulimit -v– > Displays the maximum memory size

Use the following command to check the hard and soft limits on the number of files opened by the logged-in user:

shashi@Ubuntu ~}$ ulimit -Hn
1048576
shashi@Ubuntu ~}$ ulimit -Sn
1024
Copy the code

How to fix the problem of reaching the maximum file limit?

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

root@ubuntu~]# sysctl -w fs.file-max=100000
fs.file-max = 100000
Copy the code

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

root@ubuntu~]# vi /etc/sysctl.conf
fs.file-max = 100000
Copy the code

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
Copy the code

Now verify that the new changes take effect.

root@ubuntu~]# cat /proc/sys/fs/file-max
100000
Copy the code

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
Copy the code

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

Set user-level resource limits using the limiter. Conf file

/ etc/sysctl. Conf file is used to set up the system of resource constraints, but if you want to for Oracle, MariaDB and Apache specific users to set resource constraints, you can through the/etc/security/limits file to be realized.

Example limits.conf is shown below,

root@ubuntu~]# cat /etc/security/limits.conf
Copy the code

Suppose that we want for linuxtechi user Settings to open the file number of the hard limit and the soft limit, and open the process is set for the oracle user number of hard constraints and soft limit, edit the file/etc/security/limits the conf line and add the following:

# 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
Copy the code

Save the file and exit.

Note: If you want to restrict resources to a group instead of a user, you can also use the limits.conf file and type @< group name > instead of the user name. Everything else is 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
Copy the code

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
Copy the code

Note: The other main command used is lsof, which is useful for finding out “how many files are currently open”.

At the end

As mentioned in the introduction section, the ulimit command is powerful enough to help users configure and ensure a smoother application installation without any bottlenecks. This command helps fix the limitation of (open) large files on Linux-based servers.


Via: www.linuxtechi.com/set-ulimit-…

By Shashidhar Soppin, Lujun9972

This article is originally compiled by LCTT and released in Linux China