Scp–secure copy (remote file copy program)
SCP = cp + SSH, the SSH protocol will copy files with the remote host.
How to use
SCP option File_source File_target
For remote path, use [email protected]_ip:file form (of course if you use SSH key on the remote host to omit) for example:
scp /home/wgjak47/A.md [email protected]:/tmp/B.md
About options
The SCP has fewer replication options than CP, and you can use the-r and-p options for recursive copying and preserving the original properties, such as information that you want to copy the directory and keep the source file's permissions, time, and so on:
scp -rp /home/wgjak47/secret [email protected]:/home/test/backup
SCP also has a subset of the same options as SSH. For example, you can specify an SSH port for a remote host:
scp -rp -P 3307 /home/wgjak47/secret [email protected]:/home/test/backup
With regard to speed, because of the use of encryption, the simple File transfer protocol such as TFTP is slower, but not serious.
Rsync
Remote synchronize, which is remotely synchronized. Rsync is mainly used for mirroring, while synchronizing files can keep the original file permissions, time, soft and hard links and other additional information.
Rsync Service Side
- Install Rsync First:
sudo apt-get install rsync
2. The Rsync service needs to be configured on the service side of rsync, involving the following three files:
- rsyncd.conf (master configuration file)
The default is in/etc, if not please copy the sample file from/usr/share/doc/rsync/example/to/etc.
cp /usr/share/doc/rsync/example/rsyncd.conf /etc/
# Sample RSYNCD.CONF configuration file
# GLOBAL OPTIONS
#motd FILE=/ETC/MOTD
#log FILE=/VAR/LOG/RSYNCD
# for PID file, does not use/var/run/rsync.pid if
# You is going to run rsync out of the the Init.d script.
# The INIT.D script does its own PID file handling,
# so omit the ' PID file ' line completely in the ' case.
# pid File=/var/run/rsyncd.pid
#syslog Facility=daemon
#socket options=
# MODULE OPTIONS
[FTP] #认证的模块名, specified when client uses
Comment = Public archive
Path =/var/www/pub # directories that need to be synchronized
Use chroot = yes # chroot to the synchronization directory before synchronization, security considerations.
# Max Connections=10
Lock file =/var/lock/rsyncd
# The default for read is yes ...
Read Only = yes
List = yes
UID = nobody #传输文件使用的用户
GID = Nogroup #传输文件使用的组别, make sure that this user and user group is able to read the file under path.
# exclude =
# Exclude From =
# include =
# include from =
# auth users = #认证的用户名, if there is no such line, it is anonymous
# Secrets file =/etc/rsyncd.secrets # # Certified password files
Strict modes = yes
# hosts allow = #允许访问的hosts
# hosts Deny = #进制访问的hosts, with front 2 selected 1
Ignore errors = no #忽略一些
Ignore nonreadable = yes
Transfer logging = No # record transfer log
# log format =%t:host%h (%a)%o%f (%l bytes). Total%b bytes.
Timeout = #超时
Refuse options = Checksum Dry-run
Dont compress = . GZ . tgz . zip . z . RPM . Deb . ISO . bz2 *.tbz #传输时不压缩以节省时间.
More detailed options can be found in the man page:man rsyncd.conf
- Rsyncd.secrets (password file)
The password file can be specified by default in/etc/rsyncd.secrets. Format is user name: password
For example:
Wgjak47:thisispwd
Set the file permissions to 600 to prevent password leaks.
RSYNCD.MOTD (RYSNC server information)
It is the user login information that defines the RYSNC server information. For example, let the user know who the server is to provide, such as:
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use my rsync services!
++++++++++++++++++++++++++++++++++++++++++++++
Enable Rsync Service (Debian):
Edit/etc/defalult/rsync, modify Rsync_enable=true
Open firewall port:
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
Enable Rsync service (UPDATE-RC.D, boot auto-start)
update-rc.d rsync start 20 2 3 4 5
Start the Rsync service
service rsync start
Client
Clients can use the rsync command to synchronize files from the server.
The command format for rsync can be:
- rsync [OPTION] ... src [src] ... [[email protected]] Host:dest
Copy the local file and start this mode of operation when both the SRC and des path information do not contain a single colon ":" delimiter.
- rsync [OPTION] ... [[email protected]] HOST:SRC DEST
Use a remote shell program (such as rsh, SSH) to copy the contents of the remote machine to the local machine. This mode is started when the SRC address path contains a single colon ":" delimiter.
- rsync [OPTION] ... src [src] ... DEST
Use a remote shell program (such as rsh, SSH) to copy the contents of the local machine to the remote machine. This mode is started when the DST path address contains a single colon ":" delimiter.
- rsync [OPTION] ... [[email protected]] HOST::SRC [DEST]
Copy files from the remote rsync server to the local machine. This mode is started when the SRC path information contains the "::" delimiter.
- rsync [OPTION] ... src [src] ... [[email protected]] HOST::D EST
Copy files from the local machine to the remote rsync server. This mode is started when the DST path information contains the "::" delimiter.
- rsync [OPTION] ... rsync://[[email protected]]host[:P ort]/src [DEST]
Options:
-R is recursive
-L is the link file, which means to copy the link file;-p to maintain the original permissions of the file;-T to keep the file's original user group;-O to keep the original owner of the file;-d equivalent to block device files;
-a,–archive archive mode, which means that files are transferred recursively, and all file attributes are maintained, equal to-rlptgod
-Z compression during transmission;
-P transmission progress;
-V Transfer progress and other information, and-p a little relationship, try it yourself. can read the document;
The parameters of-e ssh establish an encrypted connection.
-U only updates to prevent local new files from being rewritten, noting both machine clocks while
–progress means to show detailed progress
–delete means that if the server side deletes the file, then the client will also delete the file accordingly, maintaining a true consistency
–password-file=/password/path/file to specify the password file so that it can be used in the script without having to enter the authentication password interactively, it is important to note that the password file permission attribute is set to be only master readable.
The list of files for the remote machine is listed. This is similar to the rsync transfer, but only if the local machine information is omitted from the command.
Examples:
rsync -avzP [email protected]::test test
This command means to log on to the server with the root user and synchronize the test data to the local current directory test. Of course, local directories can be defined by yourself. If you do not have the test directory under the current directory on the client, the system will automatically create one for you, and you should pay attention to its write permissions if there is a test directory.
SCP and Rsync