---
title: "linux 设置SSH Server保持长时间连接"
author: "香格里拉没有底"
date: "2016-08-10T10:10:22Z"
tags: []
categories: ["操作系统"]
slug: "linux-ssh-server"
draft: false
lastmod: "2016-08-10T10:10:22Z"
id: 2316533
url: "https://onekeyes.iteye.com/blog/2316533"
---

怎样让服务器和一定时间没有任何操作的终端自动断开连接？ # vi /etc/profile 增加： TMOUT=1800 这样30分钟没操作就自动LOGOUT

 

**[OpenSSH](http://www.ha97.com/tag/openssh "OpenSSH")基于安全的理由，如果用户连线到SSH [Server](http://www.ha97.com/tag/server "Server")后闲置一段时间，SSH Server会在超过特定时间后自动终止SSH连线。**本人习惯长时间连接，需要做如下修改：

1、打开ssh配置文件：# vim /etc/ssh/sshd\_config  
加入如下两个参数保存就可以：

> TCPKeepAlive yes  
> ClientAliveCountMax 360

注：前一个参数表示要保持TCP连接，后一个参数表示客户端的SSH连线闲置多长时间后自动终止连线的数值，单位为分钟。

2、重启sshd生效：

> /etc/init.d/sshd restart

**注：此法适用于所有[Linux](http://www.ha97.com/category/linux "Linux")发行版的OpenSSH**

 

**ubuntu设置**

如何让ssh连接[服务器](http://server.chinaitlab.com/)或者sshtunnel保持连接呢?

　　其实也很方便,只要在/etc/ssh/ssh\_config文件里加两个参数就行了

　　1TCPKeepAliveyes

　　2ServerAliveInterval300

　　前一个参数是说要保持连接,后一个参数表示每过5分钟发一个数据包到[服务器](http://server.chinaitlab.com/)表示"我还活着"

　　如果你没有root权限,修改或者创建~/.ssh/ssh\_config也是可以的

 

转自：[http://blog.chinaunix.net/uid-8473611-id-3069386.html](http://blog.chinaunix.net/uid-8473611-id-3069386.html)