Linux to Linux: File Transfer Between Two Linux Systems

Linux to Linux file transfer

Secure file transfer between Linux systems is a routine part of system administration. Whether you’re deploying code to a production server, syncing configuration files across nodes, or pushing backups to a remote host, SSH-based tools are the standard choice. They provide encryption and authentication by default.

In this guide, we’ll transfer files between two Linux machines on the same network. Machine A (IP: 192.168.10.10, user: linuxuser) will act as the source or sender, while Machine B (IP: 192.168.10.8, user: ubuntu) will serve as the destination or receiver. We’ll demonstrate how to copy files in both directions using efficient command-line tools.

Method 1: Transfer Files via rsync

rsync (remote synchronization) is a command-line tool used on Unix-like systems to copy and sync files or folders between two machines. One machine acts as the source (local host), and the other is the destination (remote host). rsync uses SSH for secure communication and supports synchronization, compression, and differential updates.

Step 1: Install Rsync

Run the following command to install rsync on both systems:

sudo apt install rsync -y

Step 2: Check SSH Access

Since rsync uses SSH to transfer files, make sure each machine can SSH into the other. From Machine B (192.168.10.8), test SSH to Machine A using the following command:

ssh [email protected]
verify ssh access on machine A

Now, from Machine A (192.168.10.10), test SSH to Machine B:

ssh [email protected]
verify ssh access on machine B

If you’re prompted for a password and successfully log in, SSH is working.

To avoid typing your password every time, generate a key pair and copy it to the remote machine. Run the following command on the machine initiating the transfer (Press Enter through all prompts to use defaults.):

ssh-keygen -t rsa
set up ssh key authentication

Now, copy your public key to the destination machine:

ssh-copy-id [email protected]
copy public key to destination

Now, you can connect without typing a password.

Step 4: Transfer a File from Machine B to Machine A

Use rsync to copy a file from Machine B to Machine A:

rsync -avz /home/ubuntu/file.txt [email protected]:/home/linuxuser

Here, -a preserves file attributes, -v shows progress, and -z compresses data to speed up the transfer:

transfer file from machine b to a using rsync

Step 5: Transfer a Directory from Machine A to Machine B

To copy a folder from Machine A to Machine B, use the following command:

rsync -avz /home/linuxuser/docs/ [email protected]:/home/ubuntu/

Here, the trailing / ensures only the contents are copied, not the folder itself:

transfer directory from machine b to a using rsync

Step 6: Transfer in the Opposite Direction

If you want to copy from Machine A to Machine B, just reverse the source and destination in the command:

rsync -avz [email protected]:/home/ubuntu/file.txt /home/linuxuser
transfer files in opposite direction using rsync

Method 2: Transfer Files via FTP

FTP (File Transfer Protocol) is a common way to move files between computers over a TCP network, but it doesn’t use encryption by default. To transfer files, one system must act as the FTP server (using software like vsftpd), while the other connects to it as a client. Below are the steps to perform a simple FTP file transfer between two Linux machines.

Step 1: Install FTP Server on One Machine

Install vsftpd on the system acting as the server (e.g., Machine B):

sudo apt install vsftpd
install ftp server

Step 2: Allow FTP Traffic Through Firewall

Open port 21 so the server can receive FTP connections.

sudo ufw allow 21/tcp
open port 21

Step 3: Create a Test File

Let’s create a file to test the upload:

echo "FTP test file" > /home/linuxuser/Documents/ftp-test.txt
create sample file

Step 4: Connect to the FTP Server from the Client

From the client machine, connect using the following command:

ftp 192.168.10.8
connect to ftp

Enter the username and password when prompted.

Step 5: Upload a File to the FTP Server

Once connected, upload the file by executing the following command:

put /home/linuxuser/Documents/ftp-test.txt ftp-test.txt
upload file to ftp

Step 6: Download a File from the FTP Server

Use the following command to download the file from the FTP server:

get ftp-test.txt
download file from ftp

Method 3: File Transfer via Samba

Samba is mainly used to share files between Linux and Windows using the SMB/CIFS protocol. But in a Linux-to-Linux setup, it helps create shared folders that multiple systems can access with the right permissions. This is useful for teamwork or when systems need continuous access to shared files. The best way to transfer files with Samba is by mounting the shared folder using the “mount -t cifs” command. The steps below explain how to set up and use this method for file transfer.

Step 1: Install Samba on Both Machines

Run the following command on both machines (A and B) to install Samba:

sudo apt install samba smbclient cifs-utils -y

