Configuring services for NFS in Ubuntu 20.04

Posted by JessePHP on Mon, 17 Jan 2022 19:59:48 +0100

Reading guideNFS is an acronym for Network File System. It is a distributed protocol that enables clients to access shared files on remote servers. In this article, you will install services for NFS in Ubuntu 20.04 LTS. Then, demonstrate how to access files on the server from the client system.

System environment

NFS server: Ubuntu 20.04 LTS, IP address: 192.168.43.174
NFS client: Centos 8. IP address: 192.168.43.131

1, Installing NFS server

Run below command Install NFS server:

bob@ubuntu-20-04:~$ sudo apt install nfs-kernel-server

Use the following command Check whether NFS server has started:

bob@ubuntu-20-04:~$ sudo systemctl status nfs-server
● nfs-server.service - NFS server and services
     Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
     Active: active (exited) since Wed 2021-04-21 10:20:29 CST; 1min 30s ago
   Main PID: 41727 (code=exited, status=0/SUCCESS)
      Tasks: 0 (limit: 2278)
     Memory: 0B
     CGroup: /system.slice/nfs-server.service

4 October 21:20:28 ubuntu-20-04 systemd[1]: Starting NFS server and services...
4 October 21:20:29 ubuntu-20-04 systemd[1]: Finished NFS server and services.

2, Create NFS shared directory

The next step is to create an NFS shared directory. We will create it in the / mnt directory. Here, our NFS shared directory is called ShareFolder:

bob@ubuntu-20-04:~$ sudo mkdir -p /mnt/ShareFolder

We hope that all clients can access the contents of the shared folder, so we assign the highest permission:

bob@ubuntu-20-04:~$ sudo chown nobody:nogroup /mnt/ShareFolder
bob@ubuntu-20-04:~$ sudo chmod -R 777 /mnt/ShareFolder/

3, Edit the exports configuration file

Edit the / etc/exports configuration file to allow which clients can access the share.

Open the configuration file with the following command:

bob@ubuntu-20-04:~$ sudo vim /etc/exports 

The following entries indicate that single client access, multiple client access and client access of a network segment are allowed:

# If only one client is allowed to access, you can write the IP address of only one client
/mnt/ShareFolder 192.168.43.131(rw,sync,no_subtree_check)
# If only multiple clients are allowed to access, you can write as follows
/mnt/ShareFolder 192.168.43.131(rw,sync,no_subtree_check)
/mnt/ShareFolder 192.168.43.171(rw,sync,no_subtree_check)
/mnt/ShareFolder 192.168.43.137(rw,sync,no_subtree_check)
# If the client access of a network segment is running, it can be written as follows:
/mnt/ShareFolder 192.168.43.*(rw,sync,no_subtree_check)
perhaps
/mnt/ShareFolder 192.168.43.0/24(rw,sync,no_subtree_check)


Interpretation of permissions in the configuration file:

  • rw allow read and write
  • sync files are written to both hard disk and memory
  • no_subtree_check even if the output directory is a subdirectory, the nfs server does not check the permissions of its parent directory, which can improve efficiency

4, export shared directory

Use the following command to enable and take effect the shared folder:

bob@ubuntu-20-04:~$ sudo exportfs -arv
exporting 192.168.43.*:/mnt/ShareFolder

Use showmount -e to see if the shared directory is visible:

bob@ubuntu-20-04:~$ showmount -e 192.168.43.174
Export list for 192.168.43.174:
/mnt/ShareFolder 192.168.43.*

5, Configure client

Install NFS client:

# At Ubuntu 20 Installing client in: NFS common
bob@ubuntu-20-04:~$ sudo apt install nfs-common
# Installing the client in Centos8: NFS utils
[root@localhost ~]# yum -y install nfs-utils

Then create a ClientFolder directory in the / mnt directory from which the NFS shared directory on the server will be mounted.

[root@localhost ~]# mkdir -p /mnt/ClientFolder

Finally, mount the NFS shared directory as follows:

[root@localhost ~]# mount 192.168.43.174:/mnt/ShareFolder /mnt/ClientFolder/

6, Test NFS shared directory

In order to test whether the configuration is normal, we will create a file in the client / mnt/ClientFolder folder to test, as shown below:

[root@localhost ClientFolder]# dd if=/dev/zero of=./test.dd bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0.0540562 s, 194 MB/s
[root@localhost ClientFolder]# ll
total 10240
-rw-r--r-- 1 nobody nobody 10485760 Apr 21  2021 test.dd


Return to the server and check whether the file can be seen in the / mnt/ShareFolder Directory:

bob@ubuntu-20-04:~$ ll /mnt/ShareFolder/
total 10248
drwxrwxrwx 2 nobody nogroup     4096 4 October 21:50 ./
drwxr-xr-x 4 root   root        4096 4 October 21:24 ../
-rw-r--r-- 1 nobody nogroup 10485760 4 October 21:50 test.dd

summary

NFS is an acronym for Network File System. It is a distributed protocol that enables clients to access shared files on remote servers. That's how Linux should learn