nfs:

 Translates remote file sharing requests into requests on the local file system.

rpc.mountd:

This service is responsible for mounting and unmounting of file systems.

 

Files for NFS Configuration

/etc/exports :              Its a main configuration file of NFS, all exported files and directories are defined in this file at the NFS Server end. /etc/fstab :                  To mount a NFS directory on your system across the reboots, we need to make an entry in /etc/fstab. /etc/sysconfig/nfs : Configuration file of NFS to control on which port rpc and other services are listening.

 

System Setup

To setup 

NFS mounts, we’ll be needing two Linux/Unix machines:

  • NFS Server: server1.example.com with IP-192.168.122.50
  • NFS Client : tester1.example.com with IP-192.168.122.100
  1. Installing NFS Server and NFS Client.

    Install NFSpackages on NFS Server as well as on NFS Client
# yum install nfs-utils nfs-utils-lib # yum install portmap (not required with NFSv4) # apt-get install nfs-utils nfs-utils-lib
  1. Now start the services on both machines.

# /etc/init.d/portmap start # /etc/init.d/nfs start # chkconfig --level 35 portmap on # chkconfig --level 35 nfs on

After installing packages and starting services on both the machines, we need to configure both the machines for file sharing.

  1. Setting Up the NFS Server

First we will be configuring the NFS server.

  1. Configure Export directory

For sharing a directory with NFS, we need to make an entry in “/etc/exports” configuration file. Here I’ll be creating a new directory named “nfsshare” in “/” partition to share with client server, you can also share an already existing directory with NFS.

# mkdir /nfsshare

Now we need to make an entry in “/etc/exports” and restart the services to make our directory shareable in the network.

# vi /etc/exports /nfsshare 192.168.122.100(rw,sync,no_root_squash)

In the above example, there is a directory in / partition named “nfsshare” is being shared with client IP “192.168.122.100” with read and write (rw) privilege, you can also use hostname of the client in the place of IP in above example.

[the_ad id="2469"]

  1. NFS Options

Some other options we can use in “/etc/exports” file for file sharing is as follows.

    • ro: With the help of this option we can provide read only accessto the shared files i.e client will only be able to read.
    • rw: This option allows the client serverto both read and write access within the shared directory.
    • sync: Sync confirms requests to the shared directory only once the changeshave been committed.
    • no_subtree_check: This option prevents the subtree When a shared directory is the subdirectory of a larger file system, nfsperforms scans of every directory above it, in order to verify its permissions and details. Disabling the subtree check may increase the reliability of NFS, but reduce security.
    • no_root_squash: This phrase allows rootto connect to the designated directory.

For more options with “/etc/exports“, you are recommended to read the man pages for export.

  1. Setting Up the NFS Client

After configuring the NFS server, we need to mount that shared directory or partition in the client server.

  1. Mount Shared Directories on NFS Client

Now at the NFS client end, we need to mount that directory in our server to access it locally. To do so, first we need to find out that shares available on the remote server or NFS Server.

# showmount -e 192.168.122.50 Export list for 192.168.122.50: /nfsshare 192.168.122.100

Above command shows that a directory named “nfsshare” is available at “192.168.122.50” to share with your server.

  1. Mount Shared NFS Directory

To mount that shared NFS directory we can use following mount command.

[# mount -t nfs 192.168.122.50:/nfsshare /mnt/nfsshare

The above command will mount that shared directory in “/mnt/nfsshare” on the client server. You can verify it following command.

[# mount | grep nfs sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) nfsd on /proc/fs/nfsd type nfsd (rw) 192.168.122.50:/nfsshare on /mnt type nfs (rw,addr=192.168.122.50)

The above mount command mounted the nfs shared directory on to nfs client temporarily, to mount an NFS directory permanently on your system across the reboots, we need to make an entry in “/etc/fstab“.

# vi /etc/fstab

Add the following new line as shown below.

192.168.122.50:/nfsshare /mnt  nfs defaults 0 0
  1. Test the Working of NFS Setup

We can test our NFS server setup by creating a test file on the server end and check its availability at nfs clientside or vice-versa.

  1. At the server1, create a new text file named “nfstest.txt’ in that shared directory.
# cat > /nfsshare/nfstest.txt

This is a test file to test the working of NFS server setup.

  1. At the tester1 go to that shared directory in client serverand you’ll find that shared file without any manual refresh or service restart.
# ll /mnt/nfsshare total 4 -rw-r--r-- 1 root root 61 Sep 21 21:44 nfstest.txt # cat /mnt/nfsshare/nfstest.txt

This is a test file to test the working of NFS server setup.

  1. Removing the NFS Mount

If you want to unmount that shared directory from your server after you are done with the file sharing, you can simply unmount that particular directory with “umount” command. See this example below.

# umount /mnt/nfsshare

You can see that the mounts were removed by then looking at the filesystem again.

# df -h -F nfs

You’ll see that those shared directories are not available any more.

  1. Important commands for NFS

Some more important commands for NFS.

    • showmount -e: Shows the available shares on your local machine
    • showmount -e: Lists the available shares at the remote server
    • showmount -d: Lists all the sub directories
    • exportfs -v: Displays a list of shares files and options on a server
    • exportfs -a: Exports all shares listed in /etc/exports, or given name
    • exportfs -u: Unexports all shares listed in /etc/exports, or given name
    • exportfs -r: Refresh the server’s list after modifying /etc/exports