Flylib.com

Books Software

 
 
 

Action 2.1.3: Use the scp command as a replacement for rcp to exchange files with a remote system


Action 2.1.3: Use the scp command as a replacement for "rcp" to exchange files with a remote system

The scp (secure copy) command is used to copy files and directories to and from remote servers. This command can be used as the replacement for the unencrypted rcp program. The scp command can also be used as a substitute for sftp , although there are some key differences.

One difference is that scp does not have an "interactive" mode as sftp does and therefore doesn't have as many capabilities, although it is still a quite powerful utility. One advantage of scp over sftp is that directory and recursive copies are much easier. Additionally, the scp command can be used in conjunction with an .shosts file to bypass interactive authentication “ this is strongly discouraged since it provides yet another avenue for attackers . See Action 2.1.4 for more detail regarding host-based authentication with .shosts files.

So, if you have a need to copy directory entries in addition to files and/or you need to copy a directory structure recursively, scp is probably what you should use. If you're copying files only and they exist in multiple directories, or if you prefer interactivity, sftp is probably the better choice.

Although there are many options, the basic format of the scp command is as follows :

scp [options] [[ user @]host1:] path -to-source [ ] [[ user2@]host2: ] path-to-destination

Where:

  • options = one of the valid options as referenced in the scp man page

  • user = the account you are attempting to access on the remote system “ if not specified, the current user name will be assumed.

  • hostX = the remote system(s) to which you are attempting to copy to/from

  • path-of-source = the source location of files/directories to copy

  • path-of-destination = the destination location of files/directories to copy

{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}

The following example uses scp to duplicate the FTP file transfer from the last example (host is server.example.com , account is sshuser , file is /tmp/snoop.txt ):

$ scp sshuser@server.example.com:/tmp/snoop.txt .
        sshuser@server.example.com's password:

********

snoop.txt             100% *****************************  7956         00:00
        $ ls -l
        total 24
        drwxr-xr-x    2 root      root            512  May 20 1999 103346-22
        drwxr-xr-x    3 sshuser    150            512  Nov 10 09:39 ns_imap
        drwx------    2 sshuser    150            512  Nov 10 09:39 nsmail
        -rw-r--r--    1 sshuser    150           7956  Dec 18 14:30 snoop.txt
        drwxrwxr-x    3 root     other            512  Jul 10 1999 upgrade
        $

Besides the obvious advantage of encrypting communication, note that another advantage of scp over rcp is the verification of completion and the reporting of the number of bytes transferred.

You can also omit the user name as part of the command; if you do so, then the current user name will be assumed, as shown below:

$ scp server.example.com:/tmp/snoop.txt .
        sshuser@server.example.com's password:

*******

snoop.txt        100% ***************************** 7956    00:00
        $

In this example, we use the command line options -r to recursively copy a directory structure and -p to keep the original permissions of all files and directories:

$ scp -pr sshuser@server.example.com:/tmp/patches /tmp
        sshuser@server.example.com's password:

********

105395-09.tar.Z 100% ***************************** 537 KB 00:00
        107684-09.zip   100% ***************************** 851 KB 00:00
        110615-09.zip   100% ***************************** 868 KB 00:01
        dsmerror.log    100% ***************************** 150	00:00

The source directory is /tmp/patches , which is a directory with three subdirectories. The destination directory on the local machine is /tmp , so this command completely duplicates the /tmp/patches directory from host server.example.com to the local host, assuming your account has read permissions for all the files and directories in this structure. A quick way to verify that everything was copied is to use the du -sk command on both systems and compare the byte count, as shown below:

$ du -sk /tmp/patches
        2284    /tmp/patches

Both systems should now report the same or roughly the same byte total. A more thorough method for verifying byte totals is to take checksums of files that were copied via scp. See the man pages for cksum, sum or other checksum utilities, depending on the flavor of UNIX you are using. Here's an example of using the cksum command:

$ find . -type f  xargs cksum
        2795861523   550601   ./2.6/105395-09.tar.Z
        405161581    872202   ./7/107684-09.zip
        2165853569   889286   ./8/110615-09.zip
        3510532143   150      ./8/dsmerror.log

Note that since we used the "-p" command line option to perform the copy, all permissions of the /tmp/patches and child sub-directories and files should be as they exist on the source system.

It should be noted that both sftp and scp can be utilized for "puts" as well as "gets" ” all of the previous examples have demonstrated "gets". A "put" would be initiated from the machine where the data to be copied exists. The following example does the previous transfer in reverse, using "put" rather than "get":

$ scp -pr /tmp/patches sshuser@server.example.com:/tmp
        sshuser@server.example.com's password:

********

105395-09.tar.Z       100% *****************************    537 KB     00:00
        107684-09.zip         100% *****************************    851 KB     00:00
        110615-09.zip         100% *****************************    868 KB     00:00
        dsmerror.log          100% *****************************    150        00:00
        $ uname -a
        SunOS client.example.com 5.8 Generic_108528-16 sun4m sparc SUNW,SPARCstation-20

scp has many other command line options - to see them, issues scp with on options:

$ scp
        usage: scp [-pqrvBC46] [-F config] [-S program] [-P port]
                   [-c cipher] [-i identity] [-o option]
               [[user@]host1:]file1 [...] [[user@]host2:]file2

For more detail of each option, refer to the scp man page.