How Can We Help?

How to Setup NFS server on CentOS

Table of Content
How to Setup NFS server on CentOS
How to Setup NFS server on CentOS

What is NFS server

In this tutorial, we will learn how to set up an NFS server on CentOS. NFS (Network file Sharing) is a protocol used to share files and directories with other linux users. An NFS share is mounted on a client machine. NFS service uses port Number 2049  

For creating NFS server on client machine we have to install nfs-utils tool on server as well as on client machine also for installing nfs-utils tool on server use 

1. Server side configuration.

Step 1: First, install the nfs utility, by installing nfs-utils on your Centos server.

# yum install nfs-utils

Step 2: After installation, we have to start and enable nfs-server.service 

# systemctl start nfs-server.service
#systemctl enable nfs-server.service
Note: For starting and enabling nfs-server.service “rpcbind” service must be enabled and started 

Step 3: Now, if you are using a firewall on the server, allow nfs, mountd, rpc-bind and after adding services to the firewall, reload the firewall service.

# firewall-cmd --permanent --add-service=nfs
# firewall-cmd --permanent --add-service=rpc-bind
# firewall-cmd --permanent --add-service=mountd

# firewall-cmd --reload

After making these changes in firewall configuration you can check the configuration by using the following command

# firewall-cmd --list-all

Step 4: Now select the directory that you want to share with the client machine, in this case, I am going to create the nfsshare directory under the root directory.

#mkdir /nfsshare

Step 5: Now, make the changes in permission and ownership directory /nfsshare

#chown nfsnobody:root /nfsshare
#chmod 777 /nfsshare

Now add the configuration in /etc/exports using vi utility

# vi /etc/exports

Add the following content in the file

<export> <host1>(<options>) <hostN>(<options>)...

Example :


Now verify share with the following command

#exportfs -avr

2. Client side configuration

Step 6: First of all install nfs-utils on client machine

# yum install nfs-utils

Now, check the shared directory over the network

#showmount -e

Here this IP is server IP 

Step 7: Now for temporary mounting use the command

#mount -t nfs -o sync

For Permanent mounting please make entry in /etc/fstab file /mnt/nfsshare    nfs     nosuid,rw,sync,hard,intr  0  0

3. Mounting using autofs

Autofs is a program that automatically mounts specified directories on an on-demand basis. It is based on a kernel module for high efficiency, and can manage both local directories and network shares. These automatic mount points are mounted only when they are accessed, and unmounted after a certain period of inactivity. This on-demand behavior saves bandwidth and results in better performance than static mounts managed by /etc/fstab. While autofs is a control script, automount is the command (daemon) that does the actual auto-mounting. For Auto mounting setup 

Step 8: Install autofs package on client Machine

#yum install autofs

Step 9: Now start and enable autofs service on Machine

#systemctl enable autofs 
#systemctl start autofs

Step 10: Now Create Master map file under /etc/auto.master.d/share.autofs

#vi /etc/auto.master.d/share.autofs

In this file add following content

/mnt/nfsshare /etc/auto.nfs

/mnt/nfsshare Mount Point
/etc/auto.nfs  Mapping file

Now edit /etc/auto.nfs file and add following content and save this file

#vi /etc/auto.nfs
*  -rw,sync,fstype=nfs4

Step 10: Now restart the server

#systemctl reboot

And this is how you have learnt in this tutorial how to Setup NFS server on CentOS


Also Read: How to Setup and Configure FirewallD on CentOS 7, How to install Cockpit on Fedora Server

Table of Contents