温馨提示×

怎样重置ubuntu ulimit

小樊
40
2026-10-17 07:15:25
栏目: 智能运维

在 Ubuntu 中,ulimit 用于限制用户/进程可使用的系统资源(如打开文件数、进程数等)。要“重置”或“修改” ulimit,需要看你是临时重置(当前 shell / 进程)还是永久重置(所有用户/系统)。

下面分情况说明。


一、查看当前 ulimit

ulimit -a

常见项:

  • -n:打开文件数(file descriptors)
  • -u:最大进程数
  • -c:core 文件大小
  • -s:栈大小

二、临时重置(仅当前 shell / 进程)

1. 直接修改当前 shell

ulimit -n 65536
ulimit -u 4096

✅ 只对当前终端和由其启动的进程有效
❌ 退出 shell 后失效

2. 重置为系统默认值

ulimit -n unlimited

或重新登录:

exit

再登录即可恢复默认。


三、永久重置(推荐做法)

方法 1:通过 /etc/security/limits.conf

编辑文件:

sudo vim /etc/security/limits.conf

示例(针对所有用户):

* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096

说明:

  • soft:警告值
  • hard:最大值
  • nofile:打开文件数
  • nproc:进程数

⚠ 修改后重新登录或重启才生效。


方法 2:针对 systemd 服务(非常重要)

如果是 服务 / 后台进程,limits.conf 可能不生效。

编辑:

sudo vim /etc/systemd/system.conf

修改:

DefaultLimitNOFILE=65536
DefaultLimitNPROC=4096

然后:

sudo systemctl daemon-reexec

或重启系统。


方法 3:针对单个 systemd 服务

sudo systemctl edit nginx

添加:

[Service]
LimitNOFILE=65536

四、Docker / 容器环境

在 Docker 中:

docker run --ulimit nofile=65536:65536 ...

或在 docker-compose.yml:

ulimits:
  nofile:
    soft: 65536
    hard: 65536

五、恢复“出厂默认”

如果不确定改了什么:

  1. 删除 /etc/security/limits.conf 中你加的内容
  2. 还原 /etc/systemd/system.conf
  3. 重启系统

六、常见坑

❌ 普通用户不能提高 hard limit
❌ SSH 登录可能受 UsePAM yes 影响
❌ 云服务器常默认很低(如 1024)


如果你能说明:

  • 是 用户级 还是 服务级
  • 具体想改哪一项(如 nofile)
  • Ubuntu 版本

我可以给你精确命令。

0 踩