# Installing and Configuring NFS Server on Ubuntu 22.04

Network File System (NFS) allows a system to share directories and files with others over a network. With NFS, users and programs can access files on remote systems as if they were on the local hard disk. This is useful for sharing resources such as documents, media files, and configurations across multiple machines.

**<mark>Step 1: Update the System</mark>**

Ensure all packages are up-to-date:

```plaintext
sudo apt update
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726932599489/4c3b6936-283d-40a2-8fc8-f42d7b3a20db.png align="center")

**<mark>Step 2: Install NFS Server</mark>**

Install the `nfs-kernel-server` package to provide NFS functionality:

```plaintext
sudo apt install nfs-kernel-server -y
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726932658767/e191520a-cd0b-4a49-a569-4a48c871b136.png align="center")

**<mark>Step 3: Create a Directory for Sharing</mark>**

Create a directory that you want to share over the network.

```plaintext
sudo mkdir -p /mnt/nfs_share
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726932740739/016a3bf9-5fc3-45bf-b425-bb3794a7adb3.png align="center")

Set the appropriate permissions for the shared directory. For example, to allow read/write access to everyone:

```plaintext
sudo chown nobody:nogroup /mnt/nfs_share
sudo chmod 777 /mnt/nfs_share
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726932790932/8d624d63-5e70-4779-9a4a-5087a8aed930.png align="center")

**<mark>Step 4: Configure NFS Exports</mark>**

#### Edit the `/etc/exports` file to define the directories that you want to share and specify which clients can access them:

```plaintext
sudo nano /etc/exports
```

**Contents of** `/etc/exports` should look like this:

```plaintext
/mnt/nfs_share 192.168.13.0/24(rw,sync,no_subtree_check)
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726935771505/75a93f30-9f10-4079-ac8a-a38b8677f4f1.png align="center")

Here’s what the options mean:

* `rw`: Allow read and write access.
    
* `sync`: Ensure changes are written to disk before the client is informed.
    
* `no_subtree_check`: Improves performance by disabling subtree checking.
    

**<mark>Step 5: Apply the NFS Export Configuration</mark>**

Apply the new export configuration by running:

```plaintext
sudo exportfs -ar
sudo exportfs -v
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726935930046/bbc5b1f4-177d-493e-8fba-861731fd371d.png align="center")

**<mark>Step 6: Start and Enable NFS Server</mark>**

Start the NFS server and enable it to run at boot:

```plaintext
sudo systemctl start nfs-kernel-server
sudo systemctl enable nfs-kernel-server
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726933167540/50b18fc7-d252-4955-8d18-32aac109320b.png align="center")

```plaintext
sudo systemctl status nfs-kernel-server
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726934339497/8ed5a01b-f81e-4750-8bea-2f220c7df805.png align="center")

#### Check if the NFS export configuration is correct by running:

```plaintext
sudo exportfs -v
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726936016118/653b69d9-ddff-4c44-8f99-63f969130dbd.png align="center")

**<mark>Step 7: Adjust Firewall</mark>**

If you are using `ufw` (Uncomplicated Firewall), allow NFS traffic:

```plaintext
sudo ufw allow from 192.168.13.0/24 to any port nfs
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726936176905/a3a0cd07-2101-4f8a-a611-3fe94eda4d84.png align="center")

**<mark>Step 8: Mount NFS Share on Client</mark>**

On a client machine, install the NFS client tools:

```plaintext
sudo apt install nfs-common -y
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726933759764/6801a045-d9f0-4e28-b37c-997084c855ec.png align="center")

Create a directory to mount the NFS share:

```plaintext
sudo mkdir -p /mnt/nfs_client
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726933797976/50e86cde-2073-423b-abaa-30bec83ad67a.png align="center")

Mount the NFS share from the server (replace `nfs-server-ip` with the IP address of your NFS server):

```plaintext
sudo mount nfs-server-ip:/mnt/nfs_share /mnt/nfs_client
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726936737249/dc9a827b-b170-4562-9bb8-10bfb99bb2fc.png align="center")

To check the mount:

```plaintext
df -h
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726939533246/063fba7c-6148-436c-9376-18f437f8cc6c.png align="center")

To make the mount persistent across reboots, add the following line to the client’s `/etc/fstab`:

```plaintext
sudo nano /etc/fstab
```

Add this line:

```plaintext
nfs-server-ip:/mnt/nfs_share /mnt/nfs_client nfs defaults 0 0
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726936798410/3b752ffc-3bc9-4ee0-b8ef-6505026b2c7e.png align="center")

**<mark>Step 9: Verify the NFS Share</mark>**

On the NFS server, create some folders:

```plaintext
mkdir folder1
mkdir folder2
mkdir folder3
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726937369609/afe97123-bfdf-4c29-8dca-da34e081a1f9.png align="center")

Now check the mounted directory on the client machine to verify that the NFS share is working:

```plaintext
ls -l /mnt/nfs_client
```

You should see output like this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726937460564/d4e9b1eb-aad0-426e-a9fe-6de4e1eb6904.png align="center")

All folders should be visible, indicating that the NFS share is functioning correctly.
