Thursday, April 27, 2023

10 Example of SCP (Secure Copy) Command in Linux

Hello guys, in last few articles I have explained how to use netstat, curl, and lsof command in Linux and in this article, I am going to share how to use SCP command in Linux to copy files and directory from remote host. Similar to SSH, its one of the essential command for any Java Programmer or IT professional as most of the time you will need to transfer data between servers and then you will need either SCP, SFTP, or rSync. The scp is another essential command for Linux user which allows you to copy files and directory from one server to another in a Linux network. It is based upon SSH and behave similarly in terms of authentication and logging but allows you to copy a file or directory from remote server. 

10 SCP Command Example in Linux for Beginners

Before we checkout the frequently used SCP command example in Linux and UNIX, let's get ourselves familiar with the syntax of SCP command as you will need to remember it whenever you want to use it.

syntax: scp [-r] user@host1:file1 user@host2:file2

1) Copying files from remote host as current user
You can also call this as downloading a file from server

$ scp .

This command login into as current user and copies the file /home/user1/app.jar into current directory. When you run this command it will ask for password of current user and if current user has permissions to access the then it will download that file. 

2) Copying files from remote host as any user
Sometime, you need to download a file as different user e.g. by running some scripts. SCP allows you to specify username along with hostname in the format user@host:path, where path is the location of the file you want to copy. here is an example of copying files from remote server as any user other than current one:

$ scp .

This command will ask password for user1 when you will execute this command on Linux. Once you enter the password it will download the file /home/user1/app.jar into current directory. 

3) Copying files from local host to remote host
You can also call this uploading a file on server.

$ scp myapp.jar

This command will copy the myapp.jar from current directory in local machine to the /tmp directory of and ask password for user1 when you run this command. The user1 must have access to for this command to be successful. 

4) Copying files from remote host without password
Since SSH allows you to login without password by using RSH private key, you can also use SCP to download or upload file from remote server without entering password, just provide the identity file containing private key to scp using -i option as shown below:

$ scp -i ~/.ssh/id_rsa .

Here the file ~/.ssh/id_rsa contains private key for user1. 

If you don't have private key then you can generate the public key private key pair by using the ssh-keygen -t rsa command. 

5) Copying a directory from local host to remote host with current user
You can copy an entire directory with sub-directory and files to remote host using scp command in Linux. The SCP provides recursive copy option using -r flag, this option recursively copy entire directory as shown below:

$ scp -r debug_logs/

This will copy all files inside debug_logs folder in current directory as well as any sub-directory to the /tmp folder of

10 Example of SCP (Secure Copy) Command in Linux

6) Printing debug information
Sometime when you are trying to copy files or directory from one host to another but failing for some unknown reason, it's a wise decision to run scp with -v option which prints debug information, -v stands for verbose. 

For example, when you run following command it will print a lot of useful debug information which is helpful for understanding how SSH login works and how SCP copies file from one server to another:

$ $ scp -v debug.log xtky3135vdap:/tmp
Executing: program /usr/bin/ssh host, user (unspecified), command scp -v -t /tmp
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to [] port 22.
debug1: Connection established.
debug1: identity file /home/user1/.ssh/identity type -1
debug1: identity file /home/user1/.ssh/id_rsa type 1
debug1: identity file /home/user1/.ssh/id_rsa1 type -1
debug1: identity file /home/user1/.ssh/id_rsa2 type -1
debug1: identity file /home/user1/.ssh/id_dsa type -1
debug1: loaded 5 keys
debug1: Remote protocol version 2.0, remote software version OpenSSH_4.3
debug1: match: OpenSSH_4.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received

You can see a lot of debug information which explains every steps SCP program executes to copy a file e.g. in first few steps SSH login has explained. 

7) Copying file from one host to another
Suppose you want to copy a file from one host to another without logging into any of them, can you do it with SCP? Yes, we can do that, instead of copying to/from local host, we can explicitly specify source and destination hosts to SCP command as shown below:

$ scp user1@host1:/home/user1/abc.txt user1@host2:/tmp/

The key is that you login to both host using same user and the user must have access to both the host. 

8) Compressing file before copying to remote host
By default scp uses the Triple-DES cipher to encrypt the data being sent. Using the Blowfish cipher has been shown to increase speed. This can be done by using option -c blowfish in the command line.

$ scp -c blowfish /tmp/debug.log
It is often suggested that the -C option for compression should also be used to increase speed. The effect of compression, however, will only significantly increase speed if your connection is very slow. Otherwise it may just be adding extra burden to the CPU. 

Here is an example of using blowfish and compression:

$ scp -c blowfish -C local_file

9) How to copy multiple files from one machine to another in Linux
You can copy multiple files using single SCP command from one host to another in Linux. You just need to specify the file names with comma as shown in following example:

$ scp{app.jar,config.tx} .

This will copy both app.jar and config.txt from the /tmp directory of remote host to current directory of localhost. Though it will ask to enter password twice, you surely don't want to do that. The following version which encloses list of files into \{ }\ will only ask for password once.

$ scp\{app.jar,config.tx}\ .
app.jar 100% 7235KB 1.4MB/s 00:05
config.txt 100% 812 0.8KB/s 00:00

10) Copying multiple files from local host to remote host
You can also copy multiple files and directory from remote host as shown in following example:

$ scp\{a,b,c\} . 

That's all about essential and useful SCP command examples in Linux. It is one of the essential command for any Linux user, be it a developer, support guy, BA, project managers or DBAs. You often find yourself transferring data from one server to another and scp command really helps there. 

You can also use scp -i to download a file from remote server without entering a password, something which you can automate by writing shell script. 

In short, The scp command copies files between hosts on a network. It uses ssh for data transfer, and uses the same authentication and provides the same security as ssh.

No comments :

Post a Comment