在 CentOS 中,您可以通过以下步骤设置 ulimit:
-
打开终端。
-
使用文本编辑器打开
/etc/security/limits.conf
文件。例如,您可以使用 nano 编辑器:sudo nano /etc/security/limits.conf
-
在文件末尾添加以下行,根据您的需求设置软限制(soft limit)和硬限制(hard limit)。这里的
username
是要设置 ulimit 的用户名,soft_limit
和hard_limit
分别表示软限制和硬限制的值。username soft limit value username hard limit value
例如,如果您想为用户 “john” 设置最大文件描述符数量为 4096,您可以这样写:
john soft nofile 4096 john hard nofile 4096
请注意,
nofile
表示文件描述符的限制。根据您的需求,您还可以设置其他类型的限制,例如进程数、CPU 时间等。 -
保存并关闭文件。
-
为了让更改生效,您需要重新登录到系统或者重启系统。
请注意,这些设置仅适用于通过 PAM(Pluggable Authentication Modules)进行身份验证的用户。对于 root 用户,您可能需要直接编辑 /etc/pam.d/system-auth
或 /etc/pam.d/password-auth
文件,并在其中添加以下行:
session required pam_limits.so
然后保存并关闭文件,重新登录或重启系统以使更改生效。