How to Install and Configure NFS Server on Linux

In this article we will learn and configure NFS (Network File System) which is basically used to share the files and folders between Linux systems. This was developed by Sun Microsystems in 1980 which allows us to mount the file system in the network and remote users can interact and the share just like local file and folders.
NFS can be configured as a centralized storage solution.
No need of running the same OS on both machines.
Can be secured with Firewalls.
It can be shared along with all the flavors of *nix.
The NFS share folder can be mounted as a local file system.
NFS mount needed at least two machines. The machine hosting the shared folders is called as server and which connects is called as clients.
Server: 192.168.87.156
Client: 192.168.87.158
We needed to install the packages for NFS
# yum install nfs-utils nfs-utils-lib
Output:
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
epel/metalink | 4.0 kB 00:00
* base: mirror.digistar.vn
* epel: mirrors.ustc.edu.cn
* extras: mirror.digistar.vn
* updates: mirror.digistar.vn
Resolving Dependencies
--> Running transaction check
---> Package nfs-utils.x86_64 1:1.2.3-64.el6 will be installed
---> Package nfs-utils-lib.x86_64 0:1.1.5-11.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================================
Package             Arch             Version             Repository                Size
================================================================================================
Installing:
nfs-utils          x86_64             1:1.2.3-64.el6       base                   331 k
nfs-utils-lib      x86_64             1.1.5-11.el6         base                   68 k
Transaction Summary
================================================================================================
Install 2 Package(s)
Total download size: 399 k
Installed size: 1.1 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): nfs-utils-1.2.3-64.el6.x86_64.rpm | 331 kB 00:00
(2/2): nfs-utils-lib-1.1.5-11.el6.x86_64.rpm | 68 kB 00:00
------------------------------------------------------------------------------------------------
Total 60 kB/s | 399 kB 00:06
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : nfs-utils-lib-1.1.5-11.el6.x86_64 1/2
Installing : 1:nfs-utils-1.2.3-64.el6.x86_64 2/2
Verifying : 1:nfs-utils-1.2.3-64.el6.x86_64 1/2
Verifying : nfs-utils-lib-1.1.5-11.el6.x86_64 2/2
Installed:
nfs-utils.x86_64 1:1.2.3-64.el6 nfs-utils-lib.x86_64 0:1.1.5-11.el6
Complete!
After this run the below commands to start the NFS servers and make sure it start at boot time.
# chkconfig nfs on
# service rpcbind start
# service nfs start

Output:
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]


We need to decide a directory which we want to share with the client. The directory should be added to /etc/exports
# vi /etc/exports
All the below lines to the file.
/share 192.168.87.158(rw,sync,no_root_squash,no_subtree_check)
/share – is the share folder which server wants to share
192.168.87.158 – is the IP address of the client to whom want to share
rw – This will all the clients to read and write the files to the share directory.
sync – which will confirm the shared directory once the changes are committed.
no_subtree_check – Will prevents the scanning the shared directory, as nfs performs the scans of every share directory, Disabling the subtree check will increase the reliability, but reduces the security.
no_root_squash – This will all the root user to connect to the designated directory.
Once, we enter the details of the share in config file, run the below command to export them
# exportfs -a
Install the required packages to connect to NFS
# yum install nfs-utils nfs-utils-lib -y
Once the packages are installed on the client, create the directory to mount point the shared folder
# mkdir -p /mnt/share
# mount 192.168.87.156:/share /mnt/share/
To confirm if the share is mounted or not run the command ‘df -h’, this will show the list of mounted folders.
# df -h
Output:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
50G 5.2G 42G 12% /
tmpfs 427M 80K 427M 1% /dev/shm
/dev/sda1 477M 42M 410M 10% /boot
/dev/mapper/VolGroup-lv_home
95G 60M 90G 1% /home
192.168.87.156:/share
18G 2.0G 15G 13% /mnt/share


To see the list of all the mounted file systems.
# mount
Output:
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/VolGroup-lv_home on /home type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.87.156:/share on /mnt/share type nfs (rw,vers=4,addr=192.168.87.156,clientaddr=192.168.87.158)
Create a file and folders in the server share directory
# touch test1
# mkdir test
Then goto the client side machine and check the /mnt/share folders
# ls /mnt/share/ -lh
total 4.0K
drwxr-xr-x 2 root root 4.0K Apr 20 2016 test
-rw-r--r-- 1 root root 0 Apr 20 2016 test1
To automatically mount the share folder permanently while boot in the client machine, add the entries in the /etc/fstab file
# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Apr 2 00:11:04 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=1adb2ad5-d0c7-48a5-9b10-f846a3f9258c /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_home /home ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
192.168.87.156:/share /mnt/share nfs auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0


Some options and important command of NFS
# showmount -e
Export list for localhost.localdomain:
/share 192.168.87.158
This will show the available share on the local machine, so needed to run on the server side.
# showmount -e 192.168.87.156
Export list for 192.168.87.156:
/share 192.168.87.158
This will show the remote server shared folders needed to run on the client side –
# exportfs -v
/share 192.168.87.158(rw,wdelay,no_root_squash,no_subtree_check,sec=sys,rw,no_root_squash,no_all_squash)
List all the share files and folders with options on the server
# exportfs -u
/share 192.168.87.158
This will un-export the shared folders or files which are in /etc/exports
# exports -r
This will refresh the servers list and check for the changes if any.
After this configuration and setup, you should be able to use NFS to share the files between *inx machines without any problem, then we should be able share the folders to only the client to whom we want to share the folder, this will improve the security.