Remote Copying With the scp Command
The scp command copies files or directories between a local and a remote systemor between two remote systems. You can use this command from a remotesystem (after logging in with the ssh command) or from the local system.The scp command uses ssh for data transfer. Thus, the scp command uses thesame authentication and provides the same security as the ssh command.
With scp, you can perform the following remote copy operations:
Copy a file or directory from your local system to a remote system
Copy a file or directory from a remote system to your local system
Copy a file or directory between remote systems from your local system
Security Considerations for Copy Operations
To copy files or directories between systems, you must have permission to login and copy files.
The scp command, as a component of the ssh command, requires that youhave either a user account or host key access to the target system.Consult Chapter 15, Using Secure Shell, in Oracle Solaris 11.1 Administration: Security Services for further information.
Caution - Both the cp and scp commands can overwrite files without warning. Ensure thatfile names are correct before executing the command. |
Specifying the Source and Target for Copy Operations
With the scp command, you can specify the source (the file or directoryto be copied) and the target (the location in which to copy the file ordirectory). You can shorten the path strings by using the tilde character (~)and the shell wildcard characters (*, ?, and so forth).
The tilde character (~) is expanded by all shell programs to be thecurrent user's home directory. The current user is the user under which theshell is executing. If the home directory for the user jack is /export/home/jack, thenfor the user jack, ~/myfile.txt expands to /export/home/jack/myfile.txt.
This expansion also works for remote paths. If the user jack wantsto copy a file from his home directory, then these three path descriptionsare equivalent:
mars:/export/home/jack/myfile.txt
mars:~/myfile.txt
mars:myfile.txt
This expansion is also useful when referring to another user's remote home directory.In this case, you would include the user's name after the tilde character.For the user jack, mars:~jill/myfile.txt is equivalent to mars:/export/home/jill/myfile.txt, but it isshorter to type.
How to Copy a File Between Two Systems (scp)
- Ensure that you have permission to copy files on the target system.
The scp command requires authentication. Depending upon the method of authentication used, youmust have either an account on the target system, or an authorizedpublic key on the target system. You should at least have read permission onthe source system and write permission on the target system.
See AlsoHow to move a file in LinuxCaution - If you do not have an account on the target system, or ifthe target system is not configured to allow public keys, you will receive anauthentication error. For example:
$ scp mars:/var/tmp/testdir/letter.txt .Permission denied (gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive)
Ensure that you have either a user account or public key access configuredon the target system for authentication. See Secure Shell Authentication in Oracle Solaris 11.1 Administration: Security Services.
- Determine the location of the source and target.
If you don't know the path of the source or target, you canfirst log in to the remote system with the ssh command, as described inLogging In to a Remote System (ssh). Then, navigate through the remote system until you find the location. Youcan then perform the next step without logging out of the remote system.
- Copy the file or directory.
$ scp [-r] [[user1@]hostname1:]file1 ... [[user2@]hostname2:]file2
- -r
Use to recursively copy entire directories.
- user1, user2
Login account to use on the remote host.
- hostname1, hostname2
The names of the remote host from or to which the file is to be copied.
- file1
The file name or directory name to be copied. Several source file names may be included on one command line.
- file2
The destination file name or directory name.
Example3-5 Using the scp Command to Copy a Remote File to a Local System
In this example, scp is used to copy the file letter.doc from the/home/jones directory of the remote system pluto to the working directory on thelocal system.
$ scp pluto:/home/jones/letter.doc .The authenticity of host 'pluto (192.168.56.102)' can't be established.RSA key fingerprint is b4:88:7b:cf:f5:23:d3:ad:0b:14:22:31:74:7b:6c:74.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.56.102' (RSA) to the list of known hosts.Password:letter.txt 100% |*****************************| 23 00:00 $
In this example, this is the first time the system pluto isaccessed, hence, the RSA key fingerprint message.
Here, the “.” symbol at the end of the command line refers to thecurrent working directory on the local system.
Example3-6 Using the scp Command to Copy a Local File to a Remote System
In this example, scp is used to copy the file notice.doc from thehome directory (/home/smith) of the local system earth to the /home/jones directoryof the remote system, pluto.
$ scp notice.doc pluto:/home/jonesPassword:notice.doc 100% |*****************************| 0 00:00
Because no remote file name is provided, the file notice.doc is copiedinto the /home/jones directory with the same name.
In the following example, the scp operation from the previous example is repeated,but scp is executed from a different working directory on the local system(/tmp). Note the use of the “~” symbol to refer to the current user'shome directory:
$ scp ~/notice.doc pluto:/home/jonesPassword:notice.doc 100% |*****************************| 0 00:00
Copyright © 2002, 2012, Oracle and/or its affiliates. All rights reserved. Legal Notices |