Saturday, April 27, 2024
HomeJava10 Instance of SCP (Safe Copy) Command in Linux

10 Instance of SCP (Safe Copy) Command in Linux


Hi there guys, in previous few articles I’ve defined how one can use netstat, curl, and lsof command in Linux and on this article, I’m going to share how one can use SCP command in Linux to repeat recordsdata and listing from distant host. Just like SSH, its one of many important command for any Java Programmer or IT skilled as more often than not you have to to switch information between servers after which you have to both SCP, SFTP, or rSync. The scp is one other important command for Linux person which lets you copy recordsdata and listing from one server to a different in a Linux community. It’s based mostly upon SSH and behave equally by way of authentication and logging however lets you copy a file or listing from distant server. 

10 SCP Command Instance in Linux for Newcomers

Earlier than we checkout the regularly used SCP command instance in Linux and UNIX, let’s get ourselves aware of the syntax of SCP command as you have to to recollect it everytime you need to use it.

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

1) Copying recordsdata from distant host as present person

It’s also possible to name this as downloading a file from server

$ scp host1.com:/house/user1/app.jar .

This command login into host1.com as present person and copies the file /house/user1/app.jar into present listing. Whenever you run this command it is going to ask for password of present person and if present person has permissions to entry the host1.com then it is going to obtain that file. 

2) Copying recordsdata from distant host as any person

Someday, you want to obtain a file as completely different person e.g. by operating some scripts. SCP lets you specify username together with hostname within the format person@host:path, the place path is the situation of the file you need to copy. right here is an instance of copying recordsdata from distant server as any person aside from present one:

$ scp user1@host1.com:/house/user1/app.jar .

This command will ask password for user1 when you’ll execute this command on Linux. When you enter the password it is going to obtain the file /house/user1/app.jar into present listing. 

3) Copying recordsdata from native host to distant host

It’s also possible to name this importing a file on server.

$ scp myapp.jar user1@host1.com:/tmp/

This command will copy the myapp.jar from present listing in native machine to the /tmp listing of host1.com and ask password for user1 if you run this command. The user1 will need to have entry to host1.com for this command to achieve success. 

4) Copying recordsdata from distant host with out password

Since SSH lets you login with out password by utilizing RSH non-public key, it’s also possible to use SCP to obtain or add file from distant server with out getting into password, simply present the id file containing non-public key to scp utilizing -i choice as proven under:

$ scp -i ~/.ssh/id_rsa user1@hsot1.com:/house/user1/itemdetails_03072017.txt.gz .

Right here the file ~/.ssh/id_rsa comprises non-public key for user1. 

If you do not have non-public key then you may generate the general public key non-public key pair by utilizing the ssh-keygen -t rsa command. 

5) Copying a listing from native host to distant host with present person

You possibly can copy a complete listing with sub-directory and recordsdata to distant host utilizing scp command in Linux. The SCP offers recursive copy choice utilizing -r flag, this selection recursively copy whole listing as proven under:

$ scp -r debug_logs/ host1.com:/tmp

This can copy all recordsdata inside debug_logs folder in present listing in addition to any sub-directory to the /tmp folder of host1.com

6) Printing debug data

Someday when you’re attempting to repeat recordsdata or listing from one host to a different however failing for some unknown cause, it is a smart determination to run scp with -v choice which prints debug data, -v stands for verbose. 

For instance, if you run following command it is going to print plenty of helpful debug data which is useful for understanding how SSH login works and the way SCP copies file from one server to a different:

$ $ scp -v debug.log xtky3135vdap:/tmp
Executing: program /usr/bin/ssh host host1.com, person (unspecified), command scp -v -t /tmp
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Studying configuration information /and many others/ssh/ssh_config
debug1: Making use of choices for *
debug1: Connecting to host1.com [10.20.104.243] port 22.
debug1: Connection established.
debug1: id file /house/user1/.ssh/id kind -1
debug1: id file /house/user1/.ssh/id_rsa kind 1
debug1: id file /house/user1/.ssh/id_rsa1 kind -1
debug1: id file /house/user1/.ssh/id_rsa2 kind -1
debug1: id file /house/user1/.ssh/id_dsa kind -1
debug1: loaded 5 keys
debug1: Distant protocol model 2.0, distant software program model OpenSSH_4.3
debug1: match: OpenSSH_4.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Native model string SSH-2.0-OpenSSH_4.3
debug1: SSH2_MSG_KEXINIT despatched
debug1: SSH2_MSG_KEXINIT obtained

You possibly can see plenty of debug data which explains each steps SCP program executes to repeat a file e.g. in first few steps SSH login has defined. 

7) Copying file from one host to a different

Suppose you need to copy a file from one host to a different with out logging into any of them, are you able to do it with SCP? Sure, we will do this, as an alternative of copying to/from native host, we will explicitly specify supply and vacation spot hosts to SCP command as proven under:

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

The secret is that you simply login to each host utilizing similar person and the person will need to have entry to each the host. 

8) Compressing file earlier than copying to distant host

By default scp makes use of the Triple-DES cipher to encrypt the info being despatched. Utilizing the Blowfish cipher has been proven to extend pace. This may be finished by utilizing choice -c blowfish within the command line.

$ scp -c blowfish /tmp/debug.log user1@host1.com:~

 

It’s usually instructed that the -C choice for compression also needs to be used to extend pace. The impact of compression, nevertheless, will solely considerably enhance pace in case your connection may be very gradual. In any other case it could simply be including additional burden to the CPU. 

Right here is an instance of utilizing blowfish and compression:

$ scp -c blowfish -C local_file your_username@remotehost.com:~

9) The way to copy a number of recordsdata from one machine to a different in Linux

You possibly can copy a number of recordsdata utilizing single SCP command from one host to a different in Linux. You simply have to specify the file names with comma as proven in following instance:

$ scp user1@host1.com:/tmp/{app.jar,config.tx} .

This can copy each app.jar and config.txt from the /tmp listing of distant host to present listing of localhost. Although it is going to ask to enter password twice, you certainly do not need to do this. The next model which encloses listing of recordsdata into { } will solely ask for password as soon as.

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

10) Copying a number of recordsdata from native host to distant host

It’s also possible to copy a number of recordsdata and listing from distant host as proven in following instance:

$ scp username@remotehost.edu:/some/distant/listing/{a,b,c} . 

That is all about important and helpful SCP command examples in Linux. It is without doubt one of the important command for any Linux person, be it a developer, help man, BA, challenge managers or DBAs. You usually end up transferring information from one server to a different and scp command actually helps there. 

It’s also possible to use scp -i to obtain a file from distant server with out getting into a password, one thing which you’ll automate by writing shell script. 

In brief, The scp command copies recordsdata between hosts on a community. It makes use of ssh for information switch, and makes use of the identical authentication and offers the identical safety as ssh.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments