scp with port number specified

PortScp

Port Problem Overview


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?

Port Solutions


Solution 1 - Port

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.

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 netstat utility, as follows:

sudo netstat -tnlp | grep sshd

Or, using the far more readable word based netstat option names:

sudo netstat --tcp --numeric-ports --listening --program | grep sshd

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):

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address  Foreign Address  State       ID/Program name
tcp      0      0   0.0.0.0:22     0.0.0.0:*        LISTEN      888/sshd: /usr/sbin 
tcp6     0      0   :::22          :::*             LISTEN      888/sshd: /usr/sbin 

Important Note

For the above examples, sudo was 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):

Proto Recv-Q Send-Q Local Address   Foreign Address  State       ID/Program name
tcp        0      0 127.0.0.1:46371 0.0.0.0:*        LISTEN      4489/code
...
tcp        0      0 0.0.0.0:111     0.0.0.0:*        LISTEN      -                   
tcp        0      0 127.0.0.53:53   0.0.0.0:*        LISTEN      -                   
tcp        0      0 0.0.0.0:22      0.0.0.0:*        LISTEN      -                   
...

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 -r some_directory -P 80 ...

..., intersperses options and parameters, since the -r switch takes no additional arguments and some_directory is treated as the first parameter to the command, making -P and 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:

> 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 gray text:

scp -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

Solution 2 - Port

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

Solution 3 - Port

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 [email protected]:/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

Solution 4 - Port

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.

Solution 5 - Port

for use another port on scp command use capital P like this

scp -P port-number source-file/directory user@domain:/destination

ya ali

Solution 6 - Port

This can be achived by specifying port via the -P switch:

scp -i ~/keys/yourkey -P2222 file ubuntu@host:/directory/

Solution 7 - Port

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.

Solution 8 - Port

if you need copy local file to server (specify port )

scp -P 3838 /the/source/file username@server.com:/destination/file

Solution 9 - Port

To backup all files in all directories to a remote Synology NAS using a different remote port:

scp -P 10022 -r /media/data/somedata/* [email protected]:/var/services/homes/user/directory/

Solution 10 - Port

Port can be specified using the scp protocol path: scp://[user@]host[:port][/path]

From man scp: > The source and target may be specified as a local pathname, a remote host with optional path in the form [user@]host:[path], or a URI in the form scp://[user@]host[:port][/path]. Local file names can be made explicit using absolute or relative pathnames to avoid scp treating file names containing `:' as host specifiers.

Examples:

scp local/filename scp://[email protected]:22222/path/to/filename
scp scp://[email protected]:22222/path/to/filename scp://[email protected]:33333/path/to/filename

Note, if your path is from /root, you will need to have two /s in your path. For example,

scp local/filename scp://[email protected]:22222//root/path/to/filename

Solution 11 - Port

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

Solution 12 - Port

Hope this will help someone looking for a perfect answer

Copying a folder or file from a server with a port defined to another server or local machine

  1. Go to a directory where you have admin rights preferably your home directory on the machine where you want to copy files to
  2. Write the command below

> scp -r -P port user@IP_address:/home/file/pathDirectory .

**Note:** The last . on the command directs it to copy everything in that folder to your directory of preference

Solution 13 - Port

There are many answers, but you should just be able to keep it simple. Make sure you know what port SSH is listening on, and define it. Here is what I just used to replicate your problem.

scp -P 12222 file.7z [email protected]:/home/user/Downloads It worked out well.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOne Two ThreeView Question on Stackoverflow
Solution 1 - PortMichael GoldshteynView Answer on Stackoverflow
Solution 2 - PortGooshanView Answer on Stackoverflow
Solution 3 - PortMikeView Answer on Stackoverflow
Solution 4 - PortMarian ZburleaView Answer on Stackoverflow
Solution 5 - PortHasan BararyView Answer on Stackoverflow
Solution 6 - PortVaseem007View Answer on Stackoverflow
Solution 7 - PortD DView Answer on Stackoverflow
Solution 8 - PortTuran ZamanlıView Answer on Stackoverflow
Solution 9 - PortChristopher GrayView Answer on Stackoverflow
Solution 10 - PortRafael XavierView Answer on Stackoverflow
Solution 11 - PortAJ.View Answer on Stackoverflow
Solution 12 - PortKiryamwibo YenusuView Answer on Stackoverflow
Solution 13 - PortHarteScout 1View Answer on Stackoverflow