NFS Installation and Configuration on RHEL 7 / CentOS 7

Friends today we will see, NFS installation on NFS server and NFS client on RHEL 7 and CentOS 7. So lets start step by step NFS installation. So first of all we should know what is NFS. Then we will see step by step NFS installation and configuration on NFS server and NFS client.

What is NFS?

NFS stand for Network File System. (NFS) is a way of mounting Linux disk and directories over a network. An NFS server can export one or more directories that can be mounted on a remote Linux machine. Network file system works on all IP-based networks. It uses TCP and UDP for data access and delivery, depending on the version in use.

Now let’s start NFS Server and Client installation and configuration.

Prerequisite

We need two host to installation and configuration NFS server and NFS client:-

  1. NFS Server:- IP 192.168.43.95 and Hostname:- tzclouds.local
  2. NFS Client:- IP 192.168.43.96 and Hostname:- tzcloudclient.local

We are assuming that you have already running CentOS7 or RHEL7 in order to setup NFS server and Client.

NFS Server Installation and Configuration

You have to run this command to install NFS server on NFS server.

[root@tzclouds ~]# yum install nfs-utils libnfsidmap

These are the important NFS services which we need to install.

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

nfs-server : It enables the 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

Now we need to create directory which we want to share with NFS Client server. We can also share our existing directory.

[root@tzclouds ~]# mkdir /root/nfsshare

We need to allow for client servers to read and write to the created directory.

[root@tzclouds ~]# chmod 777 /root/nfsshare/

Then we need to edit /etc/exports file to export NFS directory to the client server. Your /etc/exports file should be like this:-

[root@tzclouds ~]# vi /etc/exports
/root/nfsshare 192.168.43.96(rw,sync,no_root_squash)

~

wq!


[root@tzclouds ~]# cat /etc/exports
/root/nfsshare 192.168.43.96(rw,sync,no_root_squash)
[root@tzclouds ~]#

/root/nfsshare :– shared directory

192.168.43.96 :- This is  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.43.0/24.

rw :- Its gives 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.

We need to configure firewall on NFS server to allow client servers to access NFS shares. We can use this commands.

[root@tzclouds ~]# firewall-cmd --permanent --zone public --add-service mountd
success
[root@tzclouds ~]# firewall-cmd --permanent --zone public --add-service rpc-bind
success
[root@tzclouds ~]# firewall-cmd --permanent --zone public --add-service nfs
success
[root@tzclouds ~]# firewall-cmd --reload
success
[root@tzclouds ~]#

Now we need to start and enable NFS services

[root@tzclouds ~]# systemctl enable rpcbind
[root@tzclouds ~]# systemctl enable nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@tzclouds ~]# systemctl start rpcbind
[root@tzclouds ~]# systemctl start nfs-server
[root@tzclouds ~]# systemctl start rpc-statd
[root@tzclouds ~]# systemctl start nfs-idmapd
[root@tzclouds ~]#

We can check running status of all these services using below commands. All services should be properly running like this:-

[root@tzclouds ~]# systemctl status rpcbind
rpcbind.service - RPC bind service

   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)
   Active: active (running) since Sun 2018-05-20 09:57:34 CEST; 1min 29s ago
  Process: 3976 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 3977 (rpcbind)
   CGroup: /system.slice/rpcbind.service
           └─3977 /sbin/rpcbind -w

May 20 09:57:34 tzclouds.local systemd[1]: Starting RPC bind service...
May 20 09:57:34 tzclouds.local systemd[1]: Started RPC bind service.
[root@tzclouds ~]# systemctl status nfs-server
nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
  Drop-In: /run/systemd/generator/nfs-server.service.d
           └─order-with-mounts.conf
   
  Active: active (exited) since Sun 2018-05-20 09:57:46 CEST; 1min 37s ago
  Process: 3999 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 3994 ExecStartPre=/bin/sh -c /bin/kill -HUP `cat /run/gssproxy.pid` (code=exited, status=0/SUCCESS)
  Process: 3993 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 3999 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service
May 20 09:57:46 tzclouds.local systemd[1]: Starting NFS server and services...
May 20 09:57:46 tzclouds.local systemd[1]: Started NFS server and services.
[root@tzclouds ~]# systemctl status rpc-statd
rpc-statd.service - NFS status monitor for NFSv2/3 locking.
   Loaded: loaded (/usr/lib/systemd/system/rpc-statd.service; static; vendor preset: disabled)
   Active: active (running) since Sun 2018-05-20 09:57:41 CEST; 1min 49s ago
  Process: 3988 ExecStart=/usr/sbin/rpc.statd $STATDARGS (code=exited, status=0/SUCCESS)
 Main PID: 3990 (rpc.statd)
   CGroup: /system.slice/rpc-statd.service
           └─3990 /usr/sbin/rpc.statd
May 20 09:57:40 tzclouds.local systemd[1]: Starting NFS status monitor for NFSv2/3 locking....
May 20 09:57:41 tzclouds.local rpc.statd[3990]: Version 1.3.0 starting
May 20 09:57:41 tzclouds.local rpc.statd[3990]: Flags: TI-RPC
May 20 09:57:41 tzclouds.local rpc.statd[3990]: Initializing NSM state
May 20 09:57:41 tzclouds.local systemd[1]: Started NFS status monitor for NFSv2/3 locking..
[root@tzclouds ~]# systemctl status nfs-idmapd
nfs-idmapd.service - NFSv4 ID-name mapping service
   Loaded: loaded (/usr/lib/systemd/system/nfs-idmapd.service; static; vendor preset: disabled)
   Active: active (running) since Sun 2018-05-20 09:57:46 CEST; 1min 57s ago
  Process: 3987 ExecStart=/usr/sbin/rpc.idmapd $RPCIDMAPDARGS (code=exited, status=0/SUCCESS)
 Main PID: 3992 (rpc.idmapd)
   CGroup: /system.slice/nfs-idmapd.service
           └─3992 /usr/sbin/rpc.idmapd

