---
title: 限制容器的文件句柄和fork进程数
description: "为避免攻击者在容器内使用命令启动fork炸弹，造成拒绝服务，建议用户设置全局默认的ulimit，对创建的文件句柄、进程数进行限制。"
url: https://www.hiascend.com/document/detail/zh/mindcluster/latest/toolbox/toolboxug/toolboxug_0131.html
sourcePath: /source/zh/mindcluster/2610/toolbox/toolboxug/toolboxug_0131.html
indexId: 32275629b2e66b3852258eb5febadc55f339437acaec56bf81fd470afe43dd1965
---
# 限制容器的文件句柄和fork进程数

为避免攻击者在容器内使用命令启动fork炸弹，造成拒绝服务，建议用户设置全局默认的ulimit，对创建的文件句柄、进程数进行限制。

1. 打开配置文件。

  - CentOS 7.6默认为“/usr/lib/systemd/system/docker.service”文件。
  - Ubuntu18.04默认为“/lib/systemd/system/docker.service”文件。
2. 修改配置文件。
  在配置文件中找到“/usr/bin/dockerd”所在行，并在该行后面增加nofile（创建的文件句柄）参数和nproc（进程）参数的限制。

  修改示例如下，请根据实际情况设置对应的值。

```
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
/usr/bin/dockerd
--default-ulimit nofile=20480:40960 --default-ulimit nproc=1024:2048
```


  其中--default-ulimit nproc=1024:2048表示限制进程数量为1024个，可以在进程中修改该值，但是不能超过2048，且第一个值要小于或等于第二个值。nofile配置含义同nproc。
