scp with port number specified

前端 未结 12 618
有刺的猬
有刺的猬 2021-01-29 17:03

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.t         


        
相关标签:
12条回答
  • 2021-01-29 17:31

    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.

    0 讨论(0)
  • 2021-01-29 17:33

    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.

    0 讨论(0)
  • 2021-01-29 17:37

    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/

    0 讨论(0)
  • 2021-01-29 17:39

    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
    
    0 讨论(0)
  • 2021-01-29 17:40

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

    scp -i ~/keys/yourkey -P2222 file ubuntu@host:/directory/
    
    0 讨论(0)
  • 2021-01-29 17:43

    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
    
    0 讨论(0)
提交回复
热议问题