May 20 09:57:40 tzclouds.local systemd[1]: Starting NFSv4 ID-name mapping service...
May 20 09:57:46 tzclouds.local systemd[1]: Started NFSv4 ID-name mapping service.
[root@tzclouds ~]#

NFS Client Installation and Configuration

Now we need to install NFS packages on NFS client-server to mount remote filesystem. We can install NFS packages using below command.

[root@tzcloudsclient ~]# yum -y install nfs-utils libnfsidmap

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

[root@tzcloudsclient ~]# systemctl enable rpcbind

[root@tzcloudsclient ~]# systemctl start rpcbind

NFS shares directory mount on NFS Clients server.

Before mounting the NFS share, we need to check that our NFS share is available for NFS client or not?  We can check with showmount command on NFS Client server. Like this:-

[root@tzcloudsclient ~]# showmount -e 192.168.43.95
Export list for 192.168.43.95:
/root/nfsshare 192.168.43.96
[root@tzcloudsclient ~]#

You can see here the /root/nfsshare is available on 192.168.43.96

Now we need to create a mount point /mnt/nfsclient on NFS Client to mount the shared folder.

[root@tzcloudsclient ~]# mkdir /mnt/nfsclient

We can use below command to mount a shared directory on NFS client server. /root/nfsshare from NFS server 192.168.43.95 will be mount on /mnt/nfsclient on client server 192.168.43.96

[root@tzcloudsclient ~]# mount 192.168.43.95:/root/nfsshare /mnt/nfsclient
[root@tzcloudsclient ~]#

Verify the mounted share on client server using mount command. You will see like below in bold.

[root@tzcloudsclient ~]# mount | grep nfs
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
192.168.43.95:/root/nfsshare on /mnt/nfsclient type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.43.96,local_lock=none,addr=192.168.43.95)
[root@tzcloudsclient ~]#

We can also use df -h command to check the mounted NFS share drive on client server and you will see here in bold our NFS share drive has been successfully mounted.

[root@tzcloudsclient ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/centos-root        13G  5.2G  7.8G  40% /
devtmpfs                      904M     0  904M   0% /dev
tmpfs                         920M     0  920M   0% /dev/shm
tmpfs                         920M  8.9M  911M   1% /run
tmpfs                         920M     0  920M   0% /sys/fs/cgroup
/dev/sda1                     497M  214M  283M  44% /boot
tmpfs                         184M  4.0K  184M   1% /run/user/42
tmpfs                         184M   24K  184M   1% /run/user/0
192.168.43.95:/root/nfsshare   13G  5.2G  7.8G  40% /mnt/nfsclient
[root@tzcloudsclient ~]#

Now we can create a test file on the mounted directory to verify the read and write access on NFS share. After successfully run this commands we will see here test file has been successfully created in /mnt/nfsclient directory. Like this:-

[root@tzcloudsclient ~]# touch /mnt/nfsclient/test
[root@tzcloudsclient ~]# cd /mnt/nfsclient/
[root@tzcloudsclient nfsclient]# ls -l
-rw-r--r-- 1 root root 0 May 20 11:07 test
[root@tzcloudsclient nfsclient]#

Configuring Auto mount NFS Share in NFS Client.

HostGator Web Hosting
Now we have a configured NFS Server and NFS Client. Next we need to add additional settings such as server persistence and permanent client mount using /etc/fstab. In order to have our NFS exports permanently available after the NFS server system reboot we need to make sure that nfs service auto starts after reboot:-

To mount the shares automatically on every reboot, we need to modify /etc/fstab file in our client server like below in bold.

[root@tzcloudsclient ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Sep 26 12:44:35 2017
#
# 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=9b1b6c8c-a702-4654-8b65-3ea79c368a84 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
192.168.43.95:/root/nfsshare/ /mnt/nfsclient    nfs   rw,sync,hard,intr 0 0
~
:wq!
[root@tzcloudsclient ~]#

Now save this file and reboot the client server and check the share mount point is automatically mounted or not?

[root@tzcloudsclient ~]# reboot

Verify the mounted share on client server using mount command after rebooted your client server and see your share mount point automatically mounted or not?

[root@tzcloudsclient ~]# reboot
login as: root
root@192.168.43.96's password:
Last login: Sun May 20 11:59:06 2018
[root@tzcloudsclient ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/centos-root        13G  5.2G  7.8G  40% /
devtmpfs                      904M     0  904M   0% /dev
tmpfs                         920M     0  920M   0% /dev/shm
tmpfs                         920M  8.8M  911M   1% /run
tmpfs                         920M     0  920M   0% /sys/fs/cgroup
/dev/sda1                     497M  214M  283M  44% /boot
192.168.43.95:/root/nfsshare   13G  5.2G  7.8G  40% /mnt/nfsclient
tmpfs                         184M   16K  184M   1% /run/user/0
[root@tzcloudsclient ~]# mount | grep nfs
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
192.168.43.95:/root/nfsshare on /mnt/nfsclient type nfs4 (rw,relatime,sync,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.43.96,local_lock=none,addr=192.168.43.95)
[root@tzcloudsclient ~]#

Here we can see our NFS share drive has been automatically mounted after reboot our NFS client server. 

DNS installation on RHEL 7 and CentOS 7

Now we have completed installation and configuration NFS Server and NFS Client server.

1 thought on “NFS Installation and Configuration on RHEL 7 / CentOS 7

Leave a Reply

Your email address will not be published. Required fields are marked *