How to set up an NFS server on Ubuntu Server 22.04

[ad_1]

Jack Wallen walks you through the process of creating an NFS share that can be accessed across your LAN.

Macro Shot: IT Administrator Plugs in RJ45 Internet Connector into LAN Router Switch. Information Communication Network in Data Center with Cables Connected to Modem Ports with Blinking Lights
Image: Gorodenkoff/Adobe Stock

Network File System has been around for a very long time and makes for a simple-to-use directory sharing system within your LAN. With this in place, users gain access to remote data as though it was on their local system.

One of the primary reasons why you might choose NFS over Samba is because NFS is much faster and more reliable when dealing with small- to mid-sized files. SMB offers better performance for larger files. In the end, a combination of the two would cover pretty much all of your basis for directory access across a LAN.

I’m going to walk you through the steps of setting up an NFS server on Ubuntu Server 22.04 and then show you how to mount that shared directory on another Ubuntu-based system.

SEE: 40+ open source and Linux terms you need to know (TechRepublic Premium)

What you’ll need

To get this to work, you’ll need a running instance of Ubuntu Server 22.04 and another Ubuntu-based system to serve as a client. You’ll also need a user with sudo privileges.

Ready to work? Let’s go.

How to install the necessary software

Log into your server and install the necessary NFS package with the command:

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

When the installation completes, start and enable the service with:

sudo systemctl enable --now nfs-server

How to create the shared directory

Still on the server, let’s create a shared directory, named data, in /srv with the command:

sudo mkdir -p /srv/data

Change the ownership and permissions of the new directory with the following two commands:

sudo chown -R nobody:nogroup /srv/data
sudo chmod 777 /srv/data

How to configure the exports file

We now need to make NFS aware of the shared directory. Open the configuration file with:

sudo nano /etc/exports

At the bottom of the file, add the following:

/srv/data CLIENT_IP(rw,sync,no_subtree_check)

Where CLIENT_IP is the IP address of the client that will mount the share. If you want to add an entire subnet, you could add something like this:

/srv/data/ 192.168.1.0/24(rw,sync,no_subtree_check)

Apply the new configuration with:

sudo exportfs -a

How to open the firewall

We’ll now open our firewall to allow NFS connections to come through. This can be done in one of two ways. First, by IP address, like so:

sudo ufw allow from IP to any port nfs

Where IP is the IP address you want to allow through.

The second method is by subnet and is handled like this:

sudo ufw allow from SUBNET to any port nfs

Where SUBNET is a range of addresses in the form 192.168.1.0/24.

Reload the firewall with:

sudo ufw reload

How to install the NFS client

It’s now time to install the NFS client tool on your desktop. For that, issue the command:

sudo apt-get install nfs-common -y

How to create a mount directory

On the desktop machine, we’ll add a directory that will be used to mount the remote share. Let’s create the data directory within /mnt using the command:

sudo mkdir -p /mnt/data

How to mount the NFS share

On your desktop, mount the remote NFS share to the new mount directory with:

sudo mount SERVER:/srv/data /mnt/data

Where SERVER is the IP address of the NFS server.

You should now have access to the remote /srv/data directory through /mnt/data. Any file that is added to that directory will be available to the client machine.

How to enable auto-mounting of the share

You certainly don’t want to have to remember to manually mount the share every time you need to connect. Fortunately, it’s quite easy to set it up for automount. To do that, open the fstab file for editing with:

sudo nano /etc/fstab

Add the following line at the bottom of the file:

SERVER:/srv/data /mnt/data nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0

Where SERVER is the IP address of the NFS server.

Verify the mount with:

sudo mount -a

You should receive no feedback from the above command, indicating all is well.

And that’s all there is to set up an NFS server for easy directory access across your LAN.

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

[ad_2]

Source link