su: cannot set user id: Resource temporarily unavailable【转】

今天R&D所在主机出现su: cannot set user id: Resource temporarily unavailable资源不可用报错,直接通过其他机器ssh huyuh@xxx.xxx.xxx.xxx时,提示 Write failed: Broken pipe。


进行排查发现机器内存使用已经没有了,进行临时的内存回收操作:
sync ; sync 将内存数据刷新到磁盘

echo 3 > /proc/sys/vm/drop_caches 内存释放
echo 0 > /proc/sys/vm/drop_caches 默认

To free pagecache:
echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes):
echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache:
echo 3 > /proc/sys/vm/drop_caches


同时查看用户进行数的占用,发现用户占用数量过多,而默认的用户进程数量为1024,所以就出现上面的问题,经过调优以后,再通过su - huyuh正常。

1、/etc/profile
首先去查看了下/etc/profile文件,也有如下的ulimit配置:
ulimit -S -c 0 > /dev/null 2>&1
ulimit -HSn 65000
ulimit -u 65000
注:后面的-u参数为最大进程数,如果害怕其他用户通过fork死循环耗完本机资源,可以适当减少该值。默认该值为1024 。

2、/etc/security/limits.conf(此处设置,/etc/profile可不用设置)
接着看/etc/security/limits.conf 文件,发现其下面已新增了nofile的值 ,如下:

huyuh soft nproc 65535
huyuh hard nproc 65535
* soft nofile 65535
* hard nofile 65535


注:limits.conf文件实际上就是ulimit命令的配置文件。nproc为打开的最大进程数,nofile为打开的最大文件数。该处和上面的/etc/profile是重复设置的。实现上该处增加了以后,/etc/profile就不用再做配置的,而且该处配置更规范些,可以对用户进行限制。

3、/etc/security/limits.d/90-nproc.conf

即然以上两处都做了设置,还是有上面的提示,神奇了。后来又乱折腾了半天,突然想到之前在centos 6.3版本配置的时候,发现centos 6.X以后新增了一个/etc/security/limits.d/90-nproc.conf 文件,用于控制nproc 。这里面的默认配置是

* soft nproc 1024
root soft nproc unlimited
1024大小显然对我运行程序的单个开发用户来说,太少了点。更改为65535后,再su - huyuh时,问题解决完成。

转自

su: cannot set user id: Resource temporarily unavailable - Linux系统教程
http://www.linuxdiyf.com/linux/17096.html

原文地址:https://www.cnblogs.com/paul8339/p/6727792.html