How To Setup NFS Server on CentOS 7 / RHEL 7

7

NFS stands for Network File System, helps you to share files and folders between Linux / Unix systems, developed by SUN Microsystems in 1990. NFS enables you to mount a remote share locally.

This guide helps you to setup NFS server on CentOS 7 / RHEL 7.

Benefits of NFS

  • File / Folder sharing between *nix systems
  • Allows to mount remote filesystems locally
  • Can be acted as Centralized Storage system
  • It can be used as a Storage Domain ( Datastore) for VMware and other Virtualization Platform.
  • Allows applications to share configuration and data files with multiple nodes.
  • Allows having updated files across the share.

Important Services

The following are the important NFS services, included in nfs-utils packages.

rpcbind: The rpcbind server converts RPC program numbers into universal addresses.

nfs-server: It enables clients to access NFS shares.

nfs-lock / rpc-statd: NFS file locking. Implement file lock recovery when an NFS server crashes and reboots.

nfs-idmap: It translates user and group ids into names, and to translate user and group names
into ids

Important Configuration Files

You would be working mainly on below configuration files to setup NFS server and Clients.

/etc/exports: It is the main configuration file, controls which file systems are exported to remote hosts and specifies options.

/etc/fstab: This file is used to control what file systems including NFS directories are mounted when the system boots.

/etc/sysconfig/nfs: This file is used to control which ports the required RPC services run on.

/etc/hosts.allow and /etc/hosts.deny: These files are called TCP wrappers, controls the access to the NFS server. It is used by NFS to decide whether or not to accept a connection coming in from another IP address.

Environment

Here, I will use CentOS 7 minimal for this demo. This guide should also work on Oracle Linux and Fedora systems.

NFS Server

Host Name: server.itzgeek.local (CentOS 7)
IP Address: 192.168.1.10/24

NFS Client

Host Name: client.itzgeek.local (CentOS 7)
IP Address: 192.168.1.20/24

Configure NFS Server

Install NFS Server

Install the below package for NFS server using the yum command.

yum install -y nfs-utils

Once the packages are installed, enable and start NFS services.

systemctl start nfs-server rpcbind
systemctl enable nfs-server rpcbind

Create NFS Share

Now, let’s create a directory to share with the NFS client. Here I will be creating a new directory named nfsfileshare in the / partition.

You can also share your existing directory with NFS.

mkdir /nfsfileshare

Allow NFS client to read and write to the created directory.

chmod 777 /nfsfileshare/

We have to modify /etc/exports file to make an entry of directory /nfsfileshare that you want to share.

vi /etc/exports

Create a NFS share something like below.

/nfsfileshare 192.168.1.20(rw,sync,no_root_squash)

/nfsfileshare: shared directory

192.168.1.20: IP address of client machine. We can also use the hostname instead of an IP address. It is also possible to define the range of clients with subnet like 192.168.1.0/24.

rw: Writable permission to shared folder

sync: All changes to the according filesystem are immediately flushed to disk; the respective write operations are being waited for.

no_root_squash: By default, any file request made by user root on the client machine is treated as by user nobody on the server. (Exactly which UID the request is mapped to depends on the UID of user “nobody” on the server, not the client.) If no_root_squash is selected, then root on the client machine will have the same level of access to the files on the system as root on the server.

You can get to know all the option in the man page man exports or here.

Export the shared directories using the following command.

exportfs -r

Extras:

exportfs -v: Displays a list of shares files and export options on a server.
exportfs -a: Exports all directories listed in /etc/exports.
exportfs -u: UnExport one or more directories.
exportfs -r: ReExport all directories after modifying /etc/exports.

After configuring NFS server, we need to mount that shared directory in the NFS client.

Configure Firewall

We need to configure the firewall on the NFS server to allow NFS client to access the NFS share. To do that, run the following commands on the NFS server.

firewall-cmd --permanent --add-service mountd
firewall-cmd --permanent --add-service rpc-bind
firewall-cmd --permanent --add-service nfs
firewall-cmd --reload

Configure NFS client

Install NFS Client

We need to install NFS packages on NFS client to mount a remote NFS share. Install NFS packages using below command.

yum install -y nfs-utils

Check NFS Share

Before mounting the NFS share, I request you to check the NFS shares available on the NFS server by running the following command on the NFS client.

Replace the IP Address with your NFS server IP Address or hostname.
showmount -e 192.168.1.10

Output:

Export list for 192.168.1.10:
/nfsfileshare 192.168.1.20

As per the output, the /nfsfileshare is available on the NFS server (192.168.1.10) for the NFS client (192.168.1.20).

Extras:

showmount -e : Shows the available shares on your local machine (NFS Server).
showmount -e <server-ip or hostname>: Lists the available shares on the remote server

Mount NFS Share

Now, create a directory on NFS client to mount the NFS share /nfsfileshare which we have created in the NFS server.

mkdir /mnt/nfsfileshare

Use below command to mount a NFS share /nfsfileshare from NFS server 192.168.1.10 in /mnt/nfsfileshare on NFS client.

mount 192.168.1.10:/nfsfileshare /mnt/nfsfileshare

Verify the mounted share on the NFS client using mount command.

mount | grep nfs

Output:

ssunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)
192.168.1.10:/nfsfileshare on /mnt/nfsfileshare type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.20,local_lock=none,addr=192.168.1.10)

Also, you can use the df -hT command to check the mounted NFS share.

df -hT

Output:

Filesystem                 Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root    xfs        50G  1.2G   49G   3% /
devtmpfs                   devtmpfs  485M     0  485M   0% /dev
tmpfs                      tmpfs     496M     0  496M   0% /dev/shm
tmpfs                      tmpfs     496M  6.7M  490M   2% /run
tmpfs                      tmpfs     496M     0  496M   0% /sys/fs/cgroup
/dev/mapper/centos-home    xfs        47G   33M   47G   1% /home
/dev/sda1                  xfs      1014M  154M  861M  16% /boot
tmpfs                      tmpfs     100M     0  100M   0% /run/user/0
192.168.1.10:/nfsfileshare nfs4       50G  1.2G   49G   3% /mnt/nfsfileshare

Create a file on the mounted directory to verify the read and write access on NFS share.

touch /mnt/nfsfileshare/test

If the above command returns no error, you have working NFS setup.

Automount NFS Shares

To mount the shares automatically on every reboot, you would need to modify /etc/fstab file of your NFS client.

vi /etc/fstab

Add an entry something like below.

#
# /etc/fstab
# Created by anaconda on Wed Jan 17 12:04:02 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=60a496d0-69f4-4355-aef0-c31d688dda1b /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
192.168.1.10:/nfsfileshare /mnt/nfsfileshare    nfs     nosuid,rw,sync,hard,intr  0  0

Save and close the file.

Reboot the client machine and check whether the share is automatically mounted or not.

reboot

Verify the mounted share on the NFS client using mount command.

mount | grep nfs

Output:

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
192.168.1.10:/nfsfileshare on /mnt/nfsfileshare type nfs4 (rw,nosuid,relatime,sync,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.20,local_lock=none,addr=192.168.1.10)

If you want to unmount that shared directory from your NFS client after you are done with the file sharing, you can unmount that particular directory using umount command.

umount /mnt/nfsfileshare

Conclusion

You have set up NFS Server and NFS Client on CentOS 7 / RHEL 7 successfully. If you wish not to use static mounts, you can configure AutoFS on CentOS 7 to mount NFS share only when a user accesses them.

You might also like