Network File System (NFS)

NFS, or Network File System, is a server-client protocol for sharing files between computers on a common network. It is available on a variety of UNIX-based operating systems, not just Linux. The server and client do not have to use the same operating system. The client system just needs to be running an NFS client compatible with the NFS server.

RPM of Network File System (NFS)

If NFS rpm had already been installed. Use command.
[root@localhost ~]# rpm -qa | grep nfs If your machine don’t have these rpm install these rpms.
RPM of Network File System (NFS)

If your machine don't have these rpm install these rpms.

Starting or stopping NFS service

[root@ server ~]# service nfs start
[root@ server ~]# service nfs stop
[root@ server ~]# service nfs restart

[root@ server ~]# /etc/init.d/nfs start
[root@ server ~]# /etc/init.d/nfs stop
[root@ server ~]# /etc/init.d/nfs restart
If you want service remain in effect after rebooting, use commad [root@ server ~]# chkconfig nfs on

Configure NFS

First of all make the directory(s) that you want to share like this as shown in figure.
  1. I want to share /raj & /wisdom directories
    directories for NFS
    directories for NFS

  2. Grant the permission
    Permission for directories for NFS
    Permission for directories for NFS

  3. Configuration File for NFS
    NFS servers have only one configuration file: /etc/exports. This file lists the partitions that are sharable, the hosts they can be shared with, and with what permissions. Here is the format of each entry in the /etc/exports file:
    /wisdom 			client1(permissions) client2(permissions)
    • /wisdom is the directory you want to share with other users.
    • client1, client2 are the host names of the NFS clients. The wildcard characters * and ? may be used.
    • Permissions are the corresponding permissions for each client.(here permission not related to chmod)
  4. Permissions
    • ro Files are read-only. No files may be written to (or newly created) on this mount. This is the default
    • rw Allow both read and write requests to this volume.
In all cases, use the sync option to ensure that file data cached in memory is automatically written to the disk after the completion of any disk data copying operation. A backslash at the end of the line indicates that the entry continues on to the next line. Comments can be added by using a pound symbol (#) at the beginning of the line.

Understand NFS server by example step by step

Let us learn NFS working by example.

NFS at server side

Open the file /etc/exports and write as shown in figure.
NFS server exports file
NFS server exports file

/wisdom directory is shared by all clients with permission read only. /raj directory is shared by only network with permission read and write.
Restart the service by using command
[root@server ~]# service nfs restart
Now I made two files for sharing as shown in figure.
NFS server file sharing
NFS server file sharing

At client side NFS server

Client should be connected with server my client ip address is If client want to check the which directories have been shared.
Use command
[root@clientside ~ ] # showmount -e ipaddress of server
Below figure would clear all the doubts.
NFS server -client side
NFS server -client side

If you want to use means read and write the files as per your permissions First of all crate a directory then mount the server on that directory To mount the server on that directory, use command
[root@clientside ~]# mount -t nfs ipaddress of server:/exports directory /directory
NFS server mounting directory
NFS server mounting directory

This is showing that client create a directory /game and mount the export directory /raj on client directory /game. Check the file by using ls- l command.
NFS server Client permission
NFS server Client permission

If client has permission to write any file then he/she can create a file like this
NFS Client file creation
NFS Client file creation

If you want to unmount the directory
Leave the directory then use command.
[root@clientside ~] umount server ip address

After use you should always unmount the NFS server.

Mount NFS server at the time of booting

In this way you can use shared folder. But this share folder will be available till system is up. It will not be available after reboot. To keep it available after reboot make its entry in /etc/fstab
Open the file
Make an extra entry in file as shown in figure.
NFS server at the time of booting
NFS server at the time of booting

Red rectangle in figure is showing the new entry
First entry is ip address of server : export directory Second entry /exp is directory of client on which server would be mounted. Third entry nfs type it is like partition type. Third and fourth entry is same as above. It is not good idea to have kept server mounting so long. And also mount and umount the directory are become frustrating.

Using autofs to mount NFS file systems on demand

Recent improvements to auto-detecting and mounting removable devices have meant that you can simply insert or plug in those devices to have them detected, mounted, and displayed. However, to make the process of detecting and mounting remote NFS file systems more automatic, you still need to use a facility such as autofs. With the autofs facility configured and turned on, you can cause any NFS shared directories to mount on demand. If you know the hostname and directory being shared by another host computer, you can simply change (cd) to the autofs mount directory and have the shared resource automatically mount and be accessible to you.
The following steps explain how to turn on the autofs facility:
  1. For automount the server we have to make two directories. One is physical but other is virtual.
  2. Make a new directory
    [root@clientside ~]# mkdir /phy
  3. As root user from a Terminal window, open the /etc/auto.master file.
    Make entry like this as shown in figure.
    NFS server --/etc/auto.master
    NFS server --/etc/auto.master

    1. Define the directory.
    2. Define auto.misc file in which we define our virtual directory.
    3. Inactivity timeout before which auto mounted directories are silently unmounted.
  4. Open the /etc/auto.misc file. Add an extra line in the file as shown in the figure.
    NFS server --/etc/auto.misc
    NFS server --/etc/auto.misc

    1. vir is virtual directory its real path is /phy/vir
    2. -rw--> read and write , soft--> software , intr--> execute .
    3. export directory
Restart the service
[root@clientside ~] # service autofs restart
Check the directory like this as shown in figure.
Accessing NFS directory
Accessing NFS directory