Automatic remote copy using SCP command in Linux

Source: Internet
Author: User
Tags scp file scp command scp command in linux

In general, the copy command prompts us to enter the username and password for the remote server, which is an obstacle in automating the program because there is no manual intervention. About the server between the exchange of the password does not need to be introduced before, you can refer to the previous article. Here is an introduction to the SCP Copy command.

SCP Local username @ip address: filename 1 remote username @ip address: filename 2

[Local username @ip address:] You may need to enter the password for the remote user name without entering it.

SCP command
==================
The SCP can replicate files between 2 Linux hosts;

Command basic format:
SCP [Optional parameters] File_source File_target

======
Replicate from local to remote
======
* Copy files:

* Command format:
SCP Local_file Remote_username@remote_ip:remote_folder
Or
SCP Local_file Remote_username@remote_ip:remote_file
Or
SCP Local_file Remote_ip:remote_folder
Or
SCP Local_file Remote_ip:remote_file

1th, 2 specified the user name, the command needs to enter the password after the execution, the 1th specified only the remote directory, the file name unchanged, the 2nd specified the filename;
3rd, 4 did not specify the user name, the command needs to enter the user name and password, the 3rd specified only a remote directory, the file name unchanged, the 4th specified the filename;
* Routines:
Scp/home/space/music/1.mp3 Root@www.cumt.edu.cn:/home/root/others/music
Scp/home/space/music/1.mp3 Root@www.cumt.edu.cn:/home/root/others/music/002.mp3
Scp/home/space/music/1.mp3 Www.cumt.edu.cn:/home/root/others/music
Scp/home/space/music/1.mp3 Www.cumt.edu.cn:/home/root/others/music/002.mp3

* Copy directory:
* Command format:
Scp-r Local_folder Remote_username@remote_ip:remote_folder
Or
Scp-r Local_folder Remote_ip:remote_folder

The 1th specifies the user name, the command needs to enter the password after the execution;
The 2nd does not specify the user name, the command needs to enter the user name and password after execution;
* Routines:
Scp-r/home/space/music/root@www.cumt.edu.cn:/home/root/others/
Scp-r/home/space/music/www.cumt.edu.cn:/home/root/others/

The above command copies the local music directory to the remote others directory, which is remotely available after replication. /others/music/Directory

======
From remote replication to local
======
From remote to local, as long as the following 2 parameters of the command will be copied from the local to the remote order can be replaced;

For example:

SCP Root@www.cumt.edu.cn:/home/root/others/music/home/space/music/i.mp3
Scp-r www.cumt.edu.cn:/home/root/others//home/space/music/

Assuming that the IP address of two server A and b,b servers is 115.47.169.130, it is now necessary to copy the/var/logs/test.log files on the B server to the/bak/directory on Server A. You can execute commands on the a server

/USR/BIN/SCP Root@115.47.169.130:/var/logs/test.log/bak/test.log

If you need to copy the/bak/test.sql from server A to the/data/directory on Server B, you can execute on server a

/usr/bin/scp/bak/test.sql Root@115.47.69.130:/data/test.sql

Speaking of here, if you want to operate on the B server, I believe everyone should be all right, another point if the remote server default port is not 22, you can use the-P command to specify the port after the SCP, for example:

/usr/bin/scp-p2200 Root@115.47.169.130:/var/logs/test.log/bak/test.log


several parameters that may be useful:

-V, as in most Linux commands, is used to show progress. Can be used to view connections, authentication, or configuration errors.
-C enables compression options.
-P Select the port. Note-P has been used by RCP.
-4 forcibly use IPV4 address.
-6 forcibly use IPV6 address.
To copy a single file command:
SCP file Username@ip:filepath
Description
File: filename to copy
Username: User name for remote login,
IP: Remote server IP
FilePath: Remote file path
Copy folder commands as follows (plus a-r argument):
Scp-r file Username@ip:filepath

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.