---
title: "How to Install and Configure a Linux Ubuntu NFS Server"
description: "Learn how you can securely share directories by installing and configuring a Linux Ubuntu NFS Server in this step-by-step tutorial!"
canonical: "https://adamtheautomator.com/ubuntu-nfs-server/"
---

# How to Install and Configure a Linux Ubuntu NFS Server

> Learn how you can securely share directories by installing and configuring a Linux Ubuntu NFS Server in this step-by-step tutorial!

Source: https://adamtheautomator.com/ubuntu-nfs-server/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Install and Configure a Linux Ubuntu NFS Server](https://adamtheautomator.com/wp-content/uploads/2022/02/How-to-Install-and-Configure-a-Linux-Ubuntu-NFS-Server.jpg)

# How to Install and Configure a Linux Ubuntu NFS Server

[![](https://secure.gravatar.com/avatar/572a248f516b6d0cd566cb44fdbd9336f30ef95aa0f6d78f118c1f47b1b6d6b7?s=192&d=mm&r=g)Arvid Larson](https://adamtheautomator.com/author/arvid-larson/)16 February 20227 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Linux](/tag/linux/)[NFS](/tag/nfs/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Installing an Ubuntu NFS Server](#installing-an-ubuntu-nfs-server)
*   [Setting up NFS Shared Directory](#setting-up-nfs-shared-directory)
*   [Securing NFS Shared Directory with UFW Firewall](#securing-nfs-shared-directory-with-ufw-firewall)
*   [Mounting NFS Shared Directory on Client Machine](#mounting-nfs-shared-directory-on-client-machine)
*   [Mounting NFS Shared Directory Permanently](#mounting-nfs-shared-directory-permanently)
*   [Conclusion](#conclusion)

Are you looking for an application that allows you to share files and directories over the network? For multiple clients? You probably need NFS (Network File System)! An Ubuntu NFS server allows you to create shared directories for various clients with additional access control.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

In this tutorial, you’ll learn how to set up an NFS server and securely share files between multiple machines.

Ready? Read on and start your secure file sharing!

## Prerequisites

This tutorial will be a hands-on demonstration. To follow along, ensure you have got the following requirements:

*   A Linux server and client on the same network – This demo uses an [Ubuntu 20.04](https://www.releases.ubuntu.com/20.04/) server with hostname as `nfs-server` and an [Ubuntu 20.04](https://adamtheautomator.com/install-ubuntu/) client with hostname as `client`. But, you can use another Linux distribution, such as Debian, CentOS, or Fedora.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   A non-root user with sudo privileges.

## Installing an Ubuntu NFS Server

NFS allows secure file access on remote systems to users like they are just files stored locally. But before sharing files and directories, you’ll first install an Ubuntu NFS server. On Ubuntu, you’ll install the `nfs-kernel-server` package on the `nfs-server` machine.

> __Some commands throughout the tutorial don’t have outputs. But an outcome of the accumulated steps you’ve taken is displayed at each section’s last step.__

1\. Log in to your Linux server, and run the `sudo su` command below to get the root privileges.

```bash
sudo su
```

2\. Run the below commands to `update` the package index for your repository and install the Ubuntu NFS server package (`nfs-kernel-server`).

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

3\. Lastly, run the `systemctl` commands below to start and enable the NFS service `nfs-kernel-server`, then verify the `nfs-kernel-server` service status is active.

```bash
# start and enable NFS service
sudo systemctl enable --now nfs-kernel-server.service

# verify status NFS service
sudo systemctl status nfs-kernel-server.service
```

Related:[Controlling Systemd services with Ubuntu systemctl](https://adamtheautomator.com/ubuntu-systemctl/)

As you see below, the **active (exited)** status indicates the NFS service is running. The **exited** message tells you that the systemd manager cannot find any daemon to monitor.

![Starting, enabling, and verifying the NFS Service](https://adamtheautomator.com/wp-content/uploads/2022/02/image-200.png)

Starting, enabling, and verifying the NFS Service

## Setting up NFS Shared Directory

After installing the NFS server package, you’ll now set up the NFS shared directory by creating new directories. You’ll share these directories with clients and add new configurations to the _/etc/exports_ file.

1\. Run the below command to create a new NFS shared directory. In this example, you’ll share the `/srv/data` directory and `/srv/test` to clients.

```bash
sudo mkdir -p /srv/{data,test}
```

2\. Next, run the [`chown`](https://linuxize.com/post/linux-chown-command/) command below to change the ownership of the NFS shared directory `/srv/data` and `/srv/test` to the user `nobody` and group `nogroup`. This command ensures NFS shared directory is writable by clients. `sudo chown -R nobody:nogroup /srv/{data,test}`

This command ensures NFS shared directory is writable by clients.

```bash
sudo chown -R nobody:nogroup /srv/{data,test}
```

3\. Edit the NFS configuration `/etc/exports` using your preferred editor and populate the file with the following configuration.

The below configuration allows client machines access to the NFS shared directory with specified user access either read and write (`rw`) or read-only (`ro`). Make sure to change the IP addresses (`172.16.1.25` and `172.16.1.0/24`) with the client machines’ IP addresses.

```powershell
# sync - forces NFS to write change from the shared directory to disk immediately.
# no_subtree_check - disabling the subtree checking to get more performance.

# rw - set read/write access to NFS shared directory for clients.
# share /srv/data to client 172.16.1.25 with read and write access.
/srv/data 172.16.1.25(rw,sync,no_subtree_check,root_squash)

# r0 - set read-only access to NFS shared directory for clients.
# share /srv/test to network 172.16.1.0/24 with read only access.
/srv/test 172.16.1.0/24(ro,sync,no_subtree_check)
```

![Configuring NFS Shared directory](https://adamtheautomator.com/wp-content/uploads/2022/02/image-201.png)

Configuring NFS Shared directory

4\. Run the below command to apply the shared directory, and verify the `/etc/exports` configuration file. If you don’t get any output messages, the NFS configuration is correct.

```bash
sudo exportfs -a
```

5\. Now, run the `systemctl` command below to restart the `nfs-kernel-server` service.

```bash
sudo systemctl restart nfs-kernel-server
```

6\. Lastly, run the [`showmount`](https://linux.die.net/man/8/showmount) command below to list the shared directories on the Ubuntu NFS server. Ensure to change the IP address (`172.16.1.20`) with the NFS server IP address.

```bash
showmount --exports 172.16.1.20
```

Now you should see the shared directory list on the NFS server with the IP address that allowed you to access the Ubuntu NFS server.

![Checking list shared directory on NFS Server](https://adamtheautomator.com/wp-content/uploads/2022/02/image-202.png)

Checking list shared directory on NFS Server

## Securing NFS Shared Directory with UFW Firewall

You’ve configured the NFS shared directory, but typically, you’d want to keep out random users from accessing the shared directory.

Ensure that only the IP address allowed can access the shared directory. How? By setting up the UFW firewall and specifying the IP address to the NFS port.

Related:[How To Set Up the UFW Firewall on Linux](https://adamtheautomator.com/ufw-firewall/)

1\. Run the following commands to open the NFS port (`2049`) and allow clients on the IP addresses `172.16.1.25` and `172.16.1.0/24` access to the Ubuntu NFS server. Make sure to replace the IP address with your client’s IP address.

```bash
# allow client IP 172.16.1.25 to access NFS server
sudo ufw allow from 172.16.1.25 to any port nfs

# allow network 172.16.1.0/24 to access NFS server
sudo ufw allow from 172.16.1.0/24 to any port nfs
```

![Adding Firewall Rules](https://adamtheautomator.com/wp-content/uploads/2022/02/image-203.png)

Adding Firewall Rules

2\. Next, run the `ufw` command below to reload the UFW firewall and apply a new configuration.

```bash
sudo ufw reload
```

3\. Lastly, run the `ufw` command below to verify the status of the UFW firewall.

```bash
sudo ufw status
```

Below, you can see the NFS server port 2049 is added to the firewall and only accessible from the client IP address 172.16.1.25 and network 172.16.1.0/24.

![Checking UFW firewall status](https://adamtheautomator.com/wp-content/uploads/2022/02/image-204.png)

Checking UFW firewall status

## Mounting NFS Shared Directory on Client Machine

At this point, you’ve fully configured the Ubuntu NFS server. But how can client machines access NFS shared directory? Running the [`mount`](https://man7.org/linux/man-pages/man8/mount.8.html) command to access the NFS shared directory will do the trick. But first, you’ll install `nfs-common` (for Debian Linux distribution) or `nfs-utils` (for RHEL, CentOS distribution) to the client machine.

1\. Log in to your client machine, open the terminal, and run the `sudo su` command to get the root privileges.

```bash
sudo su
```

2\. Next, run either of the commands below to install NFS packages on the client machine, depending on your OS.

```bash
# for Ubuntu/Debian based distribution
sudo apt install nfs-common -y

# for RHEL/CentOS/Fedora Linux distribution
sudo dnf install nfs-utils -y
```

3\. Run the below command to create the target mount directory. The NFS shared directory will be mounted on the client machine `/data` and `/test` directories in this example.

```bash
mkdir -p /{data,test}
```

4\. Execute each `mount` commands below to access the NFS shared directory.

The `-t nfs` option specifies the type of file system is NFS server. This command mounts the `/srv/data` shared directory on the Ubuntu NFS server to the local client directory (`/data`). And the `/srv/test` shared directory to the click local directory `/test`.

```bash
# mount shared directory `/srv/data on the nfs server 172.16.1.20
# to the local client directory /data
sudo mount -t nfs 172.16.1.20:/srv/data /data

# mount shared directory `/srv/test on the nfs server 172.16.1.20
# to the local client directory /test
sudo mount -t nfs 172.16.1.20:/srv/test /test
```

5\. Run the `df` command below to verify the list of mounted file systems on the client machine.

```bash
sudo df -h
```

In the output below, you’ll see the NFS shared directory /srv/data is mounted on the /data directory on the client machine. At the same time, the shared directory (/srv/test) is mounted to the local directory (/test) on the client machine.

![Checking mounted file ](https://adamtheautomator.com/wp-content/uploads/2022/02/image-205.png)

Checking mounted file

6\. Next, run the below commands to navigate to the target mount directory (`/data`) and create a file called `file.txt`.

> _Since the shared directory (`/srv/data`) has read and write permissions, you can create a new file._

```bash
# 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\. Run the `ls` and `cat` commands below to verify the file is available on the mount directory `/data`.

```bash
# list files on the current directory
ls -lah

# showing the content of the file.txt
cat file.txt
```

![Testing read and write access to the NFS shared directory](https://adamtheautomator.com/wp-content/uploads/2022/02/image-206.png)

Testing read and write access to the NFS shared directory

8\. Lastly, run each command below to navigate to the mount directory (`/test)` and create a file called `file.txt`. This command lets you test write access restrictions on the read-only filesystem.

```bash
# navigate to /test directory
cd /test

# create new file
echo "Test create a file on Read-only filesystem" > file.txt
```

Like the output below, you’ll get an error message because the shared directory (/srv/test) only has read-only permission.

![Testing Write Access on the Read-only filesystem](https://adamtheautomator.com/wp-content/uploads/2022/02/image-207.png)

Testing Write Access on the Read-only filesystem

## Mounting NFS Shared Directory Permanently

Now you’ve successfully mounted NFS shared directory on the client machine, but it will be gone after you restart the system. How to mount NFS shared directory permanently? By defining NFS shared directory on the _/etc/fstab_ file.

1\. Edit the _/etc/fstab_ configuration file using your preferred editor and add the following configuration. Change the IP address with the NFS server IP address, the shared directory, and the mount target directory.

```basic
# rw/ro - allow read and write or read-only to the target mount.
# hard - enable client machine to always try to connect to the NFS whenever fail.
# noexec - disable binary file execution on the file system.
# rsize - maximum READ requests that client machine can handle.
# wzise - maximum WRITE requests that client machine can handle.
# timeo - timeout for NFS client before retries requests to the NFS server.

# NFS Shared Directory - target - type - options - dump - pass
172.16.1.20:/srv/data /data nfs rw,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
```

![Configuring /etc/fstab File to Mount NFS Shared Directory](https://adamtheautomator.com/wp-content/uploads/2022/02/image-208.png)

Configuring _/etc/fstab_ File to Mount NFS Shared Directory

2\. Next, run the `mount` command below to mount all filesystem on the `/etc/fstab` file and verify the `/etc/fstab` configuration. If there is no error message, then the configuration is correct.

```bash
sudo mount -a
```

3\. Run the `df` command below to verify the mounted file system on the client machine.

```bash
sudo df -h
```

Similar to the output below, you should see the NFS shared directory is automatically mounted on your client machine.

![Mounting NFS Shared directory permanently using /etc/fstab Configuration](https://adamtheautomator.com/wp-content/uploads/2022/02/image-209.png)

Mounting NFS Shared directory permanently using /etc/fstab Configuration

4\. Lastly, run the `reboot` command to restart the client machine. Once rebooted, log in and rerun the `df` command as you did in step three to verify NFS shared directory.

```bash
sudo df -h
```

As you see below, the NFS shared directory is still permanently mounted on the client machine.

![Verifying NFS Shared Directory is Permanently Mounted to Client Machine](https://adamtheautomator.com/wp-content/uploads/2022/02/image-210.png)

Verifying NFS Shared Directory is Permanently Mounted to Client Machine

## Conclusion

Throughout this tutorial, you’ve learned how to set up an NFS server on a Linux server to share a directory securely. You’ve also touched on permanently mounting NFS shared directory, so you won’t have to manually mount the shared directory each time you reboot your machine.

At this point, you’ve got a fully functional NFS server. So, what’s next? Perhaps learn to add a layer of security for NFS using [Kerberos authentication](https://two-oes.medium.com/configure-nfsv4-service-with-kerberos-encryption-7b7fcf57f67c)?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fubuntu-nfs-server%2F&text=How%20to%20Install%20and%20Configure%20a%20Linux%20Ubuntu%20NFS%20Server)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fubuntu-nfs-server%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fubuntu-nfs-server%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Practical-Linux-Unix-Tee-Commands-for-the-Linux-Admin.jpg)

### [Master Unix tee Commands for Real-World Linux Admin Tasks](/unix-tee/)

Simplify Linux output management and streamline your workflow with the “Unix tee” command. This tutorial guides you through its practical applications.

![](https://adamtheautomator.com/wp-content/uploads/2024/03/openldap-4.jpg)

### [How to Install and Configure an OpenLDAP Ubuntu Server](/openldap/)

Unlock the power of OpenLDAP on Ubuntu for centralized user authentication, seamless access control management, and enhanced directory services!

![](https://adamtheautomator.com/wp-content/uploads/2024/03/pipe-command-in-linux-1.jpg)

### [Unleashing the Power of the Pipe Command in Linux](/pipe-command-in-linux/)

Unlock the prowess of the pipe command in Linux to streamline tasks, boost productivity, and simplify complex operations effortlessly!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
