Write failed: Broken pipe错误SSH错误,SSH登陆报错Write failed: Broken pipe

2020-01-09
0评论
/
1209阅读
爱搜啊

Write failed: Broken pipe错误SSH错误,SSH登陆报错Write failed: Broken pipe

在使用SSH管理服务器的时候,如果你很长一段时间不登录,那么很可能等待着你的就是

“Write failed: Broken pipe”

造成这个错误的原因是SSH空闲链接时间太长导致的,所以,我们需要修改SSH默认配置来让它自动关闭链接。

在SSH的配置当中,有两个命令可以实现这个功能,它们分别是

//ServerAliveInterval

         Sets a timeout interval in seconds after which if no data has

         been received from the server, SSH(1) will send a message through

         the encrypted channel to request a response from the server.  The

         default is 0, indicating that these messages will not be sent to

         the server.  This option applies to protocol version 2 only.

//ClientAliveInterval

         Sets a timeout interval in seconds after which if no data has

         been received from the client, sshd(8) will send a message

         through the encrypted channel to request a response from the

         client.  The default is 0, indicating that these messages will

         not be sent to the client.  This option applies to protocol

         version 2 only.

这两个命令前者应用在客户机上,后者应用在服务器上,如果你没有管理员权限,那么前者是很好的选择。
这两个的功能是相同的,都是开启一个发送keep-alive包的功能,这样会允许客户端/服务器在一定时间内发送一个特定的包给对方,一旦超时,则说明断线,就关闭链接。这样就避免了链接长时间挂着导致报错。而且,一旦再次报错,稍等片刻便可以再次登录啦。

总之,二者选择其一即可。

服务端修改办法

我们先来说服务器端的修改:

//编辑文件:

/etc/ssh/sshd_config

//在内容末尾添加如下语句:

ClientAliveInterval 60

//保存后重启服务:

/etc/init.d/ssh restart

这样,当服务器连续60秒没有接收到来自客户端的keep-alive包,就会关闭会话连接了。

客户端修改办法

接下来是客户端的修改办法:

如果你没有服务器权限,那么这是个不错的选择——而且,这种办法还有个进阶的使用方法——针对某个服务器单独设置idle时长:

//编辑文件:

~/.ssh/config

//在里边添加如下语句:

ServerAliveInterval 60

//针对某一服务器的写法:

//使用如下选项连接服务器:

ssh -o ServerAliveInterval=60 user@sshserver

解决SSH登陆 Write failed: Broken pipe的办法

再通过su - www时,提示如下:

su: cannot set user id: Resource temporarily unavailable

首先去查看了下/etc/profile文件,也有如下的ulimit配置:

ulimit -S -c 0 > /dev/null 2>&1 ulimit -HSn 65000 ulimit -u 65000

注:后面的-u参数为最大进程数,如果害怕其他用户通过fork死循环耗完本机资源,可以适当减少该值。默认该值为1024 。

接着看/etc/security/limits.conf 文件,发现其下面已新增了nofile的值 ,如下:

www soft nproc 65535 www hard nproc 65535 * soft nofile 65535 * hard nofile 65535

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

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

* soft nproc 1024 root soft nproc unlimited

1024大小显然对我运行程序的www用户来说,太少了点。更改为65535后,再su - www时,问题解决 。烦人的提示不再有 。


本站附件分享,如果附件失效,可以去找找看

诚通网盘附件百度网盘附件


标签: SSH
于2020-01-09发布