In this article we will cover:

Network File System (NFS) is one of the native ways of sharing files and applications across the network in the Linux/UNIX world. NFS is somewhat similar to Microsoft Windows File Sharing, in that it allows you to attach to a remote file system (or disk) and work with it as if it were a local drive—a handy tool for sharing files and large storage space among users.

 

 

正文

NFS2 vs NFS3 vs NFSv4

NFSv2

NFSv3

NFSv4

Lab Environment

I have three Virtual Machines which I will use for NFS configuration of server and client. Below are the server specs of these Virtual Machines. These VMs are installed on Oracle VirtualBox running on a Linux server.

Still installing Linux manually?

 

I would recommend to configure one click installation using Network PXE Boot Server. Using PXE server you can install Oracle Virtual Machines or KVM based Virtual Machines or any type of physical server without any manual intervention saving time and effort.

 

Install and Configure NFS Server (NFSv4) in RHEL/CentOS 7/8

Install nfs-utils rpm

NOTE:

 

On RHEL system you must have an active subscription to RHN or you can configure a local offline repository using which "yum" package manager can install the provided rpm and it's dependencies.

Install the nfs-utils package:

NFS configuration using /etc/nfs/conf

Starting with RHEL/CentOS 7.7, to configure NFS server you must use /etc/nfs.conf instead of /etc/sysconfig/nfs. Since we plan to only enable NFSv4, we will disable older NFS versions using /etc/nfs.conf.

 

Optionally, disable listening for the RPCBIND, MOUNT, and NSM protocol calls, which are not necessary in the NFSv4-only case. Disable related services:

After you configure NFS server, restart the NFS server to activate the changes and enable it start automatically post reboot. You can also check nfs status using systemctl status nfs-server

Use the netstat utility to list services listening on the TCP and UDP protocols:

The following is an example netstat output on an NFSv4-only server; listening for RPCBIND, MOUNT, and NSM is also disabled. Here, nfs is the only listening NFS service:

ALSO READ:

 

How to open a custom port manually in Linux RHEL/CentOS 7/8

Create NFS share using /etc/exports

The /etc/exports file controls which file systems are exported to remote hosts and specifies options. It follows the following syntax rules:

Syntax:

In this structure:

I have a folder /nfs_shares which we will share on our NFS server

In this NFS configuration guide, we create NFS share /nfs_shares to world (*) with rw and no_root_squash permission

The list of supported options which we can use in /etc/exports for NFS server

Refresh NFS shares

Once you have an /etc/exports file setup, use the exportfs command to tell the NFS server processes to refresh NFS shares.

To export all file systems specified in the /etc/exports file:

Use exportfs -r to refresh shares and reexport all directories (optional as we have already used exportfs -a)

To view and list the available NFS shares use exportfs -v

HINT:

 

Every time you make any change in /etc/exports, you don't need to restart nfs-server, you can use exportfs -r to update the exports content or alternatively you can execute systemctl reload nfs-server to refresh the /etc/exports content

Here,

For complete list of supported options with exportfs, follow man page of exportfs

ALSO READ:

 

You can also create your own man page with a list of instructions for a script or a custom tool which you have created. In real time production environment it is always recommended to also create and release a man page for every script or tool we develop.

Allow NFS server services with firewalld

We will add all the NFS services to our firewalld rule to allow NFS server client communication.

Access NFSv4 shares on Linux Client

Access NFS shares temporarily (non-persistent)

If I try to access NFS shares using NFSv3, as you see after waiting for the timeout period the client fails to mount the NFS share as we have restricted the NFS server to only allow NFSv4 connections.

We can use mount command to list NFS mount points on nfs-client.

To remove NFS share access you can unmount the mount point

Allow permanent access to NFS shares (Persistent)

To access NFS shares persistently i.e. across reboots then you can add the mount point details to /etc/fstab. But be cautious before using this as it would mean that your NFS server is always accessible and it during boot stage of the NFS client, the NFS server is un-reachable then your client may fail to boot.

Add NFS mount point details in /etc/fstab in the below format. Here 10.10.10.12 is my NFS server. I have added some additional mount options rw and soft to access the NFS shares.

HINT:

 

Here since I have shared my NFS shares with rw permission on my NFS configuration steps hence I am using rw on client, if you have a read only NFS shares then accordingly use ro in mount options.

Next execute mount -a to mount all the partitions from /etc/fstab

Check if the mount was successful and you can access NFS share on the client.

