How to Install and Configure a Linux Ubuntu NFS Server

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

If you are looking to install a Linux Ubuntu server and configure it as a Network File System (NFS) Server, then this guide will help you.

The “ubuntu install nfs server” is a tutorial on installing and configuring a Linux Ubuntu NFS Server.

How to Install and Configure a Linux Ubuntu NFS Server

Are you seeking for a program that lets you share files and folders over a network? For a number of clients? NFS (Network File System) is most likely required! An Ubuntu NFS server enables you to establish shared folders with extra access control for many clients.

You’ll learn how to set up an NFS server and securely exchange data across many devices in this article.

Ready? Continue reading to begin your safe file sharing!

Prerequisites

This will be a hands-on presentation in this course. If you want to follow along, make sure you have the following items:

  • On the same network, a Linux server and client — This demonstration utilizes an Ubuntu 20.04 server with the nfs-server hostname and an Ubuntu 20.04 client with the client hostname. You may, however, use a different Linux distribution like Debian, CentOS, or Fedora.

Related: [Step-by-Step] How to Install Ubuntu 20.04

  • A user with sudo rights who isn’t root.

How to Setup an Ubuntu NFS Server

NFS provides users with secure file access to distant systems as if they were local files. However, before you can share files and directories, you must first set up an NFS server. On the nfs-server system, install the nfs-kernel-server package from Ubuntu.

Throughout the lesson, certain commands do not have outputs. However, at the end of each segment, the result of all of your previous actions is revealed.

1. Log in to your Linux server and use the sudo su command to elevate your privileges to root.

2. Run the instructions below to update your repository’s package index and install the NFS server package (nfs-kernel-server).

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

3. Finally, use the systemctl instructions below to start and activate the NFS service nfs-kernel-server, and then double-check that the nfs-kernel-server service is functioning.

# Enable and start the NFS service nfs-kernel-server.service sudo systemctl enable —now sudo systemctl status nfs-kernel-server.service # check the status of the NFS service

Controlling Systemd services using Ubuntu systemctl is a related topic.

The active (exited) state shows that the NFS service is up and functioning, as seen below. The exited message indicates that the systemd management was unable to locate a daemon to monitor.

The NFS Service is started, enabled, and verified.The NFS Service is started, enabled, and verified.

Configuring the NFS Shared Directory

You’ll now set up the NFS shared directory by creating new folders after installing the NFS server software. You’ll share these folders with clients and update the /etc/exports file with updated settings.

To establish a new NFS shared directory, use the command below. Clients will have access to the /srv/data and /srv/test directories in this case.

mkdir -p /srv/data,test sudo

2. Next, execute the chown command to give the user nobody and the group nogroup ownership of the NFS shared directories /srv/data and /srv/test. This command assures that clients may write to the NFS shared directory. /srv/data,test sudo chown -R nobody:nogroup

This command assures that clients may write to the NFS shared directory.

/srv/data,test sudo chown -R nobody:nogroup

3. Using your chosen editor, update the NFS configuration /etc/exports and add the following settings to the file.

The setup below enables client computers to access the NFS shared directory with read and write (rw) or read-only (ro) user access (ro). Make careful to replace the IP addresses (172.16.1.25 and 172.16.1.0/24) with the IP addresses of the client computers.

# sync – requires NFS to instantly write changes from the shared directory to disk. # no subtree check – to improve efficiency, disable subtree checking. # rw – Give clients read/write access to the NFS shared directory. # Share read and write access to /srv/data with client 172.16.1.25. 172.16.1.25(rw,sync,no subtree check,root squash) # r0 – Give clients read-only access to the NFS shared directory. /srv/test 172.16.1.0/24(ro,sync,no subtree check) /srv/test 172.16.1.0/24(ro,sync,no subtree check) /srv/test 172.16.1.0/24(ro,sync,no subtree check) /srv/test 172.16.1.0/

Setting up an NFS Shared DirectorySetting up an NFS Shared Directory

4. To apply the shared directory and validate the /etc/exports configuration file, use the command below. If no output messages appear, the NFS setup is correct.

5. Restart the nfs-kernel-server service using the systemctl command below.

sudo systemctl nfs-kernel-server restart

6. Finally, execute the showmount command to see a list of the NFS server’s shared folders. Make careful to replace the IP address (172.16.1.20) with the IP address of the NFS server.

172.16.1.20 —exports showmount

The shared directory list on the NFS server should now show the IP address that permitted you to access the NFS server.

Using NFS Server to check the list of shared directoriesUsing NFS Server to check the list of shared directories

Using UFW Firewall to Secure NFS Shared Directory

You’ve set up the NFS shared directory, but you’ll probably want to prevent unauthorized users from accessing it.

Make sure that only the IP addresses that have been given permission may access the shared directory. How? By configuring the UFW firewall and defining the NFS port’s IP address.

How to Configure the UFW Firewall on Linux

1. Run the following instructions to open the NFS port (2049) and provide access to the NFS server to clients with the IP addresses 172.16.1.25 and 172.16.1.0/24. Make careful to update the IP address with the IP address of your client.

# sudo ufw allow from 172.16.1.25 to any port nfs # allow client IP 172.16.1.25 to access NFS server # Allow network 172.16.1.0/24 to connect to NFS server sudo ufw allow any port nfs from 172.16.1.0/24

Configuring Firewall RulesConfiguring Firewall Rules

2. Then, to reload the UFW firewall and apply a new configuration, enter the ufw command below.

3. Finally, use the ufw command to check the UFW firewall’s status.

