如何在 Linux 服务器上设置 ulimit 和文件描述符数限制
ulimit 文件句柄
 **简介**：在生产环境中遇到打开文件数这类的挑战如今已是司空见惯的事情了。因为许多应用程序是基于 Java 和 Apache 的，安装和配置它们可能会导致打开过多的文件（文件描述符）。如果打开的文件描述符超过了默认设置的限制，就可能会面临访问控制问题，受阻于打开文件的挑战。许多生产环境因此而陷入停滞状态。


![](/data/attachment/album/201907/09/121422bv6t76nztjxqoprn.jpg)


幸运的是，在基于 Linux 的服务器上，都有 `ulimit` 命令，通过它可以查看、设置、获取文件打开的状态和配置详情。此命令配备了许多选项，通过这些组合可以设置打开文件的数量。下面逐个命令用示例做了详细说明。


### 查看任何 Linux 系统中当前打开文件数的限制


要在 Linux 服务器上得到打开文件数的限制，请执行以下命令，



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

上面的数字表明用户可以在每个用户登录会话中打开 ‘146013’ 个文件。



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

这清楚地表明，各个 Linux 操作系统具有不同的打开文件数限制。这基于各自系统中运行的依赖关系和应用程序。


### ulimit 命令


顾名思义，`ulimit`（用户限制）用于显示和设置登录用户的资源限制。当我们使用 `-a` 选项运行 `ulimit` 命令时，它将打印登录用户的所有资源限制。现在让我们在 Ubuntu/Debian 和 CentOS 系统上运行 `ulimit -a`，


#### 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
```

#### CentOS 系统



```
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
```

正如我们可以在这里看到的，不同的操作系统具有不同的限制设置。所有这些限制都可以使用 `ulimit` 命令进行配置/更改。


要显示单个资源限制，可以在 `ulimit` 命令中传递特定的参数，下面列出了一些参数：


* `ulimit -n` –> 显示打开文件数限制
* `ulimit -c` –> 显示核心转储文件大小
* `umilit -u` –> 显示登录用户的最大用户进程数限制
* `ulimit -f` –> 显示用户可以拥有的最大文件大小
* `umilit -m` –> 显示登录用户的最大内存大小
* `ulimit -v` –> 显示最大内存大小限制


使用以下命令检查登录用户打开文件数量的硬限制和软限制：



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

### 如何修复达到最大文件数限制的问题？


让我们假设我们的 Linux 服务器已经达到了打开文件的最大数量限制，并希望在系统范围扩展该限制，例如，我们希望将 100000 设置为打开文件数量的限制。



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

上述更改将在下次重启之前有效，因此要使这些更改在重启后仍存在，请编辑文件 `/etc/sysctl.conf` 并添加相同的参数，



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

保存文件并退出。


运行下面命令，使上述更改立即生效，而无需注销和重新启动。



```
root@ubuntu~]# sysctl -p
```

现在验证新的更改是否生效。



```
root@ubuntu~]# cat /proc/sys/fs/file-max
100000
```

使用以下命令找出当前正在使用的文件描述符数量：



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

注意：命令 `sysctl -p` 用于在不重新启动和注销的情况下提交更改。


### 通过 limit.conf 文件设置用户级资源限制


`/etc/sysctl.conf` 文件用于设置系统范围的资源限制，但如果要为 Oracle、MariaDB 和 Apache 等特定用户设置资源限制，则可以通过 `/etc/security/limits.conf` 文件去实现。


示例 `limits.conf` 如下所示，



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

![Limits-conf-linux-part1](/data/attachment/album/201907/09/121436ghw4hqgdlgbdqqdn.jpg)


![Limits-conf-linux-part2](/data/attachment/album/201907/09/121436qkczc2cx2gd2sqc4.jpg)


假设我们要为 linuxtechi 用户设置打开文件数量的硬限制和软限制，而对于 oracle 用户设置打开进程数量的硬限制和软限制，编辑文件 `/etc/security/limits.conf` 并添加以下行：



```
# 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
```

保存文件并退出。


**注意：** 如果你想对一个组而不是用户进行资源限制，那么也可以通过 `limits.conf` 文件，输入 `@<组名>` 代替用户名，其余项都是相同的，示例如下，



```
# 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
```

验证新的更改是否生效：



```
~]# su - linuxtechi
~]$ ulimit -n -H
4096
~]$ ulimit -n -S
1024

