ulimit 是一个用于控制 shell 进程资源限制的命令行工具。通过使用 ulimit,您可以限制用户或进程可以使用的系统资源,从而提高系统的安全性。以下是如何在 Debian 系统中使用 ulimit 来提高安全性的方法:
限制用户资源使用:
/etc/security/limits.conf 文件。username hard core 0
username hard nproc 100
username hard nofile 1024
username soft core 0
username soft nproc 100
username soft nofile 1024
其中 username 是要限制的用户名,core 是核心文件大小限制,nproc 是允许的最大进程数,nofile 是允许的最大文件描述符数。限制 shell 资源使用:
/etc/pam.d/common-session 和 /etc/pam.d/common-session-noninteractive 文件。session required pam_limits.so
限制单个进程的资源使用:
ulimit 命令设置资源限制。例如:ulimit -u 100 # 限制进程的用户数
ulimit -n 1024 # 限制进程可以打开的文件描述符数
ulimit -m 100000 # 限制进程的内存使用量(以 KB 为单位)
ulimit -t 3600 # 限制进程的 CPU 时间(以秒为单位)
限制系统范围内的资源使用:
/etc/sysctl.conf 文件以设置系统范围内的资源限制。例如:fs.file-max = 100000
kernel.pid_max = 4194303
net.core.somaxconn = 1024
sudo sysctl -p 以应用更改。监控资源使用情况:
ulimit -a 命令查看当前用户的资源限制。ulimit -H 和 ulimit -S 分别查看硬限制和软限制。watch 命令或 top、htop 等工具实时监控系统资源使用情况。通过以上方法,您可以使用 ulimit 在 Debian 系统中设置资源限制,从而提高系统的安全性。请注意,某些设置可能需要 root 权限才能生效。