Using scp Tutorial


Background



scp stands for secure copy. It is the modern replacement for the rcp command, which stands for remote copy. rcp works over insecure telnet connections, and can still be used in situations where you have a telnet server running. Running a telnet server is, however, considered bad practice for security considerations. scp, on the other hand, transfers data via ssh. Therefore, in order to use scp, an ssh server must be running on the remote machine and be accessible from the local machine. One must also possess a user account and password on the remote machine, as these will be required for the transfer.


Basic Capabilities



scp can be used to transfer files in the following ways:

  • from a remote server to the local machine
  • from the local machine to a remote server
  • from the local machine to the local machine
scp can not be used to transfer files:

  • from a remote machine to a remote machine
In order to transfer files from a remote machine to another remote machine (or the same remote machine but into a different place), one would have to first copy the file to the local machine, then copy it to the destination remote machine.


Usage



Using scp is simple. For clarity, this first example shows copying a file on the local machine to another place on the local machine. Note, however, that scp is not really the best tool for this job. This is just the most clear way to first demonstrate how to use scp.

To copy the file myfile from the current directory to /tmp/myfile using scp, one would issue this command:

scp myfile /tmp

-- or --

scp myfile /tmp/

-- or --

scp myfile /tmp/myfile

Any of the above commands will accomplish the stated goal.

As you can see from the above example, the first parameter to scp is the source file (aka, the file to be copied) and the second parameter is the destination file (aka, where to put a copy of the file). The next example shows to do this across a network.

To transfer a file named index.html from the local machine to the home directory of the user solid_liq on a remote machine named webserver.myhouse.local, one would issue the following command:

scp index.html solid_liq@webserver.myhouse.local:~/

One must also enter the password when requested. The password requested is for the remote account on the remote host. This will always be the case when using scp to transfer a file to or from a remote machine, unless ssh preshared keys are setup for the account (described in another article).

To copy this same file to /home/solid_liq/webstuff onto www.myhouse.local, one would execute this command (Note how the ~ is properly expanded to the user's home directory):

scp index.html solid_liq@www.myhouse.local:~/webstuff

To copy this file into the home directory of an account at your webhost, webhost.com, for your account, www, one would issue the following command:

scp index.html www@webhost.com:~/

As can be seen from the above examples, scp parses the arguments to the command line in the following fashion:

login name = any text preceding an @ sign
hostname = any text coming between the @ sign and a : (colon) character
remote path = the text following the colon (:) until the end of the argument (unescaped whitespace)


The login name, of course, is the login name of the account on the remote machine. The remote path is where to either put the file, or from where to retrieve the file.

This syntax is identical no matter whether the remote host is the destination or the source. Thus, to copy the /home/solid_liq/index.html file from the remote machine www.solid_liq.local to the local machine, one would simply type:

scp solid_liq@www.solid_liq.local:~/index.html .

To copy this file from webhost.com from the account www, one would type:

scp www@webhost.com:~/index.html .

Note that scp expands tildes just like the shell does. Therefore, ~/ is translated to mean the home directory of the user in question, whether it be the local user (for an argument specifying a path on the local machine) or the remote user (for a remote path). The dot (.) represents the current directory as normal, double dots (..) the parent directory, etc. Globbing, however, will not necessarily behave the same way.


More Advanced Usage



If the remote ssh server is running on a non-standard port, this can be specified to the scp command using the -P (that's a capital P) switch:

scp -P 2200 myfile me@remote.com:~/

If you need to preserver the attributes of the file (modification times, modes and access times), specify the -p (that's a lowercase p) switch:

scp -p myfile me@remote.com:~/

To see a progress meter (great for large transfers), use the -q switch:

scp -q myfile me@remote.com:~/

To recursively copy entire directories (as cp -R does), use the -r switch:

scp -r mydir/ me@remote.com:~/

To diagnose connection issues, use the -v switch:

scp -v myfile me@remote.com:~/

To limit the bandwidth used for the transfer (such as to not slow down users of your web server), use the -l (lowercase L) switch to specify the max rate in Kbit/s:

scp -l 500 myfile me@remote.com:~/




Tux