Ubuntu用terminal进行ssh连接,以及搁置一段时间自动断开问题解决方案
Ubuntu通过SSH连接服务器
- 如果系统没有安装ssh客户端,先安装ssh客户端:
sudo apt-get install ssh or sudo apt-get install openssh-client
- ssh客户端安装好后,运用以下命令连接服务器:
ssh [username]@[servername] -p [port] or ssh [username]@[ip:port]
其中,如果服务器的ssh服务端口是22,[port]可以省略。例如ssh服务器的域名为example.com,用户名为test,ssh服务端口号为22,那么可以用:ssh test@example.com
来登录。
Ubuntu用terminal进行ssh连接搁置一段时间自动断开问题解决方案
- ssh客户端设置
打开/etc/ssh/ssh_config文件:sudo vim /etc/ssh/ssh_config
在文件最后添加:ServerAliveInterval 30 ServerAliveCountMax 18
ServerAliveInterval 30表示ssh客户端会每30秒发送一个KeepAlive请求,保证终端不会因为超时空闲而断开连接.
ServerAliveCountMax 18 表示ssh客户端发出请求后,服务器端没有响应得次数达到18,就自动断开连接,正常情况下,服务器不会不响应。 - 服务器端设置
打开/etc/ssh/ssh_config文件:sudo vim /etc/ssh/ssh_config
在文件最后添加:ClientAliveInterval 30 ClientAliveCountMax 18
ClientAliveInterval 30表示ssh服务器端会每30秒向客户端发送一次请求,保证不会因为超时空闲而断开连接.
ClientAliveCountMax 18表示ssh服务器端发出请求后,客户端没有响应得次数达到18,就自动断开连接,正常情况下,客户端不会不响应。
然后重启ssh服务:service sshd restart
以上两种方案选择一种配置即可。
- 不修改ssh配置文件
在命令参数ssh中添加 -o ServerAliveInterval=30:ssh [username]@[servername] -o ServerAliveInterval=30
本作品采用《CC 协议》,转载必须注明作者和本文链接