How to Stream Files Between Two Ubuntu Machines

Updated: January 29, 2024 By: Guest Contributor Post a comment

Introduction

Streaming files between two Ubuntu machines can be a crucial task for system administrators, developers, and even general users who want to share files without the need for physical storage devices or cloud services. This comprehensive guide will delve into seamless methods for streaming files directly between two Linux-based systems.

Prerequisites

Before getting started, make sure you have the following preparation:

  • Two Ubuntu machines connected to the same network
  • OpenSSH server installed on the host machine
  • Basic knowledge of the command line and networking concepts

Setting Up SSH

Secure Shell (SSH) is a network protocol that allows secure access to another computer over a network. It is the backbone of our file streaming setup.

Install OpenSSH:

sudo apt update && sudo apt install openssh-server

Once installed, verify the SSH server status:

sudo systemctl status ssh

If it is not running, start it with:

sudo systemctl start ssh

SSH Key Authentication

To streamline the authentication process, use SSH keys. Generate a new SSH key pair on the client machine:

ssh-keygen -t rsa -b 4096

Then, transfer the public key to the host, ensuring password-less login:

ssh-copy-id user@hostmachineIP

Streaming Files Using SCP

The secure copy protocol (SCP) allows files to be copied to, from, or between different machines.

Copy a file from the client to the host:

scp /path/to/source/file user@hostmachineIP:/path/to/destination

To copy a directory and its contents, use the recursive option:

scp -r /path/to/source/directory user@hostmachineIP:/path/to/destination

Streaming Using rsync

The rsync command is a more flexible tool for file streaming, allowing synchronization of files and directories.

Example of streaming files with rsync:

rsync -avz /path/to/source/ user@hostmachineIP:/path/to/destination

The -avz flags archive (preserves permissions), verbose (shows progress), and compress (compresses data during transfer) the files, respectively.

NFS Mounts

Network File System (NFS) is a distributed filesystem protocol that allows a user on a client machine to access files over a network.

On the host, install NFS kernel server:

sudo apt install nfs-kernel-server

Edit /etc/exports to share the directory:

/path/to/shared/directory clientmachineIP(rw,sync,no_subtree_check)

Apply the changes:

sudo exportfs -a

Using Netcat for Large Files

Netcat can be used to send any size of file over the network directly. On the receiving machine, use the following command to listen for the file:

nc -l -p 12345 > destination_file

On the sending machine:

nc hostmachineIP 12345 < source_file

Conclusion

This guide provided solutions to stream files between two Ubuntu machines using different tools. Each method has its use-case; whether confidentiality, transfer speed, or handiness is the priority. Make sure to choose the one that fits your needs closely.