Network File Shares

What is NFS?

NFS stands for Network File System. A NFS is not your typical filesystem. NFS are a type of filesystem that can be mounted and shared over a network. The advantage of using a NFS filesystem is that you can centrally locate it and then share it out to multiple systems.

To share a directory with another system, you need to add the directory to be shared across the network into a file located in the following path "/etc/exports". The "/etc/exports" file contains a table of local physical filesystems on a NFS server that are accessible to NFS clients.

In the following example, we will set up a NFS share between two CentOS 8 servers.

In the example below, the following servers are used:
server01a - 192.167.122.75
client01a - 192.168.122.76

Install NFS Packages

To configure NFS on a Red Hat Enterprise Linux based systems such as CentOS or Fedora, you will need to install the following packages on the "server" and "client".

Command Issued: dnf install nfs-utils


# dnf install nfs-utils
Last metadata expiration check: 0:03:42 ago on Sat 29 May 2021 14:09:14 BST.
Dependencies resolved.
========================================================================================================================
 Package                           Architecture           Version                          Repository              Size
========================================================================================================================
Installing:
 nfs-utils                         x86_64                 1:2.3.3-35.el8                   baseos                 494 k
Installing dependencies:
 gssproxy                          x86_64                 0.8.0-16.el8                     baseos                 118 k
 keyutils                          x86_64                 1.5.10-6.el8                     baseos                  63 k
 libevent                          x86_64                 2.1.8-5.el8                      baseos                 253 k
 libverto-libevent                 x86_64                 0.3.0-5.el8                      baseos                  16 k
 rpcbind                           x86_64                 1.2.5-7.el8                      baseos                  70 k

Transaction Summary
========================================================================================================================
Install  6 Packages

Total download size: 1.0 M
Installed size: 2.8 M
Is this ok [y/N]: y
Downloading Packages:
(1/6): keyutils-1.5.10-6.el8.x86_64.rpm                                                 123 kB/s |  63 kB     00:00    
(2/6): gssproxy-0.8.0-16.el8.x86_64.rpm                                                 228 kB/s | 118 kB     00:00    
(3/6): libverto-libevent-0.3.0-5.el8.x86_64.rpm                                         192 kB/s |  16 kB     00:00    
(4/6): libevent-2.1.8-5.el8.x86_64.rpm                                                  419 kB/s | 253 kB     00:00    
(5/6): rpcbind-1.2.5-7.el8.x86_64.rpm                                                   983 kB/s |  70 kB     00:00    
(6/6): nfs-utils-2.3.3-35.el8.x86_64.rpm                                                2.6 MB/s | 494 kB     00:00    
------------------------------------------------------------------------------------------------------------------------
Total                                                                                   958 kB/s | 1.0 MB     00:01     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                1/1 
  Installing       : libevent-2.1.8-5.el8.x86_64                                                                    1/6 
  Installing       : libverto-libevent-0.3.0-5.el8.x86_64                                                           2/6 
  Installing       : gssproxy-0.8.0-16.el8.x86_64                                                                   3/6 
  Running scriptlet: gssproxy-0.8.0-16.el8.x86_64                                                                   3/6 
  Running scriptlet: rpcbind-1.2.5-7.el8.x86_64                                                                     4/6 
  Installing       : rpcbind-1.2.5-7.el8.x86_64                                                                     4/6 
  Running scriptlet: rpcbind-1.2.5-7.el8.x86_64                                                                     4/6 
  Installing       : keyutils-1.5.10-6.el8.x86_64                                                                   5/6 
  Running scriptlet: nfs-utils-1:2.3.3-35.el8.x86_64                                                                6/6 
  Installing       : nfs-utils-1:2.3.3-35.el8.x86_64                                                                6/6 
  Running scriptlet: nfs-utils-1:2.3.3-35.el8.x86_64                                                                6/6 
  Verifying        : gssproxy-0.8.0-16.el8.x86_64                                                                   1/6 
  Verifying        : keyutils-1.5.10-6.el8.x86_64                                                                   2/6 
  Verifying        : libevent-2.1.8-5.el8.x86_64                                                                    3/6 
  Verifying        : libverto-libevent-0.3.0-5.el8.x86_64                                                           4/6 
  Verifying        : nfs-utils-1:2.3.3-35.el8.x86_64                                                                5/6 
  Verifying        : rpcbind-1.2.5-7.el8.x86_64                                                                     6/6 

Installed:
  gssproxy-0.8.0-16.el8.x86_64                keyutils-1.5.10-6.el8.x86_64           libevent-2.1.8-5.el8.x86_64       
  libverto-libevent-0.3.0-5.el8.x86_64        nfs-utils-1:2.3.3-35.el8.x86_64        rpcbind-1.2.5-7.el8.x86_64        

Complete!

To start the NFS server and enable it to start automatically at boot time, use the following command:

systemctl enable --now nfs-server


# systemctl enable --now nfs-server
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.

Server Configuration

On the server that is going to be hosting the NFS share, we need to create the directory that will be exported.


