Skip to main content

Need Help Using Spok Knowledge? 

Spok

scp

Secure copy or scp is a means of securely transferring files between a local host and a remote host or between two remote hosts.  It uses the ssh protocol.  If the servers are configured with ssh keys not to require a password from "user a" to "user b", then a password isn't needed.  Otherwise, the scp command will ask for the password for each user you connect as.  This article does not cover setting up remote authentication with ssh keygens.

How to scp a file from one server to another

Copying from server to server works the same way as copying a file from one location to another.

For example (standard copy format):

cp <some file> <some other location or file name>

Such as:
    cp /home/amcom/debug/debug.txt /home/amcom/debug/debug.20170621_01.txt
or
    cp /home/amcom/debug/debug.txt /tmp
or
    cp /home/amcom/debug/debug.txt /tmp/debug.20170621_01.txt

and so on.

So with that in mind, with scp, you can copy a file from its original location to its new location:

From:
    where the file is / files are (the server you're on)
To:
    where you want it / them to go (the server you're not on)

or flipped:

From:
    where the file is / files are (the server you're not on)
To:
    where you want it / them to go (the server you're on)

or even from a server you're not on to another server you're not on.

scp options

by passing an invalid parameter (such as -?) or the --help flag to the scp command, you can see a basic usage summary:

usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
           [-l limit] [-o ssh_option] [-P port] [-S program]
           [[user@]host1:]file1 [...] [[user@]host2:]file2

or "man scp".  From there we can see some useful options, such as -r to recursively copy entire directories, or -p to preserve modification times, access time, and modes from the original file.

Like ssh, you don't *need* to specify the username if you're going from the logged-in user to the same logged-in user on the next machine, but it's a good habit to get in to (you really should), to know for a fact what you're copying and where.  Then if there are failures and you need to send the output to someone, it's less confusing for them to follow.


Copy file to remote host:

scp /path/to/SourceFile [user@]host:/path/to/TargetFile

Examples:

- from db1, as the user "amcom" (as the amcom user), copy the file "health_check.crn" from amcom1 to amcom2, leaving the original name.  The folder locations are relative to the user's home directory.

	[amcom@amcom1 ~]$ cd /home/amcom/bin

	scp -p ./health_check.crn amcom2:bin

	or, since you're already in /home/amcom/bin:
	scp -p health_check.crn amcom2:bin


	or preferably:

	scp -p health_check.crn amcom@amcom2:bin
	
	amcom@amcom2's password:

- from db1, as the amcom user, copy "health_check.crn" from amcom1 to amcom2 with a new name (for comparison purposes):

	[amcom@amcom1 ~]$ cd /home/amcom/bin

	scp -p health_check.crn amcom@amcom2:bin/health_check_db1.txt
	
	amcom@amcom2's password:

- from wb, as the amcom user, copy "wctp_out.dmn" from amcomwb to amcomdb to a different directory with a new name (for preparation / comparison purposes):

	[amcom@amcomwb ~]$ cd /home/amcom/bin

	scp -p wctp_out.dmn amcom@amcomdb:/tmp/wctp_out_amcomwb.dmn

- from db1, as root, copy a whole folder from amcomdb to amcomwb.

In this example, we're root and copying a folder named "files" from /var/log.  We also already have a folder in /u06 (on the wb) that's writable by the amcom user (named "db1").  This is a prime example of specifying the username you're connecting to (since we disallow remote / ssh logins as root).

	[root@amcom1 log]# scp -pr files amcom@amcomwb:/u06/db1

- from the voice server, as any user, copy a file from /home/amcom/PC on the web server to a folder "/home/amcom/x" on the database server:

	scp -p amcom@amcomwb:PC/profile.cfg amcom@amcomdb:x

- from db1, as the amcom user, copy multiple files from db1 to db2, but not the whole folder.

	[amcom@amcomdb ~]$ cd /home/amcom/bin

	scp -p apdi.set host.set tap_out.set amcom@amcomdb2:/tmp

Copy file from remote host:

scp [user@]host:/path/to/SourceFile /path/to/TargetFile

Examples:

- from db1, as the amcom user, copy file from wb to an existing folder named "mytest" in /home/amcom on db1:

	scp -p appman@amcomapp:wpkg/repository/Main_Smart_Console_Config/profile.cfg mytest

	or, go to mytest first, and then copy to "dot", the current folder.

	[amcom@amcom1 ~]$ cd mytest

	scp -p appman@amcomapp:wpkg/repository/Main_Smart_Console_Config/profile.cfg .
	
	appman@amcomapp's password:

- from db2, as the amcom user, copy all prg dmp.Z files from db1 to your current location (assuming an empty folder here):

	[amcom@amcom2 ~]$ cd purge

	scp -p amcom1:purge/*.dmp.Z .

	or preferably:

	scp -p amcom@amcom1:purge/*.dmp.Z .
	
	amcom@amcom1's password:
	
	or if you're unsure of your folder location,
	
	scp -p amcom@amcom1:purge/*.dmp.Z /home/amcom/purge

- from the voice server, as any user, copy httpd.conf from /opt/amcom/apache/conf on the web server to a folder "/home/amcom/wbfiles" on the database server:

	scp -p amcom@amcomwb:/opt/amcom/apache/conf/httpd.conf amcom@amcomdb:wbfiles

- from db1, as the amcom user, copy multiple specific files from the wb to an existing folder named "wbfiles" in /tmp.

The difference between copying multiple files FROM the remote server and copying multiple files TO the remote server is that quotation marks are needed here (coming FROM) to create the file list, while above (going TO) they cannot be used.

	[amcom@amcom1 ~]$

	scp -p amcom@amcomwb:"wctp_out.ini tap_out.ini bin/wctp_out.set bin/tap_out.set" /tmp/wbfiles

KB28992