zoukankan      html  css  js  c++  java
  • SSH连接自动断开的解决方法(deb/rpm)

    #########

    修改后的:

    ##

    # tail -f -n 20 sshd_config
    #MaxStartups 10:30:60
    #Banner /etc/issue.net

    # Allow client to pass locale environment variables
    AcceptEnv LANG LC_*

    Subsystem sftp /usr/lib/openssh/sftp-server

    # Set this to 'yes' to enable PAM authentication, account processing,
    # and session processing. If this is enabled, PAM authentication will
    # be allowed through the ChallengeResponseAuthentication and
    # PasswordAuthentication. Depending on your PAM configuration,
    # PAM authentication via ChallengeResponseAuthentication may bypass
    # the setting of "PermitRootLogin without-password".
    # If you just want the PAM account and session checks to run without
    # PAM authentication, then enable this but set PasswordAuthentication
    # and ChallengeResponseAuthentication to 'no'.
    UsePAM yes
    ClientAliveInterval 30
    ClientAliveCountMax 3860000

    ########

     

    SSH连接自动断开的解决方法

    使用SSH连接远程服务器时,如果长时间不操作,SSH连接上就没有数据传输,此时连接会自动断开,常见的错误提示是:

    1
    Write failed: Broken pipe

    这种超时断开机制估计是出于安全考虑设计的,不过这也会对正常使用造成一定影响,需要进行一些设置来避免这一问题。

    核心思路就是定时发送心跳包,这样就可以保证连接上始终有数据传输,就不会触发超时断开了。客户端ssh和服务器端sshd均支持此功能,只需要配置一下就可以了,以下方法选择其一即可。

    服务器端配置

    如果在服务器端进行配置的话,所有连接到此服务器的会话都会产生效果。

    修改/etc/ssh/sshd_config文件,在其中添加一行:

    1
    ClientAliveInterval  30

    这样服务器端每隔30s就会向客户端发送一个keep-alive包,以此保持连接不会断开。还可以指定发送keep-alive包的最大次数:

    1
    ClientAliveCountMax  60

    若发送了60个keep-alive包后客户端依然没有响应,则断开SSH连接,如果不指定此参数的话会一直发送下去,也就是永远不断开连接。

    客户端配置

    如果没有服务器端的权限,也可在客户端进行配置,这样这个客户端所发起的所有会话都会产生效果。

    修改/etc/ssh/ssh_config文件,与服务器端配置类似,添加以下两个参数即可:

    1
    2
    ServerAliveInterval  
    ServerAliveCountMax

    此时就是客户端定时向服务器端发送keep-alive包。

    会话配置

    如果不希望或不能修改配置文件,也可以在每次建立SSH连接时通过-o参数指定当前会话配置:

    1
    ssh -o ServerAliveInterval=30 root@192.168.1.1

    参考资料:
    ssh连接linux服务器不断开- “Write failed: Broken pipe”

  • 相关阅读:
    MPTCP iperf 发包方式
    Java 中的覆盖@Override注解 写与不写的一点点理解
    servlet 方法有哪些
    java 获取HTTP 头部信息
    七种访问方式(get post及上传文件)
    Enumeration接口的用法
    HTTP头部详解及使用Java套接字处理HTTP请求
    以debug模式启动tomcat服务器
    第一个servlet 使用out输出html文档
    基于Servlet3.0的文件上传
  • 原文地址:https://www.cnblogs.com/xuanbjut/p/12610718.html
Copyright © 2011-2022 走看看