问题
I'm trying to scp a file from a remote server to my local machine. Only port 80 is accessible.
I tried:
scp -p 80 username@www.myserver.com:/root/file.txt .
but got this error: cp: 80: No such file or directory
How do I specify the port number in a scp command?
回答1:
Unlike ssh, scp uses the uppercase P switch to set the port instead of the lowercase 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.
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:
-P port Specifies the port to connect to on the remote host. 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 Preserves modification times, access times, and modes from the original file.
Update and aside to address one of the (heavily upvoted) comments:
With regard to Abdull's comment about scp
option order, what he suggests:
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:
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). 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.
Since the -r
command line option takes no further arguments, some_directory
is "the first non-option parameter that is not an option argument." Therefore, as clearly spelled out in the getopt(1)
man page, all succeeding command line arguments that follow it (i.e., -P 80 ...
) are assumed to be non-options (and non-option arguments).
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:
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.
For more details with regard to command line ordering and processing, please read the getopt(1)
manpage using:
man 1 getopt
回答2:
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). Example:
scp -P 2222 /absolute_path/source-folder/some-file user@example.com:/absolute_path/destination-folder
回答3:
You know what's cooler than -P
? nothing
If you use this server more than a few times, setup/create a ~/.ssh/config
file with an entry like:
Host www.myserver.com
Port 80
or
Host myserver myserver80 short any.name.u.want yes_anything well-within-reason
HostName www.myserver.com
Port 80
User username
Then you can use:
scp username@www.myserver.com:/root/file.txt .
or
scp short:/root/file.txt .
You can use anything on the "Host" line with ssh, scp, rsync, git & more
There are MANY configuration option that you can use in config files, see:
man ssh_config
回答4:
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.
回答5:
scp help tells us that port is specified by uppercase 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:
for use another port on scp command use capital P like this
scp -P port-number source-file/directory user@domain:/destination
ya ali
回答7:
Copying file to host:
scp SourceFile remoteuser@remotehost:/directory/TargetFile
Copying file from host:
scp user@host:/directory/SourceFile TargetFile
Copying directory recursively from host:
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
回答8:
This can be achived by specifying port via the -P
switch:
scp -i ~/keys/yourkey -P2222 file ubuntu@host:/directory/
回答9:
if you need copy local file to server (specify port )
scp -P 3838 /the/source/file username@server.com:/destination/file
来源:https://stackoverflow.com/questions/10341032/scp-with-port-number-specified