How to install NFS client and server on Ubuntu 20.04

How to install NFS client and server on Ubuntu 20.04

Network File System or NFS for short is a distributed file system protocol that allows you to install remote directories on the server. NFS allows you to access files and folders from other servers or clients. You can write to the server from multiple clients or remote systems via the network. This technology allows you to flexibly share resources on multiple systems.

In this tutorial, we will learn how to set up an NFS server and how to install NFS shares on the client using Ubuntu 20.04.

prerequisites

  • Two Ubuntu 20.04 systems. Each server should have a non-root user with sudo privileges and UFW firewall settings.
  • You can set up the server and client via a private network. For our tutorial, I will use host_ip to denote host_ip Address and client_ip Quote the customer’s IP address.

Step 1-Install NFS on the server and client

host

We need to install nfs-kernel-server Packages on the host to share your directory.

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

client

For the client, we will install nfs-common Allow the client to mount the host directory but cannot host the package of the directory itself.

$ sudo apt update
$ sudo apt install nfs-common

Step 2-Create a shared directory on the host

You can grant clients access to directories on the host in two ways. By default, the NFS server does not allow operations that require sudo privileges. This means that the super user on the client cannot write to the file as the root user, redistribute ownership, or perform any tasks that require elevated privileges.

But sometimes, the client needs to perform certain operations on the host, these operations require elevated privileges, but do not require super user access.

the first method

This is the default way of sharing directories. This makes it difficult for the root user on the client to interact with the host using elevated privileges.

First, create a shared directory.

$ sudo mkdir /var/nfs/general -p

If you perform any root operation on the client, NFS will convert it to nobody:nogroup Credentials on the host. Therefore, we need to give proper ownership to the shared directory.

$ sudo chown nobody:nogroup /var/nfs/general

The second method

Here, we will share the host home directory with the client. This will allow the root user on the client to properly access the host.

Since the home directory already exists on the host server, we do not need to create it. Nor does it need to change its permissions because it will create multiple questions for users on the host.

Step 3-Configure NFS on the host

Now it’s time to configure NFS to set up sharing.

Open file /etc/exports On the host server.

$ sudo nano /etc/exports

At the bottom of each directory you share, add the following lines for each line.

/var/nfs/general    client_ip(rw,sync,no_subtree_check)
/home               client_ip(rw,sync,no_root_squash,no_subtree_check)

Let us study all these options and their meanings.

  • w: This allows the client to have read and write access on the host directory.
  • Synchronize: This option will force NFS to write changes before replying. This means that NFS will first write to the host directory and then respond to the client. This will ensure that the actual status of the host server is reflected, but it will slow down file operations.
  • no_subtree_check: This option disables subtree checking. Subtree checking is an operation in which for each NFS request, the server must check whether the file accessed exists and whether the file is in the exported tree. This operation causes problems when the file accessed by the client is renamed. Therefore, in most cases, it is best to disable it. It has a slight impact on security, but it can improve reliability.
  • no_root_squash: By default, NFS converts all operations performed by the client as the root user to unprivileged users on the server. This is to improve security. This option disables some sharing behaviors.

When finished, press to close the file Ctrl + X Then enter ÿ When prompted.

Restart the NFS server to apply the changes.

$ sudo systemctl restart nfs-kernel-server

Step 4-Configure the host firewall

The best practice of NFS is to enable it for each client’s IP address separately, rather than allowing access to it from anywhere.

NFS uses port 2049. Use the following command to enable access to NFS.

$ sudo ufw allow from client_ip to any port nfs

Check the status for verification.

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                 
2049                       ALLOW       167.100.34.78        
OpenSSH (v6)               ALLOW       Anywhere (v6)

Step 5-Create a mount point on the client

Now that our host is configured, it’s time to set up the client.

Create two directories for mounting on the client.

$ sudo mkdir -p /nfs/general
$ sudo mkdir -p /nfs/home

Now that we have set up everything on the host side, we can mount the share using the host’s IP address.

$ sudo mount host_ip:/var/nfs/general /nfs/general
$ sudo mount host_ip:/home /nfs/home

You can use the following command to check whether they have been successfully installed.

