現象:/etc/security/limits.conf 已配置 * soft nofile 65535,cat /proc/PID/limits 顯示生效,但 **SSH 登錄後 ulimit -a 仍顯示舊值**(如 1024)。
/etc/pam.d/sshd 缺少 session required pam_limits.so。/etc/ssh/sshd_config 中 UsePAM no(默認 yes,但編譯安裝時若無 --with-pam 參數則無效)。grep pam_limits.so /etc/pam.d/sshd
# 應有輸出:session required pam_limits.so
# 若無,添加至文件末尾
grep -i ^usepam /etc/ssh/sshd_config
# 應為:UsePAM yes
# 若為 no 或註釋,改為 yes 並重啟 sshd
sshd -T 2>/dev/null | grep usepam
# 輸出 usepam 1 表示支持
# 若為 0 或報錯,說明編譯時未加 --with-pam,需重新編譯或改用 RPM 包
systemctl restart sshd
# 新開 SSH 會話測試
ulimit -n
# 應顯示 65535
# /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
root soft nofile 65535
root hard nofile 65535
# 查看進程實際限制
cat /proc/$(pgrep -f "your_app")/limits | grep "Max open files"
# 輸出應為 65535
**核心結論**:SSH 登錄會話屬於 PAM 管理的登錄會話,**必須**同時滿足 UsePAM yes 和 pam_limits.so 加載,limits.conf 才能對登錄用戶生效。編譯安裝 OpenSSH 時務必加上 --with-pam。