解决MAC终端ssh连接服务器,空闲断线的办法

方法一 :   配置“/etc/ssh/ssh_config”文件


1 打开MAC终端  /etc/ssh/   enter

2 sudo vim ssh_config (必须用管理员权限编辑,否则无法编辑)

3 shift + i 进入编辑模式,  在host*下面加入 ServerAliveInterval 60   一项, 如下图


4  shift + : 输入wq退出

5 到此完成, 连接你的服务器试试效果吧。

(下面逐行说明的选项设置:
Host *
选项“Host”只对能够匹配后面字串的计算机有效。“*”表示所有的计算机。

ForwardAgent no
“ForwardAgent”设置连接是否经过验证代理(如果存在)转发给远程计算机。

ForwardX11 no
“ForwardX11”设置X11连接是否被自动重定向到安全的通道和显示集(DISPLAY set)。

RhostsAuthentication no
“RhostsAuthentication”设置是否使用基于rhosts的安全验证。

RhostsRSAAuthentication no
“RhostsRSAAuthentication”设置是否使用用RSA算法的基于rhosts的安全验证。

RSAAuthentication yes
“RSAAuthentication”设置是否使用RSA算法进行安全验证。

PasswordAuthentication yes
“PasswordAuthentication”设置是否使用口令验证。

FallBackToRsh no
“FallBackToRsh”设置如果用ssh连接出现错误是否自动使用rsh。

UseRsh no
“UseRsh”设置是否在这台计算机上使用“rlogin/rsh”。

BatchMode no
“BatchMode”如果设为“yes”,passphrase/password(交互式输入口令)的提示将被禁止。当不能交互式输入口令的时候,这个选项对脚本文件和批处理任务十分有用。

CheckHostIP yes
“CheckHostIP”设置ssh是否查看连接到服务器的主机的IP地址以防止DNS欺骗。建议???置为“yes”。

StrictHostKeyChecking no
“StrictHostKeyChecking”如果设置成“yes”,ssh就不会自动把计算机的密匙加入“$HOME/.ssh/known_hosts”文件,并且一旦计算机的密匙发生了变化,就拒绝连接。

IdentityFile ~/.ssh/identity
“IdentityFile”设置从哪个文件读取用户的RSA安全验证标识。

Port 22
“Port”设置连接到远程主机的端口。

Cipher blowfish
“Cipher”设置加密用的密码。

EscapeChar ~
“EscapeChar”设置escape字符。

方法二:服务端配置(不建议此方法)


配置“/etc/ssh/sshd_config”文件

linux系统server

在linux系统中使用ssh连接远程服务器时,可以使用-o的一个参数ServerAliveInterval来设置防止超时的时间。

比如:ssh -o serveraliveinterval=60 username@hostIP

服务器配置修改

修改ssh配置文件/etc/ssh/sshd_config,添加或者修改ClientAliveInterval为“ClientAliveInterval 60”。这个参数的是意思是每1分钟,服务器向客户端发一个消息,用于保持连接。保存后记得重启ssh服务。

修改过后,上面几个办法都可以让ssh保持连接,一直处于alive状态,不会因为没有操作而被服务器强制断线了。


“/etc/ssh/sshd_config”是OpenSSH的配置文件,允许设置选项改变这个daemon的运行。这个文件的每一行包含“关键词-值”的匹配,其中“关键词”是忽略大小写的。下面列出来的是最重要的关键词,用man命令查看帮助页(sshd (8))可以得到详细的列表。

编辑“sshd_config”文件(vi /etc/ssh/sshd_config),加入或改变下面的参数:

# This is ssh server systemwide configuration file.
Port 22
ListenAddress 192.168.1.1
HostKey /etc/ssh/ssh_host_key
ServerKeyBits 1024
LoginGraceTime 600
KeyRegenerationInterval 3600
PermitRootLogin no
IgnoreRhosts yes
IgnoreUserKnownHosts yes
StrictModes yes
X11Forwarding no
PrintMotd yes
SyslogFacility AUTH
LogLevel INFO
RhostsAuthentication no
RhostsRSAAuthentication no
RSAAuthentication yes
PasswordAuthentication yes
PermitEmptyPasswords no
AllowUsers admin

下面逐行说明上面的选项设置:

Port 22
“Port”设置sshd监听的端口号
 

猜你喜欢

转载自blog.csdn.net/CQG1988/article/details/84646977