By One Two Three

2012-04-26 20:35:05 8 Comments

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


@HarteScout 1 2020-04-03 03:06:06

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.

@Kiryamwibo Yenusu 2020-01-21 12:17:45

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 [email protected]_address:/home/file/pathDirectory .

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

@Michael Goldshteyn 2012-04-26 20:37:03

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

@Abdull 2014-02-08 15:06:02

btw, scp demands correct options order: scp -r some_directory -P 80 ... does not work ----- but scp -P 80 -r some_directory ... works.

@Gary 2014-05-21 23:14:16

generally in linux the command is followed by the options and then the instructions/values

@Benjamin 2015-04-06 22:13:26

@Abdull note that scp -r -p 50193 /path/to/directory works as well. The file path parameter is not tied to the -r recursion option.

@None 2015-09-06 17:05:01

@Benjamin Unless you meant to use port 50193 in which case your 'p' should be capitalized.

@mwfearnley 2016-05-27 11:46:49

@Abdull: there's an answer at that explains the -P parameter ordering. Basically, the port has to go before the host. This both prevents ambiguity and allows for two -P parameters if both hosts are remote.

@john Smith 2019-03-19 22:36:33

i come back to this post pretty often, thank you :)

@toto_tico 2019-10-19 09:34:37

so, ssh is -p , and scp is -P; great, i will certainly never forget

@Roland 2020-02-19 13:29:01

And not to mention, first must come e.g. -P 2222 for vagrant bxoes, so first the P parameter, then local/remote, then remote/local. Did you know you can also copy remote-remote with it? For vagrant I recommend to use ssh-keygen.exe -b 4096 and copy that ~/.ssh/ to ~/.ssh/known_keys on remote site and leave the existing key always in!

@Hasan Barary 2018-10-19 14:04:33

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

scp -P port-number source-file/directory [email protected]:/destination

ya ali

@Turan Zamanlı 2018-09-12 12:18:43

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

scp -P 3838 /the/source/file [email protected]:/destination/file

@Vaseem007 2017-11-14 09:53:51

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

scp -i ~/keys/yourkey -P2222 file [email protected]:/directory/

@Mike 2016-04-27 00:14:37

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:

    Port 80


Host myserver myserver80 short yes_anything well-within-reason
    Port 80
    User username

Then you can use:

scp [email protected]:/root/file.txt .


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

@user88595 2019-01-04 13:10:22

Only solution which allows the use of scp -3 from and to servers with ssh listeners on different ports. scp -3 -P 123 server1:/file -P 456 server2:/file or similar alternatives wouldn't work, scp assumed the same port across both servers

@Sadat 2020-08-26 02:10:20

Best solution ever, where the source file is from Remote machine

@Gooshan 2015-10-21 09:21:49

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

@D D 2015-07-31 16:59:05

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]
           [[[email protected]]host1:]file1 ... [[[email protected]]host2:]file2

Hope this helps.

@AJ. 2015-07-25 21:42:22

Copying file to host: scp SourceFile [email protected]:/directory/TargetFile

Copying file from host: scp [email protected]:/directory/SourceFile TargetFile

Copying directory recursively from host: scp -r [email protected]:/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 [email protected]:/directory/SourceFile TargetFile

@blagus 2016-02-19 02:10:02

Just the NOTE would be enought

@Marian Zburlea 2013-08-19 09:57:11

I'm using different ports then standard and copy files between files like this:

scp -P 1234 [email protected][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.

Related Questions

Sponsored Content

31 Answered Questions

[SOLVED] How can you find out which process is listening on a port on Windows?

6 Answered Questions

[SOLVED] How do I escape spaces in path for scp copy in Linux?

7 Answered Questions

[SOLVED] How to scp with a second remote host

  • 2012-02-04 08:06:38
  • Danosaure
  • 87356 View
  • 81 Score
  • 7 Answer
  • Tags:   scp

7 Answered Questions

[SOLVED] How does `scp` differ from `rsync`?

  • 2013-11-27 14:05:59
  • Leahcim
  • 160026 View
  • 252 Score
  • 7 Answer
  • Tags:   rsync scp

15 Answered Questions

[SOLVED] Using scp to copy a file to Amazon EC2 instance?

14 Answered Questions

[SOLVED] How to scp in Python?

  • 2008-10-30 14:09:21
  • Michael Gundlach
  • 264238 View
  • 161 Score
  • 14 Answer
  • Tags:   python ssh paramiko scp

35 Answered Questions

[SOLVED] What is the difference between a port and a socket?

Sponsored Content