What is scp?
scp stands for secure copy and is a means of transferring files between local host and remote host or between two hosts. It’s a file transfer method through SSH (Secure Shell) and allows file sending and receiving without installing a separate FTP client.
How to Use scp
Usage is as follows. First, when transferring files from local host to remote host:
scp filepath/filename username@targetservername:destinationpath/filename
# For example, if you want to send the test.txt file in the Desktop directory to the root directory of the madlife server,
scp Desktop/test.txt madplay@madlife:/root # madplay is the username
Conversely, when transferring files from remote host to local host:
scp username@servername:filepath/filename destinationlocalpath
# For example, if you want to move the test.txt moved above to local
scp madplay@madlife:/root/test.txt /Desktop/test.txt
scp options include:
- r : Recursive that copies everything including subfolders
- p : Preserve that maintains permissions and attributes
- c : Compress that performs compression, etc.
What is rsync?
rsync stands for remote synchronization and copies and synchronizes files and directories on remote locations.
How to Use rsync
rsync has many uses. Here, let’s learn how to use it in server mode using the –daemon option. For rsync servers, modify rsync-related files.
vi /etc/xinetd.d/rsync
# Change the value of disable to no.
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
Next, set connection alias and server.
# If the file below doesn't exist, create it.
vi /etc/rsyncd.conf
[alias name]
path = /home/www
uid = nobody
gid = nobody
use chroot = yes
read only = false
hosts allow = allowed IP
# path : Directory location of service target
# uid : User id / default is nobody
# gid : User group id / default is nobody
# use chroot : Use the above path as root directory (yes)
# read only : Read-only (set to no for client -> server cases)
# hosts allow : Host-based access allow settings / default is all hosts / write IPs to allow access
# comment : Description, comment for the configuration
# max connections : Number of simultaneous connections
# timeout : Timeout time settings
Finally, restart xinetd.
/etc/init.d/xinetd restart
# rsync uses port 873, so if it's blocked, you need to allow it.
Additional) Related to CentOS 7 version
systemctl start rsyncd.service
# Restart is systemctl restart rsyncd.service
# After server reboot, systemctl enable rsyncd.service
Next is rsync client configuration. First, check rsync connection availability.
telnet <rsync server IP> 873
# For example, telnet xxx.xxx.xxx.xxx 873
# Use telnet to check if port 873 connects normally.
Execute rsync.
rsync -avz <IP>::<alias specified above>path directory
#For example, assuming the rsync server configured is madlife (123.123.123.123)
#and assuming the server to get data from madlife and synchronize is madplay
#if synchronizing contents from madlife's /home/www to madplay's /home/www
#On madplay server
rsync -avz madlife::aliasname/home/www /home/www
rsync -avz 123.123.123.123::aliasname/home/www /home/www
rsync options include:
- a : Archive that brings permissions, owners, and locations as is
- v : Verbose that shows detailed synchronization progress
- r : Recursive that recursively executes subdirectories of the specified directory
- p : Perms that preserves information like modification time and permissions of original files
- z : Compress that compresses data for transmission, etc.
Difference Between scp and rsync
We mainly use scp and rsync when transferring files remotely. So what’s the difference between them? First, rsync is faster than scp. This is because it uses the remote-update protocol to copy only files with differences. The first time, it will copy all files and directories, but from then on, it’s efficient because it only copies files with differences.
On the other hand, there’s a difference in Symbolic Link handling. scp doesn’t maintain symbolic links among files being transferred and transfers the original files linked.