Tuesday, January 26, 2021

15 Practical examples of scp command

Must read

SATISH KUMAR
I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.
- Advertisement -

SCP is the most useful command for Linux system administrator, almost every day we use this command. In this article, we have explain most of the scenarios with scp command example.

It is used to copy a file/directory from a system to another remote system.

- Advertisement -

SCP uses the SSH protocol to transfer files. It also uses SSH authentication to communicate with a remote system or server.

Suggested Reading: Linux command Help and Examples

Here, In this article, we will discuss SCP uses through best SCP command example:

Copy a file to remote system

To copy a file into the remote server, you can use source and destination path.

$ scp  linuxconcept.pdf  [email protected]:.

The above scp command example will copy the linuxconcept.pdf file into satish’s home directory at “linuxconcept.com” system.

Copy multiple files to remote system

- Advertisement -

You can transfer multiple files using SCP tool; a space like below example can specify multiple files.

$ scp  test.txt  test1.txt  [email protected]:/tmp/

You can copy multiple files from a remote host to the current directory, as shown below:

$ scp [email protected]:/home/linuxconcept/{test.txt, test1.txt}  .

Run SCP command in Verbose mode

There is an option -v to use to get detailed information while copying the file between host. The option -v is used to execute SCP in verbose mode so that it will print debug information into the screen.

- Advertisement -
$ scp  -v  linuxconcept.pdf  [email protected]:.

Copy file with modification times, access times, and modes from original files

You can copy a file with modification time, access time and modes from original files, and the option -p will help you to do this, as shown below:

$ scp –p command.pdf [email protected]:.

Use “-C” option to transfer files faster

You transfer your files very fast using the -C option with SCP tool. The -C parameter will compress the files. The important thing is compression only happens on a network, and when it arrived on destination server it will return into the original size.

You can use the -C option, as shown below:

$ scp –C command.pdf [email protected]:.

Use cipher to encrypt files

You can encrypt files while transferring using scp command. By default, scp use AES-128 to encrypt but you can change encryption algorithm using the -c option, as shown below:

$ scp –c 3des command.pdf [email protected]:.

The above command changes the SCP encryption algorithm from AES-128 to 3DES.

Use bandwidth with SCP to control Network bandwidth

There is one option -l using with scp command to control file transfer bandwidth. This is most useful when you want to transfer a huge file, but you don’t want the all bandwidth consumed by the SCP process.

$ scp –l 400 all_command.pdf [email protected]:.

The value 400 with -l option of SCP command is limiting the bandwidth for scp process is 50KB/sec, the 400 is in Kbit so if you convert it into Kbyte it will become 50 KB.

400/8 = 50.

Use specific port for remote system with SCP command

To use a specific port with SCP command, you can use the -P option, as shown below:

$ scp -P 44444 linuxconcept.pdf [email protected]:.
[email protected]’s password:
linuxconcept.pdf 100% 4567KB 245.3KB/s 00:21

Run SCP command recursively

You can use the -r option with scp to execute it in recursively, as shown below:

$ scp –r  commands  [email protected]:.

The SCP use in recursively mode to transfer the directories with all files and directories inside of it.

Disable Progress meter and warning/diagnostic messages

In the case when you don’t want to see progress meter and warning messages while using SCP utility, use -q option, as shown below:

$ scp –q command.pdf  [email protected]:.

Use proxy with SCP command

Generally, we are using proxy servers in the office environment. SCP is not proxy configured so when we using proxy environment need to tell SCP to communicate with proxy.

Here, the proxy address is 10.10.10.10 and the proxy port is 8080. The proxy can also use the authentication, so first, you need to create “~/.ssh/config” file, then put below command inside it.

ProxyCommand  /usr/bin/corkscrew  10.10.10.10 8080 %h %p ~/.ssh/proxyauth

Where the file ~/.ssh/proxyauth contain username and password in below format:

username:password

The above command will only work if corkscrew installed in your system, to install it using below command:

$ apt-get install corkscrew

Or

# yum install corkscrew

Use different ssh_config file for SCP 

If you are always switching proxy and public network, it better to configure a different ssh_config file with all required proxy configuration which can use whenever it needed.

You can use a different ssh configuration file with SCP command, as shown below:

$ scp  -F  /home/linuxconcept/proxy_ssh_config  linuxconcept.pdf
[email protected]:
[email protected]’s password:
linuxconcept.pdf 100%  3578KB   234.3KB/s   00:13

By default ssh_config file per user will be in ~/.ssh/config. Create a specific ssh_config file with proxy compatible, and use it whenever you are in proxy network.

Whenever you use in your proxy network use -F option and when you are on a public network, you can skip the -F parameter.

SCP authenticate with Key file

You can use key-based (passwordlessauthentication; you need to use identity file which contains the private key. This identity file can use with the SCP command directly, as shown below:

$ scp  -vCq –I private_key.pem linuxconcept.pdf  [email protected]:/tmp/linuxconcept.pdf
- Advertisement -
- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Latest article