How to deploy an NFS server in your data center for easy file sync

Jack Wallen shows you how to use NFS to connect directories on your Linux data center servers for easy file syncing.

Diverse IT colleagues setting server hardware

Image: EvgeniyShkolenko/Getty Images/iStockphoto

Data must be shared. This is especially so in a busy company, where employees are constantly working with data and files. When this is the case, you have to make sure the data and files are available to anyone who needs them. For that, you might don several hats to try and get everything to everyone.

Or you could turn to those Linux servers in your data center. With the help of NFS, you could sync those directories from server to server or server to desktop, with ease. In just a few quick minutes you can get this done.

Let me show you how.

SEE: From start to finish: How to deploy an LDAP server (TechRepublic Premium)

What you'll need

I'm going to demonstrate how to set up NFS between two instances of Ubuntu Server 20.04. You can alter this setup to match any distribution or even from servers to desktops. For this demonstration, you'll need two Ubuntu Server installations and a user with sudo access.

My setup will be:

  • Server: 192.168.1.67
  • Client: 192.168.1.60

How to install NFS

What we're going to be using for this is the NFS Kernel Server, employing V4 of the protocol, which is the default in this case. We need to install the necessary package on the server. Log in to the server and issue the command:

sudo apt-get install nfs-kernel-server -y

How to create and mount the directories

Let's create a directory, named data, on the root of the drive with the command:

sudo mkdir /data

You'll need to change the permissions or ownership of the directory accordingly, so those who need to add data to that directory can.

Once you've created that directory, we're going to mount it to a location that will be known by NFS. Create the mount point with the command:

sudo mkdir -p /srv/nfs/data

Mount the /data directory to the new mount point with the command:

sudo mount --bind /data /srv/nsf/data

We'll now make the mount permanent by adding an entry to fstab. Open fstab for editing with the command:

sudo nano /etc/fstab

Add the following entry at the bottom:

/data /srv/nfs/data none bind 0 0

Next, we need to add entries to the exports file, so NFS knows which filesystems can be accessed. Open the file with the command:

sudo nano /etc/exports

Add the following two lines at the bottom of that file:

/srv/nfs 192.168.1.0/24(rw,sync,no_subtree_check,crossmnt,fsid=0)
/srv/nfs/data 192.168.1.0/24(rw,sync,no_subtree_check)

Make sure to alter the IP address scheme accordingly.

Save and close the file.

Export the shares with the command:

sudo exportfs -ar

How to open the firewall

You'll need to open your firewall, to allow NFS calls to pass through. On Ubuntu, that can be achieved with the command:

sudo ufw allow from 192.168.1.0/24 to any port nfs

Again, make sure to alter the IP address scheme to match your needs.

How to install and configure the NFS client

We now move over to the Client machine. Install the necessary software with the command:

sudo apt-get install nfs-common -y

Note: On Red Hat-based systems, that package is called nfs-utils.

Create a new directory with the command:

sudo mkdir /data

It might seem a bit confusing, but we're keeping the names the same across servers for consistency. You can, of course, change these directory names to anything you like.

We'll now mount the NFS share from Server to the /data directory on Client with the command:

sudo mount -t nfs -o vers=4 192.168.1.67:/data /data

The directories will mount successfully. Before we make the mount permanent, let's test it. Head over to Server and create a test file with the command:

sudo touch /data/test

Go back to Client and issue the command:

ls /data

You should see the newly-created test file listed. 

Now, let's make that NFS mount permanent. On Client, issue the command:

sudo nano /etc/fstab

Add the following line:

192.168.1.67:/data /data nfs defaults,timeo=900,retrans=5,_netdev

Save and close the file. Should your client (or server) reboot, the NFS mount should now happen automatically.

Congratulations, you've just set up an NFS server in your data center. Adapt this to any setup you need (from server to server or server to client), so your Linux machines are sharing data with ease.

Subscribe to TechRepublic's How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Also see

By Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic, The New Stack, and Linux New Media. He's covered a variety of topics for over twenty years and is an avid promoter of open source. For more news about Jack Wallen, visit his website jackwallen....