How To Setup NFS Server on CentOS 7 / RHEL 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.

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.

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: lamp01.darole.org (RHEL 7)
IP Address: 172.16.1.211

NFS Client
Host Name: web01.darole.org(RHEL 7)
IP Address: 172.16.1.221

NFS Client
Host Name: db01.darole.org(RHEL 7)
IP Address: 172.16.1.222

Configure NFS Server

Install NFS Server

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

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

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

[root@lamp01 ~]# systemctl start nfs-server rpcbind
[root@lamp01 ~]# 
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.

[root@lamp01 ~]# mkdir /nfsfileshare

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

[root@lamp01 ~]# chmod 777 /nfsfileshare/

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

[root@lamp01 ~]# vi /etc/exports
/nfsfileshare db01(rw,sync,no_root_squash) web01(rw,sync,no_root_squash)

/nfsfileshare: shared directory

web01, db02 : hostname of client machine. We can also use the IP Address instead of an hostname. It is also possible to define the range of clients with subnet like 172.16.1.0/16.

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.

[root@lamp01 ~]# exportfs -r

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 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.

[root@web01 ~]# 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.

[root@web01 ~]#  showmount -e lamp01

Output:
Export list for lamp01:
/nfsfileshare web01.darole.org,db01.darole.org

As per the output, the /nfsfileshare is available on the NFS server (lamp01) for the NFS client (web01.darole.org).

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.

[root@web01 ~]# mkdir /mnt/nfsfileshare

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

[root@web01 ~]# mount lamp01:/nfsfileshare /mnt/nfsfileshare

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

[root@web01 ~]# mount | grep nfs
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
lamp01:/nfsfileshare on /mnt/nfsfileshare type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=172.16.1.221,local_lock=none,addr=172.16.1.211)

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

[root@web01 ~]# df -PTh /mnt/nfsfileshare/
Filesystem           Type  Size  Used Avail Use% Mounted on
lamp01:/nfsfileshare nfs4   14G  2.4G   12G  18% /mnt/nfsfileshare
[root@web01 ~]#

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

[root@web01 ~]# touch /mnt/nfsfileshare/testCOPY

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

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. 

No comments:

Post a Comment