本文翻译自:scp with port number specified
I'm trying to scp a file from a remote server to my local machine. 我正在尝试将文件从远程服务器scp到我的本地计算机。 Only port 80 is accessible. 只有80端口可以访问。
I tried: 我试过了:
scp -p 80 username@www.myserver.com:/root/file.txt .
but got this error: cp: 80: No such file or directory
但得到了这个错误: cp: 80: No such file or directory
How do I specify the port number in a scp command? 如何在scp命令中指定端口号?
#1楼
参考:https://stackoom.com/question/hOAq/指定端口号的scp
#2楼
Unlike ssh, scp uses the uppercase P switch to set the port instead of the lowercase p: 与ssh不同,scp使用大写P开关来设置端口而不是小写p:
scp -P 80 ... # Use port 80 to bypass the firewall, instead of the scp default
The lowercase p switch is used with scp for the preservation of times and modes. 小写p开关与scp一起用于保存时间和模式。
Here is an excerpt from scp's man page with all of the details concerning the two switches, as well as an explanation of why uppercase P was chosen for scp: 以下是scp手册页的摘录,其中包含有关两个开关的所有详细信息,以及为scp选择大写P的原因解释:
-P port Specifies the port to connect to on the remote host. -P port指定要在远程主机上连接的端口。 Note that this option is written with a capital 'P', because -p is already reserved for preserving the times and modes of the file in rcp(1). 请注意,此选项使用大写“P”编写,因为-p已保留用于保留rcp(1)中文件的时间和模式。
-p Preserves modification times, access times, and modes from the original file. -p保留原始文件的修改时间,访问时间和模式。
Update and aside to address one of the (heavily upvoted) comments : 更新并在旁边解决其中一个(大量投票)评论 :
With regard to Abdull's comment about scp
option order, what he suggests: 关于Abdull关于scp
期权订单的评论,他建议:
scp -P80 -r some_directory -P 80 ...
..., intersperses options and parameters. ......,散布选项和参数。 getopt(1)
clearly defines that parameters must come after options and not be interspersed with them: getopt(1)
明确定义参数必须在选项之后,而不是穿插它们:
The parameters getopt is called with can be divided into two parts: options which modify the way getopt will do the parsing (the options and the optstring in the SYNOPSIS), and the parameters which are to be parsed (parameters in the SYNOPSIS). 调用参数getopt可以分为两部分:修改getopt将进行解析的方式的选项(SYNOPSIS中的选项和optstring),以及要解析的参数(SYNOPSIS中的参数)。 The second part will start at the first non-option parameter that is not an option argument, or after the first occurrence of '--'. 第二部分将从第一个非选项参数开始,该参数不是选项参数,或者在第一次出现' - '之后。 If no '-o' or '--options' option is found in the first part, the first parameter of the second part is used as the short options string. 如果在第一部分中未找到“-o”或“--options”选项,则第二部分的第一个参数将用作短选项字符串。
Since the -r
command line option takes no further arguments, some_directory
is "the first non-option parameter that is not an option argument." 由于-r
命令行选项不再使用其他参数,因此some_directory
是“第一个非选项参数的非选项参数”。 Therefore, as clearly spelled out in the getopt(1)
man page, all succeeding command line arguments that follow it (ie, -P 80 ...
) are assumed to be non-options (and non-option arguments). 因此,正如在getopt(1)
手册页中清楚地说明的那样,跟随它的所有后续命令行参数(即-P 80 ...
)都被假定为非选项(和非选项参数)。
So, in effect, this is how getopt(1)
sees the example presented with the end of the options and the beginning of the parameters demarcated by succeeding text bing in gray: 因此,实际上,这就是getopt(1)
看到的示例与选项的结尾以及由灰色的后续文本bing划分的参数的开头:
scp -P80 -r some_directory -P 80 ...
scp -P80 -r some_directory -P 80 ...
This has nothing to do with scp
behavior and everything to do with how POSIX standard applications parse command line options using the getopt(3)
set of C functions. 这与scp
行为无关,也与POSIX标准应用程序如何使用getopt(3)
C函数集解析命令行选项有关。
For more details with regard to command line ordering and processing, please read the getopt(1)
manpage using: 有关命令行排序和处理的更多详细信息,请阅读getopt(1)
联机帮助页:
man 1 getopt
#3楼
I'm using different ports then standard and copy files between files like this: 我正在使用不同的端口然后标准并在文件之间复制文件,如下所示:
scp -P 1234 user@[ip address or host name]:/var/www/mywebsite/dumps/* /var/www/myNewPathOnCurrentLocalMachine
This is only for occasional use, if it repeats itself based on a schedule you should use rsync and cron job to do it. 这仅适用于偶尔使用,如果它根据计划重复,您应该使用rsync和cron job来执行此操作。
#4楼
Copying file to host: scp SourceFile remoteuser@remotehost:/directory/TargetFile
将文件复制到主机: scp SourceFile remoteuser@remotehost:/directory/TargetFile
Copying file from host: scp user@host:/directory/SourceFile TargetFile
从主机复制文件: scp user@host:/directory/SourceFile TargetFile
Copying directory recursively from host: scp -r user@host:/directory/SourceFolder TargetFolder
从主机递归复制目录: scp -r user@host:/directory/SourceFolder TargetFolder
NOTE : If the host is using a port other than port 22, you can specify it with the -P option: scp -P 2222 user@host:/directory/SourceFile TargetFile
注意 :如果主机使用端口22以外的端口,则可以使用-P选项指定它: scp -P 2222 user@host:/directory/SourceFile TargetFile
#5楼
scp help tells us that port is specified by uppercase P. scp help告诉我们端口是由大写字母P指定的。
~$ scp
usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user@]host1:]file1 ... [[user@]host2:]file2
Hope this helps. 希望这可以帮助。
#6楼
One additional hint. 一个额外的提示。 Place the '-P' option after the scp command, no matter whether the machine you are ssh-ing into is the second one (aka destination). 在scp命令之后放置'-P'选项,无论你正在进入的机器是否是第二个(也就是目的地)。 Example: 例:
scp -P 2222 /absolute_path/source-folder/some-file user@example.com:/absolute_path/destination-folder