Goodbye SCP! Real-Time File Sharing Between Linux Machines: An NFS Practical Guide

1. Introduction: Why is NFS the “Preferred Solution” for Cross-Machine Sharing in Linux?

In daily work, have you encountered these issues: transferring large files with SCP takes a long time, frequent logins with FTP are troublesome, and multiple Linux servers need to share configuration files but struggle to synchronize? In fact, these problems can be easily solved by NFS (Network File System).

NFS, as a network sharing protocol natively supported by Linux systems, has the greatest advantage of “transparent mounting”—it allows the file directories of remote servers to be accessed like local disks, with read and write speeds close to local operations, and it can support multiple clients connecting simultaneously. Whether managing a server cluster or collaborating across devices as a developer, NFS can significantly reduce the complexity of file sharing, making it a “productivity tool” in the Linux environment.

2. Core Principles of NFS: Understand the “Sharing – Mounting” Logic in 3 Minutes

Many people find technical protocols complex, but the working principle of NFS is actually quite simple, consisting of two main logics: “server sharing + client mounting”.

  • Server: Responsible for “opening up” a specified directory and setting access permissions (such as read-only/read-write, which IPs are allowed to access), essentially acting as a “file sharing server”;
  • Client: Connects to the server over the network and “maps” the shared directory to a local folder (called a “mount point”). After that, operating this local folder is equivalent to directly operating the server’s files, without the need for manual transfer.

For example, the server is like a supermarket, displaying products (files) and opening the entrance; the client is like a customer who does not need to take the products home but can directly select and use them in the supermarket, and all customers see a real-time updated inventory of products—this is the core advantage of NFS’s “real-time synchronization”.

How to implement it?

(1) Environment Preparation

  • Two or more Linux hosts (CentOS 7/8, Ubuntu 20.04/22.04 are both acceptable);
  • Network connectivity between hosts (use ping to test connectivity to the server, as long as it can connect);
  • Disable the firewall or open the relevant NFS ports (111, 2049) to avoid permission blocks.

(2) Server Configuration (the party sharing files)

1. Install NFS service

# For CentOS system

yum install nfs-utils -y

2. Create a shared directory:

mkdir -p /data/nfs-share # Create directory (-p ensures parent directory exists)

chmod 755 /data/nfs-share # Open read, write, and execute permissions (adjust as needed in production)

3. Configure access rules:

Edit/etc/exportss file (the core configuration file for NFS), and add the sharing rules:

vi /etc/exports

/data/nfs-share 192.168.1.0/24(rw,sync,no_root_squash)

4. Start the service and set it to start on boot:

systemctl start nfs-server

systemctl enable nfs-server

# Refresh configuration (no need to restart the service after modifying exports)

exportfs -r

(3) Client Configuration (the party accessing shared files)

1. Install NFS client tools:

yum install nfs-utils -y

2. Create a local mount point:

mkdir -p /mnt/nfs-client # Local folder for mapping the server’s shared directory

3. Mount the shared directory:

# Format: mount -t nfs serverIP:sharedDirectory localMountPoint

mount -t nfs 192.168.1.200:/data/nfs-share /mnt/nfs-client

4. Verify success:

df -h # Check mount information, if “192.168.1.200:/data/nfs-share” appears, it is successful

# Test: Create a file on the client, it will sync to the server

touch /mnt/nfs-client/test-nfs.txt

# Log in to the server and execute ls /data/nfs-share to see test-nfs.txt

4. Key Supplement: Set Up Automatic Mounting on Boot

If the client restarts, the mount will fail, and automatic mounting needs to be configured:

Edit/etc/fstabAdd a line:

vi /etc/fstab

192.168.1.200:/data/nfs-share /mnt/nfs-client nfs defaults 0 0

Executemount –aTest, if there are no errors, the configuration is successful, and it will automatically mount after a restart.

If you encounter any issues during the practical operation, feel free to leave a message in the comments for discussion~ If you find this useful, remember to like, bookmark, follow me, and share it with friends who need it!

Leave a Comment