Friday, February 10, 2017

How to setup NFS Server on CentOS 7 / RHEL 7

In this post, I would explain you , how to setup NFS server on CentOS 7 & RHEL 7. This step by step installation and configuration method of NFS server is work in Fedora 22 version also.

Network File System is used for to share files and folders between Linux / Unix systems. NFS enables you to mount a remote share locally as well as it allows to have updated files across the share.

Before starting the setup method , we need to understand which service and files are used for NFS setup.

Please find the below services which are used for NFS setup. its must be always runs on operating system.

rpcbind service: The rpcbind server is used to converts RPC program numbers into universal addresses.

nfs-server service:  Its enables the NFS clients to access NFS shares.

nfs-lock / rpc-statd service: these are the recovery services when an NFS server crashes and reboots.

nfs-idmap service: It translates user and group ids into names, and to translate user and group names
into ids.

The mail configuration file for NFS server and client is "/etc/exports". It controls which file systems are exported to remote hosts and specifies options.

Now, we will start the step by step process for setup of NFS server on CentOS 7 / RHEL 7.

NFS Server Setup:

1. First we need to install the NFS packages on the server where we want to setup of NFS server. We can install the required NFS packages using YUM.

#yum install nfs-utils libnfsidmap

It's installed all the required packages on NFS server.

2. Once the packages are installed we will enable and start all the above services which we explain in my post.

#systemctl enable rpcbind
#systemctl enable nfs-server
#systemctl start rpcbind
#systemctl start nfs-server
#systemctl start rpc-statd
#systemctl start nfs-idmapd

You can check the status of all these service by using this command "systemctl status service_name" to ensure all are working fine.

3. Now we will created the shared directory which we want to share for client.

#mkdir /backup
#chmod -R 777 /backup

We can change the permission of NFS folder as per your requirement. In my case I'll provide the read write permession to all NFS client on this shared folder, so they can easily copy and remove the files. Ideally for security purpose we never provide 777 permission.

4. In this step we will make an entry of shared folder and client information , what permission we give to client to access the folder and which client can access the NFS shared folder.

# vi /etc/exports

/backup 10.135.0.27(rw,sync,no_root_squash)

In above command output, you can see the "/backup" is shared NFS server folder and "10.135.0.27" user client have rights to access this shared folder.

Also in brackets if you see the permission parameter which is very important when we setup the NFS setup. Please find the small idea about these permission parameteres.

rw: Writable permission to shared folder

sync:  all changes to the according filesystem are immediately flushed to disk.

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

5. Now, we will export the shared directories using following command.

# exportfs -r

We can use other syntax as well for this, which is listed below.

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

6. Now in above step 5, we configured and installed the NFS server but if firewall is running on your machine then we need to add NFS services in firewall as well.

#firewall-cmd --permanent --zone public --add-service mountd
#firewall-cmd --permanent --zone public --add-service rpc-bind
#firewall-cmd --permanent --zone public --add-service nfs
#firewall-cmd --reload

NFS Client Steup:

1. Once we installed the NFS server, now we will mount the remote file system on NFS client machine. So for this , on client machine we will install the same NFS packages which we installed during NFS server setup.

#yum install nfs-utils libnfsidmap

It's installed all the required packages on NFS client. Once the packages is installed on NFS client machine we will start the "rpcbind" services on client machine.

#systemctl enable rpcbind
#systemctl start rpcbind

2. Now we will mount the NFS shared folder on client machine but before doing that we will check on client machine which NFS server is available.


client# showmount -e 10.135.0.27    (10.135.0.2 is myserver machine IP)

Export list for 10.135.0.27:
/backup      10.135.0.2

So you can able to see on client machine our NFS shared folder is available on 10.135.0.27 NFS server. 

3. In this step , now we will mount this NFS shared folder on NFS client machine, for this we will create a mount point on client machine where we mount the server shared folder.

client# mkdir /mnt/backup
client#mount 10.135.0.27:/backup /mnt/backup

So you can check the mount folder using "df -h" command.

4. To make a permanent entry on client machine so that once you take a reboot of client machine , the shared folder is not umount.

client# vi /etc/fstab
10.135.0.27:/backup/ /mnt/backup nfs rw,sync,hard,intr 0 0

Please make an entry permanent on client machine and save it and take a restart of machine , after reboot once you login you will see the shared folder still available on the client machine.

5. For testing of shared folder, you can create a one file on client machine then check on the server this newly created file is shown on the server folder also.

So, using all these steps you can easily setup the NFS server and client on your machine.

No comments:

Post a Comment