Use SCP to securely transfer files between two Unix computers

In Unix, you can use SCP (the scp command) to securely copy files and directories between remote hosts without starting an FTP session or logging into the remote systems explicitly. The scp command uses SSH to transfer data, so it requires a password or passphrase for authentication. Unlike rcp or FTP, scp encrypts both the file and any passwords exchanged so that anyone snooping on the network cannot view them.

Note:
As of October 14, 2018, SCP and SFTP file transfers to Big Red II, Big Red II+, Carbonate, and Karst require two-factor authentication using Duo. SSH public-key authentication remains an option for researchers who submit the "SSH public-key authentication to HPS systems" user agreement (log into HPC everywhere beta using your IU username and passphrase), which requires you to agree to set a passphrase on your private key when generating your key pair. If you have questions about how two-factor authentication with Duo may impact your workflows, contact the UITS Scientific Applications and Performance Tuning (SciAPT) team. If you have system-specific questions, contact the UITS High Performance Systems (HPS) team. For help with Duo, see Get started with Two-Step Login (Duo) at IU and Help for Two-Step Login (Duo).

Syntax

The syntax for the scp command is:

scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2

The location of the source file is specified by username1@source_host:directory1/filename1, which includes the:

  • Name of the account on the host computer (username1)
  • Hostname of the computer on which the source file resides (source_host)
  • Name of the directory containing the source file (directory1)
  • Filename of the source file (filename1)

The location to which the source file will be copied is specified by username2@destination_host:directory2/filename2, which includes the:

  • Name of the account on the destination computer (username2)
  • Hostname of the computer to which the source file will be copied (destination_host)
  • Name of the directory to which the source file will be copied (directory2)
  • Filename of the copy (filename2)
Note:
Make sure to include a space between the source and destination paths. Also, be careful when copying files that share the same name on both hosts; you may accidentally overwrite data you intended to keep.

For more about scp, consult its manual page. At the Unix prompt, enter:

man scp

At Indiana University, for personal or departmental Linux or Unix systems support, see At IU, how do I get support for Linux or Unix?

Examples

For the following examples, assume your username is dvader, and you are logged into your account on the computer empire.gov:

  • To copy a file called rebels.txt from your home directory on empire.gov to a directory called revenge in your account on the computer deathstar.com, enter:
    scp ~/rebels.txt dvader@deathstar.com:~/revenge
    

    You'll be prompted for your password on the destination system (deathstar.com). The command won't work unless you enter the correct password.

  • To copy a directory (and all the files it contains), use scp with the -r option. This tells scp to recursively copy the source directory and its contents.

    To copy the entire revenge directory from your deathstar.com account to your empire.gov account, enter:

    scp -r dvader@deathstar.com:~/revenge ~/revenge
    

    You'll be prompted for your password on the source system (deathstar.com). The command won't work unless you enter the correct password.

  • To copy multiple files within a directory, you can use wildcards (for example, * or ?). However, to use wildcards for copying multiple source files from a remote system, you need to place quotes (" ") around the path to the source files. This is necessary because the Unix shell, not the scp command, expands unquoted wildcards.

    Therefore, to copy all the .txt files from the revenge directory on your deathstar.com account to your revenge directory on empire.gov, enter:

    scp dvader@deathstar.com:"revenge/*.txt" ~/revenge/
    

    You'll be prompted for your password on the source system (deathstar.com). The command won't work unless you enter the correct password.

For the following example, assume you (dvader) are logged into another computer (that is, some other computer that's not empire.gov or deathstar.com). To copy luke.txt from your home directory on empire.gov to your revenge directory on deathstar.com, enter:

scp dvader@empire.gov:~/luke.txt dvader@deathstar.com:~/revenge

You'll be prompted to enter two passwords: one for the source system (empire.gov) and one for the destination system (deathstar.com). The command won't work unless you correctly enter both passwords.

This is document agye in the Knowledge Base.
Last modified on 2018-12-03 12:15:41.

Contact us

For help or to comment, email the UITS Support Center.