如何使用scp
将文件夹从远程复制到本地主机?
我使用ssh
登录我的服务器。
然后,我想将远程文件夹foo
复制到本地/home/user/Desktop
。
怎么做到这一点?
问题是如何使用scp
命令将文件夹从远程复制到本地。
$ scp -r userRemote@remoteIp:/path/remoteDir /path/localDir
但是使用sftp
这是更好的方法 - SSH文件传输协议(也称为安全文件传输协议,或SFTP)是一种网络协议,可通过任何可靠的数据流提供文件访问,文件传输和文件管理。(维基百科) 。
$ sftp user_remote@remote_ip
sftp> cd /path/to/remoteDir
sftp> get -r remoteDir
Fetching /path/to/remoteDir to localDir 100% 398 0.4KB/s 00:00
有关sftp
命令的帮助,请输入help
或?
。
我不知道为什么,但我不得不在源服务器指令之前使用本地文件夹。使它工作
scp -r . [email protected]:/usr/share/nginx/www/example.org/
要使用scp的全部功能,您需要执行以下步骤:
然后,例如,如果你有〜/ .ssh / config:
Host test
User testuser
HostName test-site.com
Port 22022
Host prod
User produser
HostName production-site.com
Port 22022
你可以节省密码输入并简化scp语法,如下所示:
scp -r prod:/path/foo /home/user/Desktop # copy to local
scp -r prod:/path/foo test:/tmp # copy from remote prod to remote test
此外,您将能够使用远程路径完成:
scp test:/var/log/ # press tab twice
Display all 151 possibilities? (y or n)
更新:
要启用远程bash-completion,你需要在<source>
和<target>
主机上都有bash-shell,并且正确地运行bash-completion。有关更多信息,请参阅相关问题
How to enable autocompletion for remote paths when using scp? SCP filename tab completion
将所有本地位置复制到远程位置(上载)
scp -r /path/from/destination username@hostname:/path/to/destination
将所有从远程位置复制到本地位置(下载)
scp -r username@hostname:/path/from/destination /path/to/destination
自定义端口,其中xxxx
是自定义端口号
scp -r -P xxxx username@hostname:/path/from/destination /path/to/destination
将当前目录从远程复制到本地
scp -r username@hostname:/path/from/file .
救命:
-r
递归复制所有目录和文件/
的完整位置,获取pwd
的完整位置scp
将替换所有现有文件hostname
将是主机名或IP地址-P portnumber
注意:由于防火墙中不允许使用端口,有时自定义端口将无法工作,因此请确保防火墙中允许使用自定义端口进行传入和传出连接
我一直用的是:
scp -r username@IP:/path/to/server/source/folder/ .
。 (点):这意味着current folder
。所以从服务器复制并粘贴到此处。
IP:可以是像125.55.41.311
这样的IP地址,也可以像ns1.mysite.com
一样托管。
最好先在远程服务器上压缩目录:
tar czfP backup.tar.gz /path/to/catalog
其次,从远程下载:
scp [email protected]:/path/to/backup.tar.gz .
最后,解压缩文件:
tar -xzvf backup.tar.gz
如果您有一个可以从远程位置下载的文件,如果您不太关心安全性,请尝试将scp默认加密(Triple-DES)更改为“blowfish”。
这将大大减少文件复制时间。
scp -c blowfish -r [email protected]:/path/to/foo /home/user/Desktop/
典型情况,
scp -r -P port username@ip:/path-to-folder .
用样本解释,
scp -r -P 27000 [email protected]:/tmp/hotel_dump .
哪里,
port = 27000
username = "abc" , remote server username
path-to-folder = tmp/hotel_dump
. = current local directory
如果您遇到“身份验证失败太多”,请指定已添加到服务器ssh服务器的确切SSH密钥:
scp -r -i /path/to/local/key [email protected]:/path/to/folder /your/local/target/dir