Several methods of transferring files between two Linux systems

Source: Internet
Author: User
Tags bz2 scp file file permissions rsync scp command

SCP Transfer

You can use the SCP command when you want to transfer files between two Linux hosts.

SCP transfer is slow, but the security of the transmission is ensured by using SSH Channel

Copying files
    • Copy a local file to a remote

SCP file name – User name @ computer IP or computer name: remote path

    • Copy files back to local from remote

scp– User name @ computer IP or computer name: file name Local path

Command format

1th, 2 Specify the user name, command execution after the need to enter the password, the 1th only specifies the remote directory, the file name is unchanged, 2nd specified the file name;
3rd, 4 does not specify a user name, command execution after the need to enter a user name and password, the 3rd only specifies the remote directory, the file name is unchanged, 4th specified the file name;

Copy Directory
    • Copy a local directory to a remote

Scp-r Directory name User name @ computer IP or computer name: remote path

    • Copy directory back to local from remote

SCP-R User name @ computer IP or computer name: directory name Local Path

Command format

The 1th one specifies the user name, the command executes after the need to enter the password;
The 2nd does not specify a user name, the command will need to enter the user name and password after execution;

Example

Detailed parameters
Parameters Description
-A As far as possible, the file status, permissions and other information are reproduced as original
-R If the source contains a directory name, the files in the directory are also copied to the destination
-F If a destination already has a file with the same file name, it should be deleted before copying it.
-V The same as-V in most Linux commands to show progress. Can be used to view connections, certifications, or configuration errors
-C Enable compression options
-P Select the port. Note-P has been used by RCP
-4 Forcibly use IPV4 address
-6 Forcibly use IPV6 address
Examples Show
    1. Copy everything under the computer named "V111.nn" to the native/home/admin/directory.
    1. As admin, the IP address is "192.168.219.125",/home/admin/test directory all the things are copied to the native/home/admin/directory
     scp -r  admin@192.168.219.125:/home/admin/test  /home/admin/

Reference

Configure SCP to transfer files between Linux or Unix without a password

Linux CP/SCP Command +SCP command detailed

[CentOS cannot use SCP command workaround] (http://www.linuxidc.com/Linux/2014-09/106569.htm_

SCP copy file between two Linux hosts

Rsync differential Transmission (support for breakpoint continuation, data synchronization)
rsync -av /backup/ -e ssh [email protected]192.168.1.110:/bak

-a:archive archive mode, which means to transfer files recursively and keep all file attributes, links, etc., equal to-rlptgodrsync--remote sync.

Rsync is a file synchronization and data Transfer tool under the Linux system that uses the "rsync" algorithm to synchronize files between a client and a remote file server, or to back up data from one partition to another on the local system.

If Rsync has a data transfer interruption during the backup process, it can continue to transmit the inconsistent portions after recovery. Rsync can perform full or incremental backups.

Its main features are:

    1. Can be mirrored to save the entire directory tree and file system;

    2. It is easy to maintain the original file permissions, time, soft and hard links, without special permission to install;

    3. Can incrementally synchronize data, file transfer efficiency is high, so synchronization time is short;

    4. You can use RCP, ssh and other means to transfer files, of course, you can also through a direct socket connection;

    5. Support anonymous transmission, in order to facilitate the site mirror image, etc.;

    6. Encrypted transmission of data to ensure the security of the data;

Rsync to implement file backup synchronization detailed

Rsync synchronizes two servers

rsync remote sync under CentOS 6.5

Data backup and synchronous configuration using rsync under Linux

Linux uses the Rsync client and server-side synchronization directory for backup

Pipeline transport (reduced IO overhead)
gzip -c sda.img | ssh [email protected]192.168.1.110 "gunzip -c - > /image/sda.img"

Using gzip compression for sda.img, the-c parameter indicates output to stdout, which is piped

Gunzip-c-"-" means receiving a sdtin from a pipe

NC Transport (Data flow redirection of a network)

What NC does is to establish a TCP or UDP link between the two computers and transfer traffic between the two ports, which is a network traffic redirection

Clone a disk partition using DD combined with NC command network

Host:

if=/dev/vda | gzip -c | nc -l 50522

For recovery machine:

192.168.215.63 50522 | gzip -dc | dd of=/dev/sda

The DD command clones the/DEV/VDA disk and uses gzip compression to redirect the data stream to the native 50522 port, and to use NC to connect the host 50522 port on the recovery machine, it can receive the bit data stream of the host 50522 port, then unzip it using gzip and revert to/dev/ SDA disk

The DD command reads the disk sector, so regardless of the disk file system, or partition table, disk MBR information, DD is able to replicate, you can use the Bs,count parameter to control the size to be cloned

For example, DD bs=512 count=1 If=/dev/vda of=mbr.img only copies the first sector of the disk VDA 512K byte data (bootstrapper and partition Table)

In general, you can use the SCP to complete the file transfer between two hosts, but in the case of no trust between the host, the SCP needs to enter the password every time, it is not very convenient to use, this note has been introduced before the password to execute the script or transfer the file method, but for some temporary tasks, Preparation is still a bit of a hassle.

What is NC?

NC is a shorthand for netcat, about what NC is, man says: arbitrary TCP and UDP connections and listens

NC can easily implement any TCP/UDP port listening, connection establishment, port scanning, and so on. That is, NC can either listen to the specified port as a server in TCP or UDP, or it can initiate a TCP connection or a UDP packet as a client. The trick to be described below is to use its TCP connection to implement inter-host file transfer.

NC control parameters are many, commonly used several parameters are listed as follows:

Note: The destination port for the NC connection can either be a specific integer or be specified directly through the service name, in which case the NC is responsible for mapping the name to a well-known port (Cat/etc/services can be used to view the mappings between these ports and the service name)

Parameters Description
-L Used to specify that the NC will be in listening mode. Specifying this parameter means that the NC is treated as a server, listening and accepting connections, rather than initiating connections to other addresses
-P Port Specifies the port to be used by the NC. If the-l was previously specified, then-P here specifies the specific listening port, and if-L is not specified, the connection is initiated to this port
-S Hostname/ip-address Specifies the source IP address for sending data for multi-NIC machines
-U Specifies that the NC uses the UDP protocol, which defaults to TCP
-V Output interaction or error messages, especially useful for novice debugging
Use NC to transmit data between hosts

After the introduction of the face of NC, it is estimated that many students already know how to use NC to achieve the transfer of files between the two machines. Here is a brief description of the implementation process.

Listening on the specified port on the data receiver's machine
8210 > demo.txt        # 在本机8210端口侦听TCP连接,将收到的数据写入文本文件nc -l -p 8210 > demo.tar.bz2    # 在本机8210端口侦听TCP连接,将收到的数据写成压缩文件

Sends data in TCP to the specified address (ip+port) on the data sender machine
8210 < demo.txt      # 向ip为dest_ip的机器的8210端口发送demo.txt文件nc dest_ip 8210 < $(tar -jcvf demo.tar.bz2 demo.txt) # 压缩后发送
-V Print error message
8210  发送方:nc -v dest_ip 8210

Note: When using NC to transfer files, attention should be paid to the sequence of operations, receiver listens to the port first, sender sends the data to the receiver's machine on that port. Otherwise, the send fails.

Set up a file server

In addition to the way in which files are transferred centrally, it can also be transmitted via a resume file server and then via a network mount, this applies to regular copies, and below is an example of mounting an FTP server.
Mount the FTP server

yum install curlftpfsmkdir /data/ftpcurlftpfs username:[email protected] /data/ftp

Several methods of transferring files between two Linux systems

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.