On Unix or Linux operating systems, the scp utility, stands for secure copy, is similar to the more famous command, cp, but is used to transfer files and directories between hosts on a secure encrypted network.
Since it relies on ssh for data transfer, it offers the same security and uses the same authentication ssh. The scp command will prompt for passwords for authentication (if needed), unlike rcp.
In this article, we will dive into the world of secure transfer of files in Linux using the scp command. You will see how to use this utility through detailed explanations and example use cases of the commonly used scp switches and options..
1 Since scp relies on ssh for data transfer, an ssh key or password is therefore required for authentication on the remote systems.
2 Make sure beforehand to have write permission on the target system and read permissions the source file.
3 Note that the colon (:) is used by the scp tool to distinguish between remote and local locations.
4 scp will overwrite files without prior warning when copying files that have the same location and name on either sides.
In order to start using the scp command, you need to understand its basic syntax.This takes the form below:
scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
OPTION : Represents the scp options like ssh configuration, cipher, limit, ssh port, recursive copy …etc.
[user@]SRC_HOST:]file1 : Represents the source file.
[user@]DEST_HOST:]file2 : Represents the destination file
Like any other command, the scp tool has some options that are used to control its behavior. The most common ones are the following:
-P : Indicates the ssh port of the remote host to connect to .
-p : Specifies that files modification, modes and access times are preserved from the original file.
-q : Needed to suppress non-error messages and progress meter.
-C: Forces scp to compress the data as it passes the -C flag to ssh in order to enable compression of the encrypted secure connection.
-r: Tells the scp command to copy entire directories recursively.
-S : Program name to use for the encrypted connection. Since ssh is used, the program must therefore understand ssh switches.
-v: Verbose mode. Enables scp and ssh to output messages about their progress. This is useful in debugging connection, configuration and authentication problems.
Before we start using scp, let’s copy files from one folder to another using cp. We all know how to achieve this using the cp command :
cp /home/net2_user/mysong.mp3 /home/net2_user/audio
Which copies the file mysong.mp3 in the home directory of the user net2_user to the audio directory in the home folder of the same user, i.e. net2_user.
On a similar fashion, the scp command can be used as follows :
scp /home/net2_user/mysong.mp3 email@example.com:/home/net2_user/audio
Where this will upload the file mysong.mp3 to the server net2host.com, using net2_user as the login name, into the remote directory /home/net2_user/audio. Note here that scp will prompt you for net2_user’s remote password before initiating the upload. If a remote directory has not been provided, the file will be copied to the home directory of the remote user. Not providing a filename on the destination folder, will make scp copy the file with the original name. If you want to save the file under a different name, you need to specify the new file name.
You may be interested to read: How to install OpenSSH server on Ubuntu ?
In order to download files, you could mention a remote location as the source location. For instance :
scp firstname.lastname@example.org:/home/net2_user/audio/* /home/net2_user/downloads
Which will download all the audio files in the remote directory /home/net2_user/audio on net2host.com into the local directory /home/net2_user/downloads.
Note on file names :
The name of the file may consist of a user and host in order to indicate the transfer of the file to/from that host. In order to avoid scp handling file names containing ‘:’ as host specifiers, local file names can be either absolute or relative. Remote file names however should include both user and host.
Default port number:
If on the remote host, SSH is listening on a different port, i.e. other than the default 22, then you can use the -P switch to indicate the port.
To copy a directory is to a remote location, is similar to copying files. You just need to use -r flag for recursive transfer.
To copy therefore an entire directory to remote system, run the the command with the -r option, for instance :
scp -r /home/net2_user/local_folder email@example.com:/home/net2_user/remote_folder
The following command will copy the file /folder/song.mp3 from the remote host net2_host1.com to the directory /folder on the remote host net2_host2.com.
scp net2_user1@net2_host1.com:/folder/song.mp3 net2_user2@net2_host2.com:/anotherfolder
Once this is initiated, you will be asked for the passwords for both remote destinations.
In order to route the traffic through the local host, i.e. the machine on which the command was executed, use the -3 switch as follows:
scp -3 net2_user1@net2_host1.com:/folder/song.mp3 net2_user2@net2_host2.com:/anotherfolder