Step 2: Set Up a Shared Folder on Machine A

Create a Shared Folder on Machine A using the mkdir command:

mkdir -p /home/linuxuser/shared

Then, give full permissions so other machines can read/write:

chmod 777 /home/linuxuser/shared
create folder and change permissions

Now edit the Samba’s configuration file:

sudo nano /etc/samba/smb.conf

Paste the following code at the bottom of the file:

[SharedFolder]
path = /home/linuxuser/shared
available = yes
valid users = linuxuser
read only = no
browsable = yes
public = yes
writable = yes
edit samba conf file

Set a password for the Samba user:

sudo smbpasswd -a linuxuser
set password

Now, restart the Samba service:

sudo systemctl restart smbd
restart samba

Step 3: Access the Samba Share from Machine B

On Machine B (192.168.10.8), use smbclient to test:

smbclient //192.168.10.10/SharedFolder -U linuxuser
access samba

Enter the Samba password you set earlier. If it works, you’ll get an smb: prompt, where you can use commands like ls, get, and put to manage files.

For example, you can use the put command to upload a File to the FTP Server:

put /home/ubuntu/Documents/smb-test.txt smb-test.txt
upload file to ftp server

Similarly, to download a File from the FTP Server, you can use the get command:

get smb-test.txt
download file ftp server

Step 4: Mount the Samba Share on Machine B

To access the Samba shared folder as if it’s part of the local file system on Machine B, first create a directory to serve as the mount point:

mkdir -p /home/ubuntu/mountA
create a folder to share

Next, mount the shared folder from Machine A using the following command:

sudo mount -t cifs //192.168.10.10/SharedFolder /home/ubuntu/mountA -o username=linuxuser,password=<SambaPassword>
mount the shared folder

Once mounted, you can check if the folder is accessible by listing its contents:

ls /home/ubuntu/mountA
list folder content to verify it is accessible

Step 5: Transfer Files

To send a file from Machine B to the shared folder on Machine A, use the cp command:

sudo cp /home/ubuntu/smb-test.txt /home/ubuntu/mountA/
send file

This file will now be available in /home/linuxuser/shared on Machine A. Use the cd command to access it:

access shared file

To copy files from Machine A to Machine B, simply pull them from the mounted directory into your local folders, like this:

cp /home/ubuntu/mountA/filename.txt /home/ubuntu/

Method 4: Transfer Files via scp (Secure Copy)

The scp command lets you copy files between two systems using SSH. It works much like the cp command, but instead of copying files within your local machine, it securely transfers them over the network. Both the data and login details are encrypted during the process. This makes scp a good choice for quick and straightforward file transfers when you don’t need a more advanced syncing tool like rsync.

Step 1: Install OpenSSH (if not already installed)

To use scp, SSH must be installed and running on both machines. You can install it using the following commands:

sudo apt update && sudo apt install openssh-server -y

Step 2: Enable and start SSH service

After installation, enable and start the SSH service on both machines so it starts automatically on boot:

sudo systemctl enable --now ssh && sudo systemctl status ssh

You should see it as active (running).

Step 3: Verify SSH Connectivity

Before transferring files, test the SSH connection between the machines. From Machine A to Machine B:

ssh [email protected]
verify ssh connectivity

From Machine B to Machine A:

ssh [email protected]
connecting to machine A

If you’re prompted to enter a password and you successfully log in, then SSH is working properly.

Step 4: Transfer the File from Machine A to Machine B

To copy a file from Machine A to Machine B, use the following command on Machine A:

scp /path/to/localfile.txt [email protected]:/home/ubuntu/

Replace /path/to/localfile with actual file path:

scp /home/linuxuser/localfile.txt [email protected]:/home/ubuntu/

This sends the file localfile.txt to the /home/ubuntu/ directory on Machine B:

transfer file from machine A to machine B

Step 5: Transfer the File from Machine B to Machine A

To copy a file from Machine B to Machine A, run this command on Machine B:

scp /path/to/localfile.txt [email protected]:/home/linuxuser/

For example, the following command places the file into the /home/linuxuser/ directory on Machine A:

scp /home/ubuntu/text.txt [email protected]:/home/linuxuser/
transfer file from machine B to machine A

Step 6: Transfer an Entire Directory

If you want to copy a full folder, use the -r flag with scp to transfer it recursively. For instance, to transfer a directory from Machine A to Machine B, run the command:

scp -r /home/linuxuser/project [email protected]:/home/ubuntu/
transfer directory from machine A to machine B