$ df -h
Filesystem                      Size  Used Avail Use% Mounted on
udev                            448M     0  448M   0% /dev
tmpfs                            99M  664K   98M   1% /run
/dev/vda1                        25G  3.2G   21G  14% /
tmpfs                           491M     0  491M   0% /dev/shm
tmpfs                           5.0M     0  5.0M   0% /run/lock
tmpfs                           491M     0  491M   0% /sys/fs/cgroup
tmpfs                            99M     0   99M   0% /run/user/1000
123.175.112.87:/var/nfs/general  25G   18G  6.3G  74% /nfs/general
123.175.112.87:/home             25G   18G  6.3G  74% /nfs/home

You can see the two installed shares at the bottom of the list. Since they are all mounted from the same file system, they show full disk usage, not actual usage of the mounted directory.

To see how much actual space is occupied by each installation, use the following command.

$ du -sh /nfs/general
36K     /nfs/general

Step 6-Test NFS sharing

Create a test file to /var/nfs/general share it.

$ sudo touch /nfs/general/general.test

Check its ownership.

$ ls -l /nfs/general/general.test
-rw-r--r-- 1 nobody nogroup 0 Aug  1 13:31 /nfs/general/general.test

This was installed using the default NFS option, and since we created the file using the root user on the client computer, ownership of the host has been transferred to nobody:nogroup. Here, client users cannot perform management jobs on the host computer.

Create a test file to / nfs / home` share.

$ sudo touch /nfs/home/home.test

Check its ownership.

$ ls -l /nfs/home/home.test
-rw-r--r-- 1 root root 0 Aug  1 13:32 /nfs/home/home.test

Here, since we created the test file using the root user on the client computer, the ownership remains on the host. This is because we used no_root_squash This flag allows the root user on the client to also be the root user on the host.

Step 7-Mount the NFS share during the boot process

Once the client computer is started, these installations will not be retained. To make them permanent, we need to edit /etc/fstab file.

turn on /etc/fstab File for editing.

$ sudo nano /etc/fstab

Paste the following line at the bottom of the file. Each line corresponds to each directory that we want to mount at boot time.

. . .
host_ip:/var/nfs/general    /nfs/general   nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
host_ip:/home               /nfs/home      nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0

To learn about these options, please check nfs.

$ man nfs

Save the file by pressing Ctrl + X Then enter ÿ When prompted.

Reboot the server to check the installation.

$ sudo reboot

Log in to the client server and verify that there is an installation.

$ df -h
Filesystem                      Size  Used Avail Use% Mounted on
udev                            448M     0  448M   0% /dev
tmpfs                            99M  664K   98M   1% /run
/dev/vda1                        25G  3.2G   21G  14% /
tmpfs                           491M     0  491M   0% /dev/shm
tmpfs                           5.0M     0  5.0M   0% /run/lock
tmpfs                           491M     0  491M   0% /sys/fs/cgroup
tmpfs                            99M     0   99M   0% /run/user/1000
123.175.112.87:/var/nfs/general  25G   18G  6.3G  74% /nfs/general
123.175.112.87:/home             25G   18G  6.3G  74% /nfs/home

Step 8-Unmount the NFS share

After NFS mounting is not required, you can use the following command to uninstall it.

$ sudo umount /nfs/home
$ sudo umount /nfs/general

A common mistake is to use unmount Instead umount This is the actual command.

Verify that they have been uninstalled correctly.

$ df -h
Filesystem                      Size  Used Avail Use% Mounted on
udev                            448M     0  448M   0% /dev
tmpfs                            99M  664K   98M   1% /run
/dev/vda1                        25G  3.2G   21G  14% /
tmpfs                           491M     0  491M   0% /dev/shm
tmpfs                           5.0M     0  5.0M   0% /run/lock
tmpfs                           491M     0  491M   0% /sys/fs/cgroup
tmpfs                            99M     0   99M   0% /run/user/1000

You also need to /etc/fstab Files so that they will not be remounted at startup. You can also place a # The front character in case you need to use the mount again.

in conclusion

This concludes our tutorial, where we learned how to create NFS mounts on the host and how to access them from different client computers. If you have any questions, please raise them in the comments below.

Source

Sidebar