Site icon LinuxAndUbuntu

Linux File Sharing Over Network Computers Using scp And rsync

Linux File Sharing Over Network Computers Using scp And rsync

Linux File Sharing Over Network Computers Using scp And rsync

In this article, we will look at scp and rsync, two powerful Linux utilities for sharing files and folders across networks. scp allows you to simply copy directories or files from or to any remote destination, whereas rsync, in addition to simply copying files, also acts as a synchronising tool, synchronising changes between source and destination. The following discussion focuses on their application and the various parameters that can be used in conjunction with them to improve speed and security.

What Is SCP?

The SCP protocol is a network protocol that is based on the BSD RCP protocol. It allows file transfers over a network using Secure Shell (SSH). A client can use SCP to send files to another client or server on the same network. It can also connect to a server and request or download files. If a user wants to share multiple files across a network computer, there are two options. The user has the option of specifying each file in the command and then providing the destination path. The second method is for the user to save all of the files in one folder and share the entire folder across the network computer. Both methods are discussed further below. You are free to use whoever you want.

Install OpenSSH Server In Linux

For Linux data transfer over the network, OpenSSH is required. I discovered that it is not already installed in some Linux distributions, so the commands fail. Install OpenSSH server before you begin sharing files via SCP.

Install OpenSSH Server

For Debian or Ubuntu based Linux distributions –

sudo apt-get install openssh-server
For Fedora Based Linux distributions -
sudo dnf install openssh-server

Syntax – How To Share Files Over A Network Computer?

SCP Syntax

scp [optionalParameters] source destination

Example – If you want to share a file name “myMovieList.txt” to a remote host(192.168.1.102), you can use the following command based on the above syntax. SCP Linux File Sharing

scp myMovieList.txt savan.patel@192.168.1.102:/home/savan.patel/

To copy file from remote machine to your local machine

scp savan.patel@192.168.1.102:/home/savan/ myMovieList.txt

To copy multiple files, provide space separated list of files and at the last destination path SCP – Copy multiple files

scp file1 file2 file3 savan.patel@192.168.1.102:/home/savan.patel/

Using SCP Parameters For More File Sharing Options

You can use scp command with certain optional parameters that help you in faster copying and/or secure data transfer.

r: To copy directory you have to use parameter r to recursively copy the contents.

​The r command makes it very simple to transfer multiple files contained in a single folder. It’s one of the best methods to do quick and secure Linux file sharing over network computers. ‘r’ Parameter to copy the folder to a host computer

For example to copy blogs folder to remote machine, you would use command –

scp -r /home/savan/blog savan.patel@192.168.1.102:/home/savan.patel/

P: To send out file on the machine with different port​By default, scp shares file over network computers through port 22. In many cases, you would want to use another port then you can P parameter to use another port for folder sharing. ‘P’ Parameter to share files on host computer through a different port

scp -P8080 -r /home/savan/blog savan.patel@192.168.1.10:/home/savan.patel/

c: Change Encryption algorithm by default, scp uses Triple-DES cipher to encrypt data before transferring it. You can use other ciphers like blowfish (which has higher performance over default one). ‘c’ Change Encryption Algorithm

scp -c blowfish movieList.txt savan.patel@192.168.1.102:/home/savan.patel/

C: To transfer even faster you can use compression parameter that would compress the data while transferring and auto decompress it on the destination.

scp -C movieList.txt savan.patel@192.168.1.102:/home/savan.patel/

What Is rsync?

Rsync is useful for both sharing and synchronising files and folders. In contrast to SCP, rsync only transfers differences between source and destination files/folders. It is faster than SCP because it uses compression and decompression by default. It will save you a lot of time once you get used to it.

Install rsync In Linux

For Debian or Ubuntu based Linux distributions –

sudo apt-get install rsync

For Fedora based Linux distributions –

sudo dnf install rsync

Syntax To Sync Folder Between Network Computers

Syntax

rsync [optionalParameters] source destination

With rsync user can synchronize two folders existing over network computers. To transfer/sync folder from local to the remote host, use the following command – Sync folder using rsync

rsync -ar /home/savan/backup/ savan.patel@192.168.1.102:/home/savan.patel/backup/

​[a and r params preserve timestamp, owner, links, etc and recursively syncs the files respectively].​List of optional parameters are:

-z: compress data.
-a: preserve links, owner, timestamp, etc.
-r: recursive.
–progress: show progress while transferring.
–include and — exclude: include or exclude pattern or files. Example

rsync -ar /home/savan/backup/ --include 'S*' --exclude '*' savan.patel@192.168.1.102:/home/savan.patel/backup/


only rsync files/folders starting with S.

— delete: delete the directory if it doesn’t exist at source but exists at the destination.
–max-size : max transfer size limit. [eg: –max-size=’2000k’]
— remove-source-files: remove files at source after sync
— bwlimit: limit bandwidth for transfer [–bwlimit=1000] (limit by 1000 bps)
If you are unsure of rsync command result, you can also do a dry run with the command : –dry-run.

Conclusion

Both Linux utilities are useful for sharing files between computers on a network. Because it transfers only differences and compressed data by default, rsync is more efficient. That’s all I’ve got for now. More information can be found on man pages. Please leave any suggestions, comments, or questions in the section below.

Exit mobile version