【解決原理】Centos7系統通過ssh連接遠程服務器和文件上傳下載

爲了學習,不得不把個人電腦系統變爲了Linux,系統爲Centos7,有時候還需要連接外部的服務器,在Windows系統下,使用Xftp系列的軟件可以很方便的在圖型化界面操作,但是如果電腦自身就是Linux系統,該怎麼進行遠程連接呢?這裏需要用到Linux中的一種遠程連接方式:ssh;

ssh -p 1111 [email protected] #把1111替換成服務器端口,uname替換成你在服務器已經註冊過的用戶名,000.000.000.000是服務器的ip地址
第一次連接可能會讓你同意一些設置,選擇yes就行,然後輸入密碼,要查看當前有多少個處於登錄狀態的用戶,可以使用who命令查看。在這裏插入圖片描述
我這樣操作後,就可以順利的登陸遠程的節點了,由於沒辦法對截圖進行打碼,這裏只對登陸成功有個小的截圖(上圖)
如果想退出,在命令行輸入exit就直接退出了,如圖順利退出:
在這裏插入圖片描述
注意我們在遠程連接的過程中,如果很長時間沒操作,會出現系統超時斷開連接的情況,這個怎麼辦呢?主要是由於默認的連接超時時間很短,經常斷開,底層原理請繼續往下看:
1、修改文件 # vi /etc/ssh/sshd_config
vi /etc/ssh/sshd_config
找到 100多行,可以使用 set nu 查看行號 方便快速查詢
初始的內容爲:
#ClientAliveInterval 0
#ClientAliveCountMax 3
修改爲(去除#註釋然後設置以下的值,那麼爲什麼這樣設置呢?請繼續往下看)
ClientAliveInterval 60
ClientAliveCountMax 5
2.重啓sshd服務
systemctl restart sshd
這樣連接centos7長時間不操作也不會連接超時中斷了。

這裏分析下防止超時斷開連接的原理:

我們先來看設置那兩個參數是什麼?

ClientAliveCountMax
Sets the number of client alive messages (see below) which may be sent without sshd(8) receiving any messages back from the client. If this threshold is reached while client alive messages are being sent, sshd will disconnect the client, terminating the ses-sion. It is important to note that the use of client alive messages is very different from TCPKeepAlive (below). The client alive messages are sent through the encrypted channel and therefore will not be spoofable. The TCP keepalive option enabled by TCPKeepAlive is spoofable. The client alive mechanism is valuable when the client or server depend on knowing when a connection has become inactive.The default value is 3. If ClientAliveInterval (see below) is set to 15, and ClientAliveCountMax is left at the default, unresponsive SSH clients will be disconnected after approximately 45 seconds. This option applies to protocol version 2 only.
ClientAliveInterval
Sets a timeout interval in seconds after which if no data has been received from the client, sshd(8) will send a message through the encrypted channel to request a response from the client. The default is 0, indicating that these messages will not be sent to the client. This option applies to protocol version 2 only

.其實以上的英文大致意思是:

ClientAliveInterval 參數的單位是秒,爲服務器端向客戶端請求消息的時間間隔默認是0,不發送,比如你設置爲60,就是1分鐘,表示每分鐘發送一次,然後客戶端響應,這樣就保持長連接了。

ClientAliveInterval 60

對於ClientAliveCountMax
使用默認值爲3。表示服務器發出請求後客戶端沒有響應的次數達到一定值,就自動斷開,正常情況下,客戶端不會不響應
指如果發現客戶端沒有相應,則判斷一次超時,這個參數設置允許超時的次數,比如5。

ClientAliveInterval 60

ClientAliveCountMax 5

則代表允許超時 300秒 = 5分鐘。這個其實就可以保持長連接了,一般客戶端和服務端的TCP連接耗時不會超過5分鐘,爲保險起見,你也可以將兩個值設置的大一點,如果最終連接還是斷開,只能說明服務端和客戶端有一端斷電或者網絡異常了,但不會由於TCP連接不上而產生超時斷開。

文件上傳下載:
本地的系統是 Linux系統,Centos7。遠程連接的操作系統也是Linux,在windows系統下當然還可以採用Xshell 和 Xftp 進行文件的上傳和下載,在Linux下需要使用 scp命令進行上傳和下載了。

1、從服務器上下載文件 scp -p port_num username@servername:/path/filename /Users/mac/Desktop(本地目錄),其中,-p 是可以選擇的,一般默認的服務器端口都是 22 ,如果時自定義的端口,可以加上端口號,一般不用加。
例如:scp -p 12 [email protected]:/root/test.txt /Users/mac/Desktop就是將服務器上的/root/test.txt下載到本地的/Users/mac/Desktop目錄下。注意兩個地址之間有空格!
2、上傳本地文件到服務器 scp /path/filename username@servername:/path ;
例如scp /Users/mac/Desktop/test.txt [email protected]:/root/
3、從服務器下載整個目錄 scp -r username@servername:/root/(遠程目錄) /Users/mac/Desktop(本地目錄)
例如:scp -r [email protected]:/root/ /Users/mac/Desktop/
4、上傳目錄到服務器 scp -r local_dir username@servername:remote_dir
例如:scp -r test [email protected]:/root/ 把當前目錄下的test目錄上傳到服務器的/root/ 目錄
注:目標服務器要開啓寫入權限。

借鑑:鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章