通过 SSH 传输文件[已关闭]

问题描述 投票:0回答:4

我正在命令行上通过 SSH 连接到远程服务器,并尝试使用

scp
命令将目录复制到本地计算机上。但是,远程服务器返回此“使用”消息:

[Stewart:console/ebooks/discostat] jmm% scp -p ./styles/
usage: scp [-1246BCEpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
           [-l limit] [-o ssh_option] [-P port] [-S program]
           [[user@]host1:]file1 [...] [[user@]host2:]file2
[Stewart:console/ebooks/discostat] jmm%

我希望能够双向传输文件。根据我的阅读,我认为上面的命令适用于下载,

scp -p [localpath] [remotepath]
适用于上传?

ssh scp
4个回答
798
投票

你需要在某个地方

scp
做某事。您有
scp ./styles/
,所以您说的是安全复制
./styles/
,但不是将其复制到哪里。

一般要下载的话就去:

# download: remote -> local
scp user@remote_host:remote_file local_file 

其中

local_file
实际上可能是放置要复制的文件的目录。要上传,则相反:

# upload: local -> remote
scp local_file user@remote_host:remote_file

如果你想复制整个目录,你将需要

-r
。将
scp
想象为
cp
,只不过您可以使用
user@remote_host:file
指定文件以及本地文件。

编辑:如评论中所述,如果本地和远程主机上的用户名相同,则在指定远程文件时可以省略用户。


178
投票

如果复制到桌面计算机或从桌面计算机复制,请使用 WinSCP,或者如果在 Linux 上,Nautilus 通过“连接到服务器”选项支持 SCP。

scp 只能将文件复制到运行 sshd 的计算机,因此您需要在运行 scp 的远程计算机上运行客户端软件。

如果在命令行上复制,请使用:

# copy from local machine to remote machine
scp localfile user@host:/path/to/whereyouwant/thefile

# copy from remote machine to local machine
scp user@host:/path/to/remotefile localfile

23
投票

您需要指定源和目标,如果您想复制目录,您应该查看 -r 选项。

因此,要将 /home/user/whatever 从远程服务器递归复制到当前目录:

scp -pr user@remoteserver:whatever .

20
投票

不,您仍然需要

scp [from] [to]
无论您以哪种方式复制

不同的是,你需要

scp -p server:serverpath localpath

© www.soinside.com 2019 - 2024. All rights reserved.