1. 程式人生 > >解決MAC終端ssh連線伺服器,空閒斷線的辦法

解決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 [email protected]

伺服器配置修改

修改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監聽的埠號