The NFS server port 2049 has been added to the firewall and is only accessible from the client IP address 172.16.1.25 and network 172.16.1.0/24, as shown below.

Checking the status of the UFW firewallChecking the status of the UFW firewall

NFS Shared Directory Mounting on a Client Machine

You’ve finished configuring the NFS server at this point. However, how can client workstations get access to the NFS shared directory? The mount command will allow you to access the NFS shared directory. However, you must first install nfs-common (for Debian Linux) or nfs-utils (for RHEL, CentOS) on the client system.

1. Log in to your client system, open a terminal window, and type sudo su to get root access.

2. Next, depending on your OS, use one of the instructions below to install NFS packages on the client system.

# sudo apt install nfs-common -y for Ubuntu/Debian-based distributions # sudo dnf install nfs-utils -y for RHEL/CentOS/Fedora Linux distributions

3. To create the target mount directory, use the command below. In this example, the NFS shared directory will be mounted on the client machine’s /data and /test folders.

4. To access the NFS shared directory, run each of the mount instructions listed below.

The -t nfs option indicates that the file system type is NFS server. This command mounts the NFS server’s /srv/data shared directory to the local client directory (/data). And the shared directory /srv/test to the local directory /test.

# mount the nfs server 172.16.1.20’s shared directory /srv/data to the local client directory /data sudo mount -t nfs 172.16.1.20:/srv/data /data # mount the nfs server 172.16.1.20’s shared directory /srv/test to the local client directory /test sudo mount -t nfs 172.16.1.20:/srv/test /test

5. Verify the list of mounted file systems on the client computer using the df command.

The NFS shared directory /srv/data is mounted on the client machine’s /data directory, as seen in the output below. The shared directory (/srv/test) is mounted to the local directory (/test) on the client system at the same time.

Examining the mounted file Examining the mounted file

6. Next, browse to the destination mount directory (/data) and create a file named file.txt using the instructions below.

You may create a new file since the shared directory (/srv/data) has read and write permissions.

# navigate to /data directory cd /data # create a new file with “This file from client machine!” as content echo “This file from client machine!” > file.txt

7. Verify that the file is accessible on the mount directory /data using the ls and cat commands below.

# display the content of file.txt cat file.txt # list files in the current directory ls -lah

Access to the NFS shared directory is being tested for read and write access.Access to the NFS shared directory is being tested for read and write access.

8. Finally, use the commands below to access to the mount directory (/test) and generate file.txt. On a read-only filesystem, you may use this command to test write access limitations.

# navigate to /test directory cd /test # create new file echo “Test create a file on Read-only filesystem” > file.txt

Because the shared directory (/srv/test) only has read-only access, you’ll receive an error message like the one below.

On a read-only filesystem, we're testing write access.On a read-only filesystem, we’re testing write access.

Permanently mounting the NFS Shared Directory

You’ve now successfully mounted an NFS shared directory on the client computer, however it will vanish if the system is restarted. How do I make a persistent mount of an NFS shared directory? Using the /etc/fstab file to define an NFS shared directory.

1. Using your favourite editor, add the following settings to the /etc/fstab configuration file. Replace the IP address, shared directory, and mount target directory with the IP address of the NFS server.

# rw/ro – the destination mount may be read-write or read-only. # hard – make the client computer attempt to connect to the NFS every time it fails. # noexec – prevents binary files from being executed on the file system. # rsize – the maximum number of READ requests that the client machine is capable of handling. # wzise – the maximum number of WRITE requests that the client machine is capable of handling. # timeo – timeout for NFS client before retrying NFS server requests # NFS Shared Directory – dump – pass – target – type – options /data nfs rw,hard,noexec,rsize=8192,wsize=8192,timeo=14 0 0 172.16.1.20:/srv/data /data nfs rw,hard,noexec,rsize=8192,wsize=8192,timeo=14 0 0 /test nfs ro,hard,noexec,rsize=8192,wsize=8192,timeo=14 0 0 172.16.1.20:/srv/test /test nfs ro,hard,noexec,rsize=8192,wsize=8192,timeo=14 0 0

Mounting NFS Shared Directory Using the /etc/fstab FileMounting NFS Shared Directory Using the /etc/fstab File

2. Next, use the mount command to mount all filesystems on the /etc/fstab file and check the settings. The setting is correct if there is no error message.

3. Verify the mounted file system on the client computer using the df command.

The NFS shared directory should be immediately mounted on your client system, as seen in the output below.

Permanently mounting the NFS Shared Directory using /etc/fstab ConfigurationPermanently mounting the NFS Shared Directory using /etc/fstab Configuration

4. Finally, restart the client PC using the reboot command. Log in and perform the df command to validate the NFS shared directory as you did in step three.

The NFS shared directory is still permanently mounted on the client system, as seen below.

Verifying that the NFS Shared Directory is mounted on the client machine permanentlyVerifying that the NFS Shared Directory is mounted on the client machine permanently

Conclusion

You’ve learnt how to set up an NFS server on a Linux server to safely share a directory during this article. You’ve also mentioned mounting the NFS shared directory permanently, so you won’t have to manually mount it every time you reset your system.

You should now have a fully working NFS server. So, where do we go from here? Perhaps learn how to use Kerberos authentication to add a layer of security to NFS?

The “nfs-kernel-server” is a Linux Ubuntu server that allows users to mount and share data over the network. The installation process for this tool is fairly easy, but it can take some time to configure properly.

Related Tags

  • ubuntu restart nfs server
  • ubuntu nfs mount
  • ubuntu mount nfs version 3
  • linux nfs server
  • setup nfs server centos 7