【解决原理】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/ 目录
注:目标服务器要开启写入权限。

借鉴:链接

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