How to set up NFS server on CentOS/RHEL 7

In this article, you are going to learn about how to set up an NFS server on CentOS/RHEL 7.

NFS stands for Network File System. It is a distributed filesystem protocol. NFS helps to mount the remote directories on your server or client. So, you have central storage space and you are accessing it from multiple servers or clients without any problem.

It was originally developed by Sun microsystems in 1984.

Benefits of NFS server

There are many benefits to the NFS server. But, I have mentioned some of them.

  • It allows us to mount remote filesystem locally.
  • You can use it for a centralized storage system.
  • NFS allows applications to share configuration and data files with multiple nodes.
  • It also helps to share the updated files.

Alternative of NFS 

NFS is not the only file-sharing system over a network. But, you can find the below useful list.

  • AFS – Andrew File System
  • DFS – Distributed File System
  • RFS – Remote File Sharing

Preconditions for NFS server

  • A VPS or a local server on which CentOS 7 is running.
  • A VPS or a local server or client on which CentOS 7 is running. (To test the server)
  • You must log-in with a root user or a user with sudo privilege to make the changes.
  • Internet connection

Lab settings

For this lab, we are going to use two computers. So, you can find the below information useful for the setup.

Setup for NFS server

Firstly, we’ll install the nfs software package and we are going to use it as a server.

  • Operating system: CentOS/RHEL 7
  • Hostname: server.linuxgurus.local
  • IP address: 192.168.43.10

Setup for NFS client

  • Secondly, we’ll use the client to test our server.
  • Operating system: CentOS/RHEL 7
  • Hostname: client.linuxgurus.local
  • IP address: 192.168.43.20

Step 1: Installation of the NFS server

  • The package name of the nfs server software is nfs-utils and it is available in the default repository.
  • We are using the yum package manager to install it.
  • You can find the below command helpful to install now.
sudo yum -y install nfs-utils

Step 2: Starting and enabling the nfs service

As we have completed the installation step. So, now we are going to start and enable the nfs service. You can use the below command useful for it.

sudo systemctl enable --now nfs

Step 3: Checking the status of the nfs service

This is a necessary step to perform before going further. I mean to check the nfs service status. You can use the below command to check the status of the nfs service.

sudo systemctl status nfs

You can see in the below picture.

Confirming the nfs service status.

Step 4: Create a directory to share

The installation process has been completed successfully. So, this is the time to create a directory. Therefore, we can share that. I am creating a directory with the name nfsshare in the / partition.

So, let’s start,

Firstly, use the below command as it is or, change the directory name if you are creating a directory with any other name.

sudo mkdir /nfsshare

Secondly, allow the nfs client to read and write permission for the created directory. So, again you can use the below command to give permission.

sudo chmod 777 /nfsshare

Step 5: Changing or creating  the configuration file for share

We have to modify in the nfs configuration file which is located at /etc/exports, it may be not there, so we’ll create it. and we’ll put the entry of our directory /nfsshare. Here, I’m using the vim editors to open the configuration file. But, you can use any other file editors as per your choice.

sudo vim /etc/exports

Now, press to go into insert mode. And type the below information.

/nfsshare 192.168.43.10(rw,sync,no_root_squash)

/nfsshare this is the name of the shared directory

192.168.43.10 this is the IP address of the server machine. You can also use the hostname besides using the IP address. If you want to define the range of the clients with a subnet mask, then you can do that as 192.168.43.0/24

rw stands for read and write permissions.

sync if any changes are going on then it will synchronize that immediately.

no_root_squash is a tag that makes the client root user as the server root equivalent.

Step 6: Exporting the shared directory

Now we are going to export the shared directories using the following command.

exportfs -r

Note: You can also find the below command useful.

displays a list of shares files and export options on a server using the command

exportfs -v

Exports all directories listed in /etc/export using the command

exportfs -a

UnExport one or more directories using the command

exportfs -u

ReExport all directories after modifying /etc/exports using the command

exportfs -r

Step 7: Firewall configuration

This is the time to allow the services in the firewall. There are some services that we have to allow in the firewall. So, use the below command to allow them.

firewall-cmd --permanent --add-service nfs
firewall-cmd --permanent --add-service mountd

And also allow the below service

firewall-cmd --permanent --add-service rpc-bind

Now, we have to reload the firewall service

firewall-cmd --reload

Step 8: Restart the nfs service

Use the below command to restart the nfs service

sudo systemctl restart nfs

Step 9: Checking nfs server share 

Use the below command to verify if the nfs server is available to share or not.

showmount -e 192.168.43.10

Note: Change the IP address with your nfs server IP address. Or you can use your nfs server hostname.

showmount-e-command-nfs
Listing the mounted directories on the NFS server.

Step 10: Configuration of the NFS client (installation of nfs client)

You have to install the NFS package on the NFS client so that you can mount the remote nfs directory share. You can find the below command to install the nfs package on the client machine.

sudo yum -y install nfs-utils

Step 11: Creating the share directory on the nfs client machine

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

mkdir /mnt/nfsshare

Step 12: Mounting the nfs server share directory on the nfs client

Use the below command to mount an nfs share /nfsshare from NFS server 192.168.43.10 in /mnt/nfsshare on the nfs client.

sudo mount 192.168.43.10:/nfsshare /mnt/nfsshare

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

mount | grep nfs
mount-nfs-client
Checking the mount of the nfs share directory

You can also use the df -hT command to check it.

df -hT
Checking the mounted device using the df -hT command.

Step 13: Creating a file from nfs client to verify the permission

You can try to create a file to test if all the way it’s working or not. For instance, I’m creating a file with the name of the demofile.

touch /mnt/nfsshare/demofile

You can look at the picture.

nfs-share-test
File created to verify the nfs client setting

Step 14: Permanent mounting nfs share in fstab configuration file

If you want to put this share directory work permanently even after the reboot of the client machine then you need to put the entry in fstabSo, let’s edit the fstab file which is located in /etc/ directory. To edit the /etc/fstab file, I am using the vim editors but you can use it as per your choice.

vim /etc/fstab

Now, go to the insert mode after pressing and paste the below lines and change the directory name and IP address as per what you have created.

192.168.43.10:/nfsshare /mnt/nfsshare nfs nosuid,rw,sync,hard,intr 0 0

But, to save the file press the Esc key and type:wq and hit the enter key.

Step 15: Rebooting the system and re-verifying the automount setting

Firstly, we are going to reboot the nfs client machine using the below command.

sudo reboot

Secondly, we are going to re-verify using the mount command.

mount | grep nfs

You will get the output like in the below picture.

mount-nfs-client
Checking the mount of the nfs share directory

Thirdly, If you want to unmount this directory then you can use the below command.

sudo umount /mnt/nfsshare

Note: if you want to unmount completely then you have to remove the entry from the fstab in nfs client.

So, this is it about the nfs server. But, Keep practicing.

NFS service management

There are few commands to manage the service of the nfs server.

So, if you want to stop the service, you can use the below command.

sudo systemctl stop nfs

To restart  the service, use the below command

sudo systemctl restart nfs

Also, check the service if it is enabled on boot or not, you can use the below command

sudo systemctl is-enabled nfs

Conclusion

In this tutorial, you learn about nfs server installation on CentOS/RHEL 7. In conclusion, you can install an nfs server now. So, I hope you understand but if you have any questions, you can ask in the comment section.

You can read about 

How to install Nginx web server on CentOS 8

Also, you may find this information useful from oracle  

Managing Network File Systems in Oracle Solaris 11.1

And, What is the mount command in Linux?

Leave a Reply

Your email address will not be published.