Signup/Sign In

How To Use SCP Command To Securely Transfer Files

Posted in Programming   JANUARY 19, 2022

    Scp

    Introduction

    SCP stands for Secure Copy Protocol. It is a utility that may transfer files from a localhost to a remote host, from a remote host to a local host, or between two distant hosts. This post will investigate how to utilize SCP to copy between local and foreign hosts.

    SCP is almost entirely run via the command-line using the SCP command. It employs ssh (Secure Shell) to transport data to and from remote sites. It offers a range of options that determine the authentication settings, hosts, and ports like SSH.

    By default, the SCP protocol works on port 22 unless changed by a command-line argument. All SCP instructions follow the form:

    $ SCP [OPTIONS] [SOURCE] [DESTINATION]
    
    

    Let's look at how SCP enables us to move a file on our local computer to a distant one.

    Transferring a Local File to a Remote Destination

    Uploading a file from our local computer to a distant site is common for IT experts. With SCP, we can achieve this with a command like:

    $ scp path/to/local/file.ext user@remote-host:path/to/remote/file.ext

    This command will copy the local file file. ext to the provided path (after the colon) on the remote host.

    • The user-specified in the command is the username. The username must belong to a user of the remote computer.
    • The remote host given is the domain name or the IP address of the remote computer we are attempting to connect to.
    • We next indicate where we want to transfer the file to on the remote system following the colon (:).

    After executing this command, a prompt will show for the password matching to the remote host's user account:

    $ user@remote-password: host's

    Once the password is supplied, the file will be copied.

    This user account must have access to the remote path supplied in the command. If you can't use your credentials to log in remotely using ssh, then those credentials will not work when using SCP.

    Now that we know how to transfer a distant file to our local system, let's tackle the flip case - moving a file from a remote host to the localhost.

    Transferring a Remote File to a Local Destination

    In the very same approach, we may transfer a file from a distant computer to our local machine:

    $ scp user@remote-host:path/to/remote/file.ext path/to/local/file.ext

    This operates the same way, except that the remote user, host, and path are supplied before the local course.

    A prompt will still be shown for the remote host's user account password. But, when complete, you should have a new file in the folder you selected.

    Let's take a look at transferring a file between two distant sites.

    Transferring a Remote File to a Remote Destination

    Finally, the following command format is used to transfer a file between two remote hosts:

    $ scp user@remote-host:path/to/remote/file.ext path/to/local/file.ext

    Notice that in this scenario, two remote users must be given. Each needs to have access to their distant server. A password prompt will be given to accept login credentials for each user.

    From all these possibilities of file copying, we see that the SCP command is versatile on the source or destination path. This adaptability makes it incredibly handy for scripts.

    We may transmit files between our local and distant computers to test and upgrade the server or application settings. We may transfer files between our primary remote host and a backup server using SCP. The simplicity and versatility of SCP have made it popular among system administrators.

    The SCP command also includes options for additional freedom in what and how we copy. Let's explore how we may utilize command-line arguments to customize its behaviour.

    Common SCP Command-Line Options

    The SCP command includes various handy arguments (sometimes known as flags) that may vary how it connects to a remote host.

    Changing the Port

    As discussed earlier, SCP works on port 22 by default. However, this may be overcome by passing the -P option, followed by the port number.

    This is how we transfer a file to a distant location connecting to port 44 instead of 22:

    $ scp -P 44 path/to/local/file.ext user@remote-host:path/to/remote/file.ext \sModification/Access Time

    The distinction between the -P and -p flags is worth noticing. The -p option keeps the file modification timings, access times, and modes when transferring. This may be beneficial if preserving the file characteristics unaltered is desired:

    $ cp -p path/to/local/file.ext user@remote-host:path/to/remote/file.ext

    Copying Directories

    The -r parameter may be used to recursively copy a folder and its contents instead of a single file:

    $ scp -r path/to/local/folder user@remote-host:path/to/remote/folder

    This is cleaner than copying files from the folder using the * or? Wildcards.

    Authentication Keypair File

    The -i switch may authenticate the connection using a cryptographic key pair saved in a file instead of a username and password. This is typical practice for establishing distant cloud servers, such as AWS or Digital Ocean.

    You may supply a keypair file like this:

    $ scp -i path/to/local/keypair.pem path/to/local/file.ext user@remote-host:path/to/remote/file.ext

    Using Multiple SCP Options

    Flags may be used with each other as well. Here is an example that implements many options to transfer a folder from a distant host to our local computer using a keypair file for authentication on port 44 while retaining file attributes and suppressing output:

    $ cp -p -q -P 44 -i path/to/local/keypair.pem -r path/to/local/folder user@remote-host:path/to/remote/folder

    With this foundation, you're able to employ SCP for a multitude of circumstances!

    Conclusion

    This post explored SCP, a protocol that may quickly move files across hosts. We covered moving files from the local host to a distant host, from a remote host to the local host, and between two remote hosts.

    We also touched on a few essential command-line parameters which may be utilized in particular instances.

    About the author:
    Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.
    Tags:file-transferhowtolinuxscp
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS