![Ubuntu 两台机器之间的 ssh 连接问题](https://linux22.com/image/1062427/Ubuntu%20%E4%B8%A4%E5%8F%B0%E6%9C%BA%E5%99%A8%E4%B9%8B%E9%97%B4%E7%9A%84%20ssh%20%E8%BF%9E%E6%8E%A5%E9%97%AE%E9%A2%98.png)
我有两个全新安装的 Ubuntu 16.04。这两个ssh -V
都
OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g 1 Mar 2016
当我从机器 A(笔记本电脑)连接到机器 B(远程)时,我首先立即获得连接,但它总是在几分钟后关闭,我无法再次建立连接。ssh
命令不会在屏幕上显示任何内容,只是等待,什么都没有发生(因此它处于空闲状态并且永远不会连接)。如果我调用ssh -vvv username@remotemachine
我得到
OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g 1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "remotemachine" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to 10.40.2.110 [10.40.2.110] port 22.
debug1: Connection established.
debug1: identity file /home/username/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/username/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/username/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/username/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/username/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/username/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/username/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/username/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
有趣的是,如果我现在转到ssh
另一台机器并从它连接,它会工作(至少第一次)。当它第一次连接时,ssh -vvv
会在上面显示的消息后写入:
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.2 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to remotemachine as 'klykov'
我想知道哪台机器配置不正确(客户端或服务器)以及如何解决这个问题。
结果该问题是由于本地网络的IP冲突造成的。
答案1
空闲超时在 SSH 服务器和客户端上处理。
因此您需要在客户端和服务器上以秒为单位配置 /etc/ssh/sshd_config TCPKeepAlive
。ServerAliveInterval
ssh_config
ClientAliveInterval
sudo nano /etc/ssh/sshd_config
取消注释或编辑
TCPKeepAlive yes
ClientAliveInterval 600 # Timeout after 10 minutes, uncomment if no timeout
然后重新启动服务(或等效服务)
/etc/init.d/ssh restart
在客户端上
nano .ssh/config
Host *
TCPKeepAlive=yes
ServerAliveInterval 600 # Same logic as above