~]# su - oracle
~]$ ulimit -H -u
8096
~]$ ulimit -S -u
4096
```

注：其他主要使用的命令是 [lsof](https://www.linuxtechi.com/lsof-command-examples-linux-geeks/)，可用于找出“当前打开了多少个文件”，这命令对管理员非常有帮助。


### 结尾


正如在介绍部分提到的，`ulimit` 命令非常强大，可以帮助用户配置并确保应用程序安装更加流畅而没有任何瓶颈。此命令有助于修复基于 Linux 的服务器中的（打开）大量文件的限制。




---


via: <https://www.linuxtechi.com/set-ulimit-file-descriptors-limit-linux-servers/>


作者：[Shashidhar Soppin](https://www.linuxtechi.com/author/shashidhar/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[zgj1024](https://github.com/zgj1024) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In this post, we will explain how to set ulimit and file descriptors limit in Linux.

Challenges like number of open files in any of the production environment has become common now a day. Since many applications which are Java based and Apache based, are getting installed and configured, which may lead to too many open files, file descriptors etc. If this exceeds the default limit that is set, then one may face access control problems and file opening challenges. Many production environments come to standstill kind of situations because of this.

Luckily, we have “ulimit” command in any of the Linux based server, by which one can see/set/get number of files open status/configuration details. This command is equipped with many options and with this combination one can set number of open files. Following are step-by-step commands with examples explained in detail.

## View Current Open File Limit in Linux


To get open file limit on any Linux server, execute the following command,

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

The above number shows that user can open ‘146013’ file per user login session.

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

This clearly indicates that individual Linux operating systems have different number of open files. This is based on dependencies and applications which are running in respective systems.

## ulimit command


As the name suggests, ulimit (user limit) is used to display and set resources limit for logged in user.When we run ulimit command with -a option then it will print all resources’ limit for the logged in user. Now let’s run “ulimit -a” on Ubuntu / Debian and RHEL 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

**RHEL /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 be seen here different OS have different limits set. All these limits can be configured/changed using “ulimit” command.

To display the individual resource limit then pass the individual parameter in ulimit command, some of parameters are listed below:

- ulimit -n –> It will display number of open files limit
- ulimit -c –> It display the size of core file
- umilit -u –> It will display the maximum user process limit for the logged in user.
- ulimit -f –> It will display the maximum file size that the user can have.
- umilit -m –> It will display the maximum memory size for logged in user.
- ulimit -v –> It will display the maximum memory size limit

Use below commands check hard and soft limits for number of open file for the logged in user

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

#### How to fix the problem when limit on number of maximum files was reached ?

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

Use sysctl command to pass fs.file-max parameter to kernel on the fly, execute beneath command as root user,

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

Above changes will be active until the next reboot, so to make these changes persistent across the reboot, edit the file /etc/sysctl.conf and add same parameter,

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

save and exit file,

Run the beneath command to make above changes into effect immediately without logout and reboot.

root@ubuntu~]# sysctl -p

Now verify whether new changes are in effect or not.

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

Use below command to find out how many file descriptors are currently being utilized:

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

Note: Command “sysctl -p” is used to commit the changes without reboot and logout.

## Set ulimit (User level resource limit) via limit.conf file

“/etc/sysctl.conf” file is used to set resource limit system wide but if you want to set resource limit for specific user like Oracle, MariaDB and Apache then this can be achieved via “/etc/security/limits.conf” file.

Sample limit.conf is shown below,

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

Let’s assume we want to set hard and soft limit on number of open files for linuxtechi user and for oracle user set hard and soft limit on number of open process, edit the file “/etc/security/limits.conf” and add the following lines

# 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 & exit the file.

Note: In case you want to put resource limit on a group instead of users, then it can also be possible via limit.conf file, in place of user name , type **@<Group_Name>** and rest of the items will be same, example is shown below,

# 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 whether new changes are in effect or not,

~]# su - linuxtechi ~]$ ulimit -n -H 4096 ~]$ ulimit -n -S 1024 ~]# su - oracle ~]$ ulimit -H -u 8096 ~]$ ulimit -S -u 4096

Note: Other majorly used command is “[lsof](https://www.linuxtechi.com/lsof-command-examples-linux-geeks/)” which is used for finding out “how many files are opened currently”. This command is very helpful for admins.

#### Conclusion

As mentioned in the introduction section “ulimit” command is very powerful and helps one to configure and make sure application installations are smoother without any bottlenecks. This command helps in fixing many of the number of file limitations in Linux based servers.

Rajicat /proc/sys/fs/file-max shows 146013, but

ulimit -a shows:

open files (-n) 1024

What is the difference

Pradeep Kumarcat /proc/sys/fs/file-max –> shows the limit of max open file for the system wide

ulimit -a –> will show all the resources limit of a specific user

davidhello, ok so do you mean that if I set a value like this it will be add to limits.conf?

ulimit -f 65000

Pradeep KumarHi David,

User level limits are configured via ‘/etc/security/limits.conf’ file. Example is shown in above guide.