One way through ssh
Among the five ways of rsync introduced above, the second and third (1 colon) belong to the way of passing ssh. This method is to let the user log in to the remote machine and then execute the rsync task.
[root@localhost rsync]# rsync -avL test1/ .0.101:/tmp/test2/
.0.101's password:
Sending incremental file list
Created directory /tmp/test2
./
1
1.txt
2
2.txt
3
4
Sent 327 bytes received 129 bytes 182.40 bytes/sec
Total size is 0 speedup is 0.00
This method is the second way described above. It is the data copied by ssh. You need to enter the password of the 192.168.0.101 machine www account. Of course, you can also use the third way to copy:
[root@localhost rsync]# rsync -avL .0.101:/tmp/test2/ ./test3/
.0.101's password:
Receiving incremental file list
Created directory ./test3
./
1
1.txt
2
2.txt
3
4
Sent 128 bytes received 351 bytes 38.32 bytes/sec
Total size is 0 speedup is 0.00
If the above two methods are written into the script, it will be troublesome to back up, because the script is automatically entered and cannot be done. But it does not mean that there is no solution.
That is through key verification, the key does not set a password is ok. I still remember that in the past, A Ming once introduced the remote host through the key. The following is the thing.
Before the operation, we first explain the host information: 192.168.0.10 (host name Aming-1) and 192.168.0.101 (host name Aming) need to copy data from Aming-1 to Aming.
First make sure that there is this file /root/.ssh/id_rsa.pub on Aming-1:
[root@Aming-1 ~]# ssh-keygen
Generating public/private rsa key pair.
A Ming has generated a key pair before, so this file already exists. If your file does not exist on Linux, please generate it as follows:
[root@Aming-1 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
3b:74:af:e8:08:ac:99:30:3f:ef:84:7a:a0:a6:3d:89 root@Aming-1
There will be some interactive process in this process, which first prompts for the password to enter the key. For Security reasons, a password should be defined, but our purpose is to automate the synchronization of the data.
So do not enter any password here, press Enter directly, that is, the password is empty. Finally, the private key (/root/.ssh/id_rsa) and the public key file (/root/.ssh/id_rsa.pub) are generated.
Copy the contents of the public key file to the target machine:
[root@Aming-1 ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA5SPyJ / kliGTAMUan / GCN325VS8jMxvOn4uQoLU / NqBpCI3MrmvSucv6EAzxx1J2uOssW08el06LG + cUwXmm5mkqDRBV6C9qNnR / bVV5vr3QsUwbKPr7fdyJvruQWWR7cSL + mjP0SYmG2Qy2JcM3hl1IZArzC6yeUnq2Gwbax8LgbZE3XfRfOYdimwyh5Tfft7yLYipWc37k + oRUWkI3mW7PalsOlfQhxrLD / lS891y6RdSbGxMJWPoV0KMFbVh + uJgyAXpeuWl + F + / iuQPzb6w3h4pWI31bvbsE9BU82jSzHYEjpq3SN2MJN2vaLs5a0mVpm9zka / h4ITFB8Uy1iSQ == root @ Aming-1
Copy the contents of the /root/.ssh/id_rsa.pub file of the host Aming-1 and paste it into /home/ of the host Aming:
[root@Aming ~]# vim /home/
In this step you may encounter problems with the /home/ directory that can be created manually, and modify the directory permissions to 700. You can also execute the ssh-keygen command to generate this directory.
After saving the /home/ file, execute it on the host Aming-1:
[root@Aming-1 ~]# ssh .0.101
Last login: Wed Jun 12 12:24:34 2013 from 192.168.0.10
[www@Aming ~]$
You can now log in to the host Aming without entering a password. Let's first exit from the Aming host, and then execute the rsync command from the host Aming-1.
[root@Aming-1 ~]# rsync -av rsync/test1/ .0.101:/tmp/test4/
Sending incremental file list
Created directory /tmp/test4
./
1
1.txt
2
2.txt
3
4
Sent 327 bytes received 129 bytes 912.00 bytes/sec
Total size is 0 speedup is 0.00
Second, through the way of background services
This way can be understood as such, establish an rsync server on the remote host, configure rsync various applications on the server, and then the machine acts as a client of rsync to connect to the remote rsync server. The following A Ming introduces how to configure an rsync server.
Create and configure rsync configuration file /etc/rsyncd.conf
[root@Aming-1 ~]# vim /etc/rsyncd.conf
#port=873
Log file=/var/log/rsync.log
Pid file=/var/run/rsyncd.pid
#address=192.168.0.10
[test]
Path=/root/rsync
Use chroot=true
Max connections=4
Read only=no
List=true
Uid=root
Gid=root
Auth users=test
Secrets file=/etc/rsyncd.passwd
Hosts allow=192.168.0.101
The configuration file is divided into two parts: all configuration parts and module configuration parts. The global part is just a few parameters. Just like Aming's rsyncd.conf port, log file, pid file, address are all global configurations, and [ Test] The following part is the module configuration part.
There can be multiple modules in a configuration file, the module name is customized, and the format is like that in Aming's rsyncd.conf. In fact, some parameters in the module such as use chroot, max connections, udi, gid, auth users, secrets file and hosts allow can be configured as global parameters.
Of course, the parameters given by Amin are not all. You can get more information through man rsyncd.conf. The following briefly explains the meaning of these parameters:
Port specifies on which port the rsyncd service is started. The default is 873.
Log file specifies the log file
Pid file specifies the pid file. The role of this file involves the process management operations such as starting and stopping the service.
Address specifies the IP to start the rsyncd service. If your machine has multiple IPs, you can specify one of them to start the rsyncd service. The default is to start on all IPs.
[test] Specify module name, customize
Path specifies the path where the data is stored
Use chroot true|false The default is true, which means that the file is first chrooted to the directory specified by the path parameter before the file is transferred. The reason for this is to implement additional security, but the disadvantage is that you need to be roots and you cannot back up the directory file pointed to by the external symbolic link.
By default, the chroot value is true. If you have a soft connection file in your data, it is recommended to set it to false.
Max connections specifies the maximum number of connections, the default is 0, there is no limit
Read only ture|false if true, cannot be uploaded to the path specified by the module
List specifies whether the module is listed when the user queries for available modules on the server, set to true to list, false to hide
Uid/gid specifies which user/group is transferred as the file to transfer
Auth users specify the username to use when transferring
Secrets file Specifies the password file. This parameter, together with the above parameters, does not use password authentication if it is not specified. Note that the password file must have a password of 600.
Hosts allow specifies the host that is allowed to connect to the module, either IP or network segment, if multiple, separated by spaces
Edit the secrets file, you must assign 600 permissions after saving. If the permissions are incorrect, you cannot complete the synchronization.
[root@Aming-1 ~]# cat /etc/rsyncd.passwd
Test:test123
[root@Aming-1 ~]# chmod 600 /etc/rsyncd.passwd
Start the rsyncd service
[root@Aming-1 ~]# rsync --daemon --config=/etc/rsyncd.conf
After booting, you can check the log and see if the port is up:
[root@Aming-1 ~]# cat /var/log/rsync.log
[root@Aming-1 ~]# netstat -lnp |grep 873
Tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 12066/rsync
Tcp 0 0 :::873 :::* LISTEN 12066/rsync
If you want to boot, write rsync --daemon --confg=/etc/rsyncd.conf to the /etc/rc.d/rc.local file.
Test on another machine
[root@Aming ~]# rsync -avL .0.10::test/test1/ /tmp/test5/
Password:
Receiving incremental file list
Created directory /tmp/test5
./
1
1.txt
2
2.txt
3
4
Sent 143 bytes received 354 bytes 994.00 bytes/sec
Total size is 0 speedup is 0.00
Ah Ming just mentioned that there is an option called "use chroot". The default is true. If it is true, if there is a soft connection in the synchronized file, there will be a problem, first in the /root/rsync/test1/ directory of the host Aming-1. Create a soft connection file:
[root@Aming-1 ~]# ln -s /root/test.txt rsync/test1/test.txt
[root@Aming-1 ~]# ls -l rsync/test1/test.txt
Lrwxrwxrwx 1 root root 14 June 12 13:24 rsync/test1/test.txt -> /root/test.txt
Then go to the host Aming and synchronize:
[root@Aming ~]# rsync -avL .0.10::test/test1/ /tmp/test6/
Password:
Receiving incremental file list
Symlink has no referent: "/test1/test.txt" (in test)
Created directory /tmp/test6
./
1
1.txt
2
2.txt
3
4
Sent 143 bytes received 419 bytes 1124.00 bytes/sec
Total size is 0 speedup is 0.00
Rsync error: some files/attrs were not transferred (see previous errors) (code
23) at main.c(1532) [generator=3.0.6]
It can be seen that if "use chroot" is set to true, there will be problems in synchronizing the soft connection file. Below, Ah Ming will modify the rsync configuration file of the host Aming-1 and change true to false:
[root@Aming-1 ~]# sed -i 's/use chroot=true/use chroot=false/' /etc/rsyncd.conf
[root@Aming-1 ~]# grep 'use chroot' /etc/rsyncd.conf
Use chroot=false
Then perform the synchronization again on the host Aming:
[root@Aming ~]# rsync -avL .0.10::test/test1/ /tmp/test7/
Password:
Receiving incremental file list
Created directory /tmp/test7
./
1
1.txt
2
2.txt
3
4
Test.txt
Sent 162 bytes received 410 bytes 1144.00 bytes/sec
Total size is 0 speedup is 0.00
There is no problem with this, you may be wondering, why did A Ming not restart the rsyncd service after modifying the rsyncd.conf configuration file? In fact, this is a specific mechanism of rsync, the configuration file takes effect immediately, without restarting the service.
In the above example, A Ming has a password, so it can not be automatically executed in the script. In fact, this method can also be used without manual password input. It can be implemented in two ways.
The first type, specify the password file
On the client, that is, on the host Aming, edit a password file:
[root@Aming ~]# vim /etc/pass
Join the password of the test user:
[root@Aming ~]# cat /etc/pass
Test123
Change the permissions of the password file:
[root@Aming ~]# chmod 600 /etc/pass
When synchronizing, specify the password file, you can save the step of entering the password:
[root@Aming ~]# rsync -avL .0.10::test/test1/ /tmp/test8/ --password-file=/etc/pass
Receiving incremental file list
Created directory /tmp/test8
./
1
1.txt
2
2.txt
3
4
Test.txt
Sent 190 bytes received 451 bytes 1282.00 bytes/sec
Total size is 0 speedup is 0.00
Second: no user is specified on the rsync server
Modify the configuration file rsyncd.conf on the server side, that is, the host Aming-1, and remove the configuration items (auth user and secrets file) for the authentication account:
Sed -i 's/auth users/#auth users/;s/secrets file/#secrets file/' /etc/rsyncd.conf
The above command adds a "#" to the top of the "auth users" and "secrets file" lines, so that the two lines are commented out to make them meaningless.
In the previous A Ming did not talk about this usage of sed, in fact, it is not difficult to understand, just replace the two sub-command blocks with a semicolon. Then we go to the client host Aming to test:
[root@Aming ~]# rsync -avL 192.168.0.10::test/test1/ /tmp/test9/
Receiving incremental file list
Created directory /tmp/test9
./
1
1.txt
2
2.txt
3
4
Test.txt
Sent 162 bytes received 410 bytes 1144.00 bytes/sec
Total size is 0 speedup is 0.00
Note that there is no need to add test to this user. The default is to copy as root. Now you don't need to enter a password.
LED Lighting Aluminum Alloy Die Casting
Led Lighting Aluminum Alloy Die Casting,Oem Aluminium Alloy Die,Led Zinc Die Casting,Aluminium Led Light Housing
Dongguan Metalwork Technology Co., LTD. , https://www.dgdiecastpro.com