In Unix, how do I use SCP to securely transfer files between two 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.
Syntax
The syntax for the scp command is:
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 accidently overwrite data you intended to keep.
For more about scp, consult its manual
page. At the Unix prompt, enter:
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.txtfrom your home directory onempire.govto a directory calledrevengein your account on the computerdeathstar.com, enter: scp ~/rebels.txt dvader@deathstar.com:~/revengeYou'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
scpwith the-roption. This tellsscpto recursively copy the source directory and its contents.To copy the entire
scp -r dvader@deathstar.com:~/revenge ~/revengerevengedirectory from yourdeathstar.comaccount to yourempire.govaccount, enter: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
(e.g.,
*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 thescpcommand, expands unquoted wildcards.Therefore, to copy all the
scp dvader@deathstar.com:"revenge/*.txt" ~/revenge/.txtfiles from therevengedirectory on yourdeathstar.comaccount to yourrevengedirectory onempire.gov, enter: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 (i.e., 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:
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 document was developed with support from National Science Foundation (NSF) grant OCI-1053575. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the NSF.
Last modified on December 12, 2012.