Similarly, the following command copies all the contents of the directory, including subfolders and files, from Machine B to Machine A:

scp -r /home/ubuntu/docs [email protected]:/home/linuxuser/
copy content from machine B to machine A

Method 5: Transfer files via SFTP

SFTP (Secure File Transfer Protocol) is a safer version of FTP that works through SSH. It encrypts both the files and the commands sent during transfer, keeping the data private and secure. So, if SSH is enabled on your Linux systems, SFTP is a reliable way to move files between them.

Step 1: Ensure OpenSSH Server is Installed on Both Machines

Before using SFTP, make sure the SSH server is installed and running on the system you want to connect to (Machine B):

sudo apt update && sudo apt install openssh-server -y

Step 2: Allow SFTP Port (22) in UFW Firewall (if enabled)

SFTP uses port 22 by default, which must be open for connections. Run this command to allow traffic:

sudo ufw allow 22/tcp

Step 3: Start an SFTP Session

From Machine A, initiate a connection to Machine B using the sftp command. Replace ubuntu with the username of Machine B:

sftp [email protected]

Enter the password for the user ubuntu when prompted:

start an ftp session

Step 4: Upload or Download Files and Folders

Once connected, you’ll be inside an interactive SFTP session where you can send or receive files. Inside the SFTP prompt, use the following command to upload a file from Machine A to Machine B:

put /home/linuxuser/file.txt
upload files and folders

Similarly, you can use the get command to download the files and folders:

get /home/ubuntu/file.txt

Method 6: Use Python HTTP Server (Quick & Easy)

If you’re looking for a fast way to share files between two Linux systems without setting up SSH or additional tools, Python’s built-in HTTP server is a great option. This method is especially useful for temporary or one-time transfers over a local network.

Step 1: Start a Temporary File Server on the Sending Machine

Open a terminal on the machine that has the file you want to share. To do this, navigate to the folder containing the file(s) you want to transfer:

cd /path/to/files
start a temporary file server

Then start a simple HTTP server on port 8000 using Python:

python3 -m http.server 8000

This command launches a temporary web server that serves all files in the current directory:

start a simple http server

Step 2: Download the File on the Receiving Machine (Machine B)

You can now access and download the file from Machine B using either of the following methods. Open any browser and go to the following URL to access the directory content:

http://192.168.100.86:8000/
download file on receiving machine

Just click the file you want to download.

Alternatively, if you prefer the command line, you can access it using the following command:

wget http://192.168.100.86:8000/web-share-sample.txt
download file on receiving machine

If you want to send a file from Machine B to Machine A, simply switch the roles. On Machine B, run the command below:

python3 -m http.server 8000
send file using python http

Then, on Machine A, either open a browser and go to “http://192.168.100.88:8000/”, or download the file directly using the following command:

wget http://192.168.100.88:8000/file.txt
download file using wget

That’s all about transferring a file between two Linux systems.

Conclusion

Transferring files between Linux systems is a common task in both administration and development. In this guide, we explored several effective methods, including rsync for secure synchronization, FTP and Samba for network sharing, scp and SFTP for SSH-based transfers, and Python’s HTTP server for quick one-time sharing. Each method fits different needs, from regular backups to temporary file sharing. You can choose the method that best fits your needs to ensure smooth, secure, and efficient file transfers between your Linux machines.

Transferring files between two Linux systems becomes seamless with the right tools and environment. Ultahost’s affordable VPS hosting plans provide full root access, NVMe SSD storage, and a secure platform to help you move data efficiently between servers using SFTP, SCP, rsync, and more.

FAQ

Which is the most secure method to transfer files between Linux machines?
Can I use rsync to copy entire directories between machines?
What’s the difference between scp and rsync?
Is FTP a secure method for transferring files?
How can I check if SSH is working between two Linux systems?
What port should I open for SSH-based file transfers like rsync, scp, or sftp?
Can I access a shared folder from another Linux system using Samba?

Related Post

What Is the .bashrc File in Linux?

The .bashrc file is a shell script that runs every time...

How to Use apropos Command in Linux

The apropos command in Linux is an essential tool for b...

How to Use date command in Linux

The date command in Linux is a powerful tool for displa...

How to Extract .tar.gz Files in Linux

The "tar.gz" file format is a common archive format use...

How to Use the Linux Head Command

File handling is a day-to-day task of Linux users and a...

Exploring chsh Command in Linux with Examples

The chsh command a utility commonly found in Linux dist...

Leave a Comment