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?
Unlike ssh, scp uses the uppercase P switch to set the port instead of the lowercase p:
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:
Bonus Tip: How can I determine the port being used by the/an SSH daemon to accept SSH connections?
This question can be answered by using the
netstatutility, as follows:Or, using the far more readable word based netstat option names:
The output you will see, assuming your ssh daemon is configured with default values its listening ports, is shown below (with a little trimming of the whitespace in between columns, in order to get the entire table to be visible without having to scroll):
Important Note
For the above examples,
sudowas used to run netstat with administrator privs, in order to be able to see all of the Program Names. If you run netstat as a regular user (i.e., without sudo and assuming you don’t have admin rights granted to you, via some other method), you will only see program names shown for sockets that have your UID as the owner. The Program Names for sockets belonging to other users will not be shown (i.e., will be hidden and a placeholder hyphen will be displayed, instead):Update and aside to address one of the (heavily upvoted) comments:
With regard to Abdull’s comment about
scpoption order, what he suggests:…, intersperses options and parameters, since the
-rswitch takes no additional arguments andsome_directoryis treated as the first parameter to the command, making-Pand all subsequent command line arguments look like additional parameters to the command (i.e., hyphen prefixed arguments are no longer considered as switches).getopt(1)clearly defines that parameters must come after options (i.e., switches) and not be interspersed with them, willy-nilly:Since the
-rcommand line option takes no further arguments,some_directoryis "the first non-option parameter that is not an option argument." Therefore, as clearly spelled out in thegetopt(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 gray text:scp -r
some_directory -P 80 ...This has nothing to do with
scpbehavior and everything to do with how POSIX standard applications parse command line options using thegetopt(3)set of C functions.For more details with regard to command line ordering and processing, please read the
getopt(1)manpage using: