How to mount nfs ubuntu 22. 04

How to Mount NFS Ubuntu 22.04

NFS stands for the Network File System, a protocol that allows mounting remote directories on a server and accessing it from different clients. With NFS, you get a standard way of accessing remote systems and it facilitates resource sharing.

This guide goes through installing the NFS server, creating a shared space, and mounting and unmounting the NFS server.

Mounting the NFS Ubuntu 22.04

To work with the NFS, you must install it on a server and the client.

Installing the NFS Server

Login to your server where you want to install and configure the NFS server, then update the packages.

$
Sponsored
sudo apt update

How to mount nfs ubuntu 22. 04 1

Once the update is done, we need to install the NFS server package. Install it via apt with the command below.

$ sudo apt install nfs-kernel-server

Press y to continue with the installation.

How to mount nfs ubuntu 22. 04 2

After the NFS server is installed, check its status. On Ubuntu 22.04, the NFS server is automatically enabled after the installation. If we check the status, we confirm that it is active and ready for use.

$ sudo systemctl status nfs-server

How to mount nfs ubuntu 22. 04 3

Furthermore, the NFS server will automatically be enabled even after you reboot your server.

After the installation, we need to create our shared directory on our server so that when we mount NFS on the client, we can access this shared directory.

The shared directory is defined in the /etc/exports but we must first create it. For this case, let us name it linuxhint_shared.

$ sudo mkdir /mnt/linuxhint_shared

How to mount nfs ubuntu 22. 04 4

Change the directory’s permissions to grant all permissions to all users. This way, we can read, write, and execute the directory from the client machine.

See also  Flowblade Video Editor 2.14 Adds Editable Titles, Slowmo Playback & USB Shuttle
$ sudo chmod 777 /mnt/linuxhint_shared/

How to mount nfs ubuntu 22. 04 5

Sometimes, you can take it further and change the directory ownership. But that depends on your preference.

To define the shared directory on the /etc/exports file on the server, we must have the client IP as we must define it and point it to the shared directory.

So, log in to your client machine and get its IP address. You can use the ifconfig or the IP commands.

$ ip a | grep en

How to mount nfs ubuntu 22. 04 6

For our case, we are getting the IP address for the enp0s3 network interface. Our client IP for this case is 192.168.1.122.

Use a file editor of your choice to access the /etc/exports file.

$ sudo nano /etc/exports

Once the file opens, define the shared directory and the IP address of the NFS client that will use it. We have also granted the read and write permissions to the shared directory using the rw option. The sync ensures the NFS server is available to the clients and allows it to write changes before it responds to the client machines.

Lastly, adding the no_subtree_check disables the subtree checking and guarantees that no conflict arises when users change the filename.

How to mount nfs ubuntu 22. 04 7

Replace the file content to match your shared directory and your client’s IP, then save and exit the file.

To apply the changes made to the file and export the shared directory, run the command below.

$ sudo exportfs -a

How to mount nfs ubuntu 22. 04 8

Restart the NFS server and confirm that its status is set to active.

To apply the changes made to the file and export the shared directory, run the command below.

How to mount nfs ubuntu 22. 04 9

You can verify the shared directory defined in the NFS server using the exportfs command.

Sponsored

To apply the changes made to the file and export the shared directory, run the command below.

$ sudo exportfs -v

You should get the path to the shared directory we created earlier.

How to mount nfs ubuntu 22. 04 10

The last step on the server is to configure the firewall to allow traffic from the client machine. For that, create the rule below and replace the IP with that of your client machine.

$ sudo ufw allow from 192.168.1.122 to any port nfs

How to mount nfs ubuntu 22. 04 11

Check the firewall status to confirm that your rule was added successfully.

$ sudo ufw status

How to mount nfs ubuntu 22. 04 12

Configuring NFS on the Client

Login to your client machine and update the repositories.

$ sudo apt update

You then need to install the nfs-common package to help with mounting the NFS server shared directory.

$ sudo apt install nfs-common

How to mount nfs ubuntu 22. 04 13

Once the package installs, we then need to create a mount destination on the client machine. Let us name it client_shared.

$ sudo mkdir -p /mnt/client_shared

How to mount nfs ubuntu 22. 04 14

Specify the NFS server IP address, the path to the shared directory, and the destination mount directory on the client machine to mount the NFS shared directory on the client machine, as in the example below.

$ sudo mount 192.168.1.103:/mnt/linuxhint_shared /mnt/client_shared

How to mount nfs ubuntu 22. 04 15

Verify that the NFS server is mounted on the client machine using the df command.

See also  How to Configure Static IP Address on Ubuntu 20.04?
$ sudo df -h

Our NFS is mounted successfully on the client.

How to mount nfs ubuntu 22. 04 16

Let us create a file on the client machine to the shared folder of the NFS server to check if we have the write permissions.

How to mount nfs ubuntu 22. 04 17

Open the server and check if the file is available. If you open the file, we note that it is the same one we created on the client machine. This confirms that our shared directory on the NFS server works as expected.

How to mount nfs ubuntu 22. 04 18

Mounting NFS Automatically

The method we have described so far involves manually mounting NFS on Ubuntu. We can automate the task such that the NFS server will be mounted at boot-up time.

First, let us unmount the NFS destination directory.

$ sudo umount /mnt/client_shared

How to mount nfs ubuntu 22. 04 19

Open the /etc/fstab and add the NFS server IP, the shared directory, and the destination directory, as in our example below.

How to mount nfs ubuntu 22. 04 20

Use the mount command to verify that the configuration is correct.

$ sudo mount -a

How to mount nfs ubuntu 22. 04 21

You can confirm that the NFS has been mounted successfully on your client. Every time you boot the client machine, NFS will automatically get mounted.

How to mount nfs ubuntu 22. 04 22

Conclusion

Mounting NFS on Ubuntu 22.04 is not a complicated process. You first need to install and configure the NFS on your server, then install it on the client. Once you configure it, as we have done in this post, your NFS will be mounted successfully on your Ubuntu.

Leave a Comment

Only people in my network can comment.