最近自己買了個站群伺服器,用 ssh 連線時一段時間不操作就要重新連線,讓人很是麻煩。現在終於解決了,步驟很簡單。
登陸站群伺服器後輸入 lsb_release -a 可以檢視系統版本,此命令適用於所 有的 linux,包括 Redhat 、 SuSE 、 Debian 等發行版,但是在 debian 下要安裝 lsb 。如:
[root@chen /]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.3.1611 (Core)
Release: 7.3.1611
Codename: Core
此方法適用於 centos7 的版本。用文字編譯器開啟 ssh 服務裡面的一個配置檔案。程式碼如下:
[root@chen /]# vi /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
此時已經進入配置檔案裡面了,輸入 i 進入編輯模式,找到如下程式碼:
#PermitUserEnvironment no#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax3
#ShowPatchLevel no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
將
#ClientAliveInterval 0
#ClientAliveCountMax3
去掉註釋並改為
ClientAliveInterval 60
ClientAliveCountMax 3
ClientAliveInterval 指定了站群伺服器端向客戶端請求訊息 的時間間隔, 預設是 0, 不傳送. 而 ClientAliveInterval 60 表示每分鐘傳送一次, 然後客戶端響應, 這樣就保持長連線了.ClientAliveCountMax, 使用預設值 3 即可.ClientAliveCountMax 表示站群伺服器發出請求後客戶端沒有響應的次數達到一定值, 就自動斷開. 正常情況下, 客戶端不會不響應。 最後一定要記得重啟 ssh 服務,否則是不會生效的,重啟程式碼如下:
[root@chen /]# systemctl restart sshd.service
[root@chen /]#
然後就 OK 啦。另外悄悄告訴你判斷 ssh 服務有沒有啟動的命令是:
[root@chen /]# systemctl status sshd.service
?sshd.service – OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2017-09-21 16:57:34 CST; 2min 35s ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 9640 (sshd)
CGroup: /system.slice/sshd.service
忖 9640 /usr/sbin/sshd -D
看到 running 就是啟動了,如果沒有啟動的話輸入
systemctl start sshd.service
就啟動完成了。設定開機自啟動的話則輸入:
systemctl enable sshd.service