ChatGPT解决这个技术问题 Extra ChatGPT

scp with port number specified

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 should be in capital -P 80

M
Michael Goldshteyn

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

btw, scp demands correct options order: scp -r some_directory -P 80 ... does not work ----- but scp -P 80 -r some_directory ... works.
generally in linux the command is followed by the options and then the instructions/values
@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.
@Benjamin Unless you meant to use port 50193 in which case your 'p' should be capitalized.
@Abdull: there's an answer at askubuntu.com/a/307078/37574 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.
G
Gooshan

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

M
Mike

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


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
Best solution ever, where the source file is from Remote machine
N
Nicolás Ozimica

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.


H
Hasan Barary

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

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

ya ali


How to give the port of the destination instead of the sender?
h
hg8

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

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

D
D D

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.


M
MewX

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://user@acme.com:22222/path/to/filename
scp scp://userA@foo.com:22222/path/to/filename scp://userB@bar.com: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://user@acme.com:22222//root/path/to/filename

T
Turan Zamanlı

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

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

C
Christopher Gray

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

scp -P 10022 -r /media/data/somedata/* user@192.168.1.x:/var/services/homes/user/directory/


A
AJ.

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


Just the NOTE would be enought
K
Kiryamwibo Yenusu

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

Go to a directory where you have admin rights preferably your home directory on the machine where you want to copy files to 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

H
HarteScout 1

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 user@193.168.X.X:/home/user/Downloads It worked out well.


h
huezohuezo1990
scp -P 22 -r DIR  huezo@192.168.1.100:/home/huezo

scp -P PORT -r DIR  USER@IP:/DIR

Your answer could be improved by adding more information on what the code does and how it helps the OP.