The ‘SCP’ Command in Linux – The Easiest Way to Copy Securely
Linux has been around for more than a decade. It is currently the most popular free, open-source operating system software in the world. You will find that Linux is used in servers all over the world. In Linux, users have to type in commands to create and handle files, unlike Windows. However, Linux has several advantages over your everyday Windows OS. One of the advantages of running Linux, especially on servers, is that the data that is passed between computers is transferred safely (and effectively).
The SCP Command in Linux
The SCP command in Linux is used to copy files over a network connection in a secure way (provided there is a working network connection in place, of course). The SCP command provides security by asking for passwords and passphrases before the data can be copied. The SSH (Secure Shell) system is used to transport data between two machines by the SCP command. This means that all the features of the SSH system (which will be explained later) are available in the SCP system.
Copying Files to a Remote Host
The syntax for the SCP command is as follows:
scp name_of_file host_name :
The above command will work if you want to copy a file from the directory you have currently navigated to, to another directory that is located on another computer- provided that you have the same username on both computers. Here, the “name_of_file” is the name of the file you’re looking to transfer and the host_name is the name of the destination host. The name of the destination host will usually be like an internet address line, like: linux. nmt. gov.
If you have a different username on the destination host, you need to change the command a little:
scp name_of_file yourusername@host_name :
If you want to place the file you want to copy to a directory that isn’t the home directory (which is the default location), you can use the following command:
scp name_of_file yourusername@host_name : / path1/ path2/ directory_name
If you want to change the name of the file as you’re placing it, you can just append the above command like this:
scp name_of_file yourusername@host_name : / path1/ path2/ directory_name/ new_name_of_file
Alternatively, you can name your file when you place it in your home directory, as follows:
scp name_of_file yourusername@host_name : new_name_of_file
The SCP command lets you copy multiple files at once too. Just follow the usual procedure, except this time use the scp command.
You can copy a file that is located in directory that you aren’t in currently with the following command:
scp path1/ path2/ directory/ name_of_file : yourusername@host_name:
Copying From a Remote Host
Instead of copying files to a remote host, you can copy files from a remote host. The syntax for this is as follows:
scp name_host : name_of_file .
Notice that we have used a “.” after the filename. This will tell Linux to copy your file (name_of_file) to the directory you have currently navigated to. You can navigate to any directory of your choice before issuing this command. Alternatively, you can change the directory the file will be copied to with the following command:
scp name_host : name_of_file path1/ path2/ directory
Copying Files Without Being Logged in on Remote Systems
The SCP command also lets you copy files from one remote machine to another, even if you’re not logged in to either of them. You will, however, need to input passwords for both the systems.
The syntax for this is as follows:
scp name_user1@name_host1 : name_of_file name_user2@name_host2 :
You will be asked for a password when copying from the first system (name_host1) to the second system (name_host2). Please note that there are two colons “:” in the above command. The first colon is in the middle of the command and the second at the end of the command. The file will get copied to the default location (the home directory). If you want to change the directory the file will be copied to, you need to specify a different path (as given in the above examples).
You don’t need to specify a username while typing the command if it is the same on both systems. To copy from one remote system to another when the username is the same (to the home directory), use the following command:
scp name_host1 : name_of_file host2 :
Please take note that there are two colons “:” in the above command as well.
The SCP command works in the background, so you can’t keep track of the progress unless you include certain parameters. Either the file will be copied successful or your will be shown an error message indicating the copying has failed.
You can include the –v parameter to get a verbose report on screen that will help you diagnose any authentication or connection problems with the scp command:
scp -v name_of_file host_name :
If you want to know how long the copying process will take, you can use the “-p” paremeter as follows:
scp –p name_of_file host_name :
This command will show you the estimated time it will take to copy the file.
You can also combine the above two commands, like this:
scp –pv name_of_file host_name :
If you think the process is going to take too long, you can use the “-C” parameter to speed things up. The “-C” parameter tells Linux to compress the file as it’s traveling on the network. On arrival, it is automatically decompressed. This usually drastically speeds up the copying process. Please note that the “-C” parameter is uppercase. The syntax for this is as follows:
scp –C name_of_file host_name :
You can combine all three commands to get a debug report, estimated time to completion and also to compress your file:
scp –Cpv name_of_file host_name :
SSH and SCP
SCP can use most of the facilities provided by SSH (Secure SHell). This allows you to set usernames and passwords and radically beefs up the protection your files enjoy. SSH commands should be combined with SCP commands for the best results.
We hope this gives you a good overview of how to use the SCP command in Linux. Mastering the Linux command line can seem daunting at first, but its quite easy once you learn the basic patterns. If you’re looking to land a job as a LInux system admin, you’d better be pretty thorough.
Top courses in Linux
Linux students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.