# cd /
# mkdir nfs_test
# chmod 775 nfs_test/
# ls -ld nfs_test/
drwxrwxr-x. 2 root root 6 May 29 14:20 nfs_test/

Create Exports file

After creating the directory to be exported, we need to reference this directory within a file called "/etc/exports".

The /etc/exports file controls which file systems are exported to remote hosts along with any specific mount options. The file must follow the rules below:

All Blank lines are ignored.
To add a comment, start a line with a hash "#.
Long lines can be wrapped with a backslash "\".
Each exported file system should be on its own line.
Any lists of authorized hosts placed after an exported file system must be separated by space characters.
Options for each of the hosts must be placed in parentheses directly after the host, without any spaces separating the host and the first parenthesis.

Each entry for an exported file system has the following structure:

export host(options)

Below is the entry created on the NFS server "server01a" in the file "/etc/exports".


/nfs_test 192.168.122.76(rw,sync,no_root_squash)

/nfs_test - The filesystem/directory to be exported.

rw - Read Write Permission.

sync - The NFS server will not reply to requests before changes made by previous requests are written to disk.

no_root_squash - Parameter disables "root" squashing.

Restart NFS Service

After making changes, it is always advisable to restart the NFS services.

Issue the command: systemctl start nfs-server"


# systemctl restart nfs-server

# systemctl status nfs-server
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor >
  Drop-In: /run/systemd/generator/nfs-server.service.d
           └─order-with-mounts.conf
   Active: active (exited) since Sat 2021-05-29 20:05:13 BST; 7s ago
  Process: 1342 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCE>
  Process: 1340 ExecStopPost=/usr/sbin/exportfs -au (code=exited, status=0/SUCC>
  Process: 1337 ExecStop=/usr/sbin/rpc.nfsd 0 (code=exited, status=0/SUCCESS)
  Process: 1363 ExecStart=/bin/sh -c if systemctl -q is-active gssproxy; then s>
  Process: 1353 ExecStart=/usr/sbin/rpc.nfsd (code=exited, status=0/SUCCESS)
  Process: 1351 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCE>
 Main PID: 1363 (code=exited, status=0/SUCCESS)

May 29 20:05:13 server01a systemd[1]: Starting NFS server and services...
May 29 20:05:13 server01a systemd[1]: Started NFS server and services.

Verify exported filesystems

Command: showmount -e


# showmount -e
Export list for server01a:
/nfs_test 192.168.122.76

The above command shows what files are being exported from the NFS server.

Client Configuration

If you haven't already installed the package: "nfs-utils", the you will need to issue the following command: "dnf install nfs-utils -y".

Next you will need to create the mount area where you want to mount the NFS share to. In the example below, I will create a directory called "nfs".


# cd /
# mkdir nfs

# ls -ld nfs
drwxr-xr-x. 2 root root 6 May 29 21:06 nfs

In the above example, the permissions have been left as "root". On your system, you will probably want to amend the ownership of this area.

Verify exported filesystem

To verify your client can see the NFS mount from the NFS server, you can issue the following command:

showmount -e 192.168.122.75

On your system, you will need to amend the IP address to match that of your NFS server.


# showmount -e 192.168.122.75
Export list for 192.168.122.75:
/nfs_test 192.168.122.76

From the above we can see that the NFS server is exporting its share to the client server.

Mount the NFS share to the client

To mount the NFS share manually, you can issue the following command:

mount -t nfs 192.168.122.75:/nfs_test /nfs


# mount -t nfs 192.168.122.75:/nfs_test /nfs

# df -h /nfs
Filesystem                Size  Used Avail Use% Mounted on
192.168.122.75:/nfs_test  2.8G   75M  2.8G   3% /nfs

From the above, we can see that the NFS share is now mounted. I have used the "df -h" command to show the mounted share.

If the share is to be mounted permanently, then you will need to add an entry within the mount table "/etc/fstab".

NFS entry in mount table

To mount the share automatically when the server is started, you will need to add an entry similar to the one below.


192.168.122.75:/nfs_test /nfs  nfs  defaults 0 0

Test Access to the NFS share

A simple way to test the share is to create a file on the NFS server and verify it can be seen on the client.

On the NFS Server I have created a test file called "server_created_file.txt".


[root@server01a nfs_test]# ls -l
total 4
-rw-r--r--. 1 root root 45 May 29 20:29 server_created_file.txt

[root@server01a nfs_test]# cat server_created_file.txt 
hello, I am a file created on the NFS server

Now on the client, navigate to the "/nfs" area and list the files that can be seen.


[root@client01a nfs]# ls -l
total 4
-rw-r--r--. 1 root root 45 May 29 20:29 server_created_file.txt
[root@client01a nfs]# cat server_created_file.txt 
hello, I am a file created on the NFS server

Useful Commands

Below are commands that can be used for "starting", "stopping" and "checking" the current status of the NFS service.

Start Command: systemctl start nfs-server
Stop Command: systemctl stop nfs-server
Restart Command: systemctl restart nfs-server
Status Command: systemctl status nfs-server
Verify Service Enabled: systemctl is-enabled nfs-service
Export files: exportfs -a
Display exported file systems: showmount -e