Install and Configure NFS Server (NFSv3) in RHEL/CentOS 7/8

NFSv2 and NFSv3 rely heavily on RPCs to handle communications between clients and servers. RPC services in Linux are managed by the portmap service.

The following list shows the various RPC processes that facilitate the NFS service under Linux:

rpc.statd

rpc.rquotad

rpc.mountd

rpc.nfsd

rpc.lockd

rpc.idmapd

rpc.svcgssd

rpc.gssd

Install nfs-utils and rpcbind to setup NFSv3

We will install nfs-utils and additionally we will also need rpcbind to configure NFS server (NFSv3) in Red Hat/CentOS 7/8 Linux

NOTE:

 

On RHEL system you must have an active subscription to RHN or you can configure a local offline repository using which "yum" package manager can install the provided rpm and it's dependencies.

On Debian and Ubuntu you should install below list of rpms

Start nfs-server, rpcind services and check nfs status

We do not need any additional NFS configuration to configure NFS server (basic). But you can check /etc/sysconfig/nfs (if using RHEL/CentOS 7.6 and earlier) or /etc/nfs.conf (if using RHEL/CentOS 7.7 or higher) for any customization.

Check nfs status of nfs-server and rpcbind services to make sure the are active and running

NOTE:

 

systemd will automatically start rpcbind (as a dependency) whenever the nfs server is started, and so you don’t need to explicitly start rpcbind separately.

Check the netstat output for listening TCP and UDP ports

You can compare this output with NFSv4 setup, here we have more number of ports and service running with NFSv3 compared to NFSv4

Create NFS Shares

Next we will create a directory which we can share over NFS server. In this NFS configuration guide, I will create a new directory /nfs_shares to share for NFS clients.

The syntax and procedure to create NFS share is same between NFSv4 and NFSv3

Syntax:

In this structure:

In this NFS configuration guide, we create NFS share /nfs_shares to world (*) with rw and no_root_squash permission

The list of options supported with NFSv3 configuration remains same as I shared under NFSV4 section of this article.

Refresh NFS shares

Once you configure NFS server and have an /etc/exports file setup, use the exportfs command to tell the NFS server processes to refresh NFS shares.

To export all file systems specified in the /etc/exports file:

HINT:

 

I prefer to use exportfs -r as re-exports are the shares. The list of options supported with exportfs between NFSv3 and NFSv4 are same which I shared above in this article.

List the currently exported NFS shares on the server. This command will also show the default permissions applied to the NFS share.

Allow NFS server services with firewalld

You can get the list of NFS and rpcbind ports used by NFSv3 from the netstat output we shared, instead we will use service list to allow firewall access for NFSV3

Reload the firewall service to make the changes persistent

Access NFSv3 shares on Linux Client

Access NFS shares temporarily (non-persistent)

Check if the mount was successful

If I try to access NFS shares using NFSv4

As you see the client was allowed to access the NFS share even with NFSv4 so you see since we have not restricted our NFS server to only use NFSv3, it is allowing NFSv4 connections also.

You can use the same list of commands to list NFS mount points for NFSv3 mounts on the clients as I listed under NFSv4.

To remove NFS share access you can unmount the mount point

Allow permanent access to NFS shares (Persistent)

To access NFS shares persistently i.e. across reboots then you can add the mount point details to /etc/fstab. But be cautious before using this as it would mean that your NFS server is always accessible and it during boot stage of the NFS client, the NFS server is un-reachable then your client may fail to boot.

Add NFS mount point details in /etc/fstab in the below format. Here 10.10.10.2 is my NFS server. I have added some additional mount options other than defaults, such as defaults, soft and nfsvers=3 to access the NFS shares only with v3 protocol.

Next execute mount -a to mount all the partitions from /etc/fstab

Check if the mount was successful and you can access NFS share on the client.

Lastly I hope the steps from the article to install and configure NFS server and client using NFSv3 and NFSv4 on Red Hat and CentOS 7/8 Linux was helpful. So, let me know your suggestions and feedback using the comment section.

*References:* Configure NFS Server with NFSv3 and NFSv4 in RHEL 8 NFS wiki page Linux Administration: Network File System (NFS)

Related Searches: centos nfs server, how to setup nfs share, centos 7 install nfs server, how to check nfs status in linux, how to check if nfs server is running on linux, nfs in linux tutorial, nfs configuration in rhel 7 step by step, install and configure NFS server and client