SCP(安全复制协议)是一种网络文件传输协议,可以在远程系统和本地主机或两个远程位置之间轻松安全地传输文件。它使用安全外壳(SSH)机制为您的数据和凭据提供身份验证和加密功能。
管理员使用SCPLinux命令来简化Linux服务器之间的数据传输过程,因为它不需要任何登录到服务器。此外,它提供比文件传输协议(FTP)更好的安全性,并且符合PCI安全委员会标准。
在本文中,您将了解如何在远程主机(例如VPS)上使用SCP命令。我们将分解基本的SCP命令并提供示例,以帮助您更好地理解Linux命令的工作原理。
了解SCPSSH命令语法
在两个远程系统之间执行文件传输的SCP命令的基本语法如下所示:
scp[其他选项][[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/]:/[目录和文件名][[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/]:/[目的目录]
这是命令的细分:
- [其他选项]‒可以添加到SCP命令的修饰符。我们稍后会介绍最受欢迎的。
- [source[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e–;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+=’%’+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/]‒托管目标文件的计算机的用户名和IP。
- :/‒通知SCP命令用户将在源目录中键入。
- [目录和文件名]‒文件的位置和名称。
- [[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e–;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+=’%’+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/]‒目标系统的用户名和IP。
- [目标目录]‒将保存文件的目标目录。
以下示例显示了真实场景中的SCP命令:
scp-p[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:/media/scp.png[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:/desktop/destination
如果您要复制到本地主机或从本地主机复制,则不需要IP地址和目标或源路径,如/desktop/folder_name。
专家提示
使用-r选项递归复制整个目录。
使用SCP命令复制文件
SCP最好的一点是它可以让您在两个远程主机或远程机器与本地系统之间安全地传输文件。以下部分将介绍SCP命令如何用于每种类型的传输。
重要的!无论您使用SCP执行何种类型的传输,您都不应该通过SSH连接到远程服务器。
从本地服务器复制到远程主机
我们会将本地文件scp.zip复制到名为root的远程服务器。服务器的IP地址位于用户名之后。
scp/users/Hostinger/desktop/scp.zip[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:/writing/article
如果您没有设置自动SSH客户端确认,系统将提示您输入远程计算机的用户密码并查看进度表。它将如下所示:
[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:
novel3.zip100%00.0KB/s00:00
假设远程服务器设置为在默认SSH端口22以外的端口上侦听SSH连接。在这种情况下,您必须使用-P选项指定该端口。
scp-P2322/users/Hostinger/desktop/scp.zip[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:/writing/article
如果您还想在传输操作期间更改文件的名称,那么您的命令将如下所示(如果您不使用默认端口,请添加–P后跟适当的端口号):
scp/users/Hostinger/desktop/scp.zip[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:/writing/article/howtoscp.zip
使用-r选项后跟相同的命令行来复制具有多个文件或子目录的目录。
scp-r/users/Hostinger/desktop[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:/writing/article
专家提示
要查找目录的完整路径,请导航到适当的目录并使用pwdSSH命令(打印工作目录)。
将远程文件传输到本地计算机
颠倒命令的源系统和目标系统必须反映在语法中。这一次,我们将从同一台远程主机复制scp.zip文件到我们的本地计算机。
scp[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:/writing/articles/SCP.zipUsers/Hostinger/Desktop
执行此命令将提示需要密码的相同SSH登录输出。仅当sudo权限已禁用身份验证或强制SSH客户端在您的计算机上使用私钥时,它才会出现。
在远程主机之间安全地移动文件
要将文件从一台远程服务器复制到另一台,您必须在终端中运行以下命令后输入两个帐户的密码:
scp[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:/writing/article/scp.zip[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:/publishing
上面的命令将源文件/writing/article/scp.zip从第一台主机复制到第二台主机。要复制文件夹,请使用-r选项并指定文件夹路径而不是其中的文件。
在正常情况下,文件会直接从第一台远程服务器发送到第二台。但是,如果您想通过您的机器重新路由操作,请添加-3选项,如以下命令所示:
scp-3[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:/writing/article/scp.zip[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:/publishing
使用带选项的SCP命令
上面的例子有几个修改命令的SCP选项。有许多单字符形式的常用选项(-o)及其描述性等效项(–option)。
例如,-q参数提示系统以安静模式运行操作。它禁用进度表输出以及警告和诊断消息。使用-v参数启用详细输出以便于调试:
scp-v/users/Hostinger/desktop/scp.zip[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/
-4和-6选项确定使用的协议版本,即IPv4或IPv6。您还可以使用address-family关键字更全面地配置IP地址要求。
scp-6[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/:/users/Hostinger/desktop/scp.zip
-p选项保留源文件中的修改、访问时间和模式。
scp-p/users/Hostinger/desktop/scp.zip[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/
使用-stat选项检查复制文件的详细信息,例如创建日期和文件大小。
-C选项在传输操作发生时启用大文件或目录的数据压缩。请记住,此选项不适用于压缩文件,例如.zip和.rar档案。
scp-C文件夹[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/
不要与-C混淆,-c选项可让您指定客户端应使用的加密算法。您可以输入的一些值是“aes256-ctr”、“aes256-cbc”和“3des-cbc”。shell配置中的默认选项是“AnyStdCipher”。
scp-c3des-cbc/users/Hostinger/desktop/scp.zip[email protected]/*<![CDATA[*/!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))returnt[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)=128)o=(parseInt(m)1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/*]]>*/
重要的!使用SCP选项时要小心,因为它们区分大小写。
结论
安全复制协议使得在网络上的多个远程系统之间传输文件变得更加容易。它使您可以安全地将文件从一台远程主机复制到另一台,而无需通过加密连接登录。此外,这种远程文件传输方法使用安全外壳对您的数据进行加密,确保传输信息的机密性。
本文来源:https://www.yuntue.com/post/89957.html | 云服务器网,转载请注明出处!

微信扫一扫打赏
支付宝扫一扫打赏