Mounting a NFS partition

NFS is network file system. It is a type of partition which allows you to store off your data into different physical computers which are connected via LAN. This is what adds to the appeal of NFS and makes it so different from other FS types. It allows the system administrator by availing the same FS to be mounted off in different machines.

So you can see why NFS is so cool. Just imagine having your same home directory on all of your machines , and that too without needing extra disk space !

Setting up NFS is pretty easy too. To setup up NFS you need atleast one server and one client. I used Fedora 20, as my server and a CentOS 6.5 based VM as my client. to be honest it took me nearly 5 hours to configure NFS on these machines.

First of all you need to install the packages  for NFS on both of the machines. To do that you would execute

sudo yum install nfs-utils nfs-utils-lib
sudo yum install portmap (not required with NFSv4)

Now start the services by:-

service nfs start
service rpcbind start

You might be thinking why do we  need to start the RPC (Remote Procedure Call) service ? Its because its the backbone of NFS suite. All of its services are provided via the help of RPC.

Setting up the NFS server:

After that you need to edit your /etc/exports file on your server. This file contains the information about what directories the server will share via NFS and with whom it will share. You need to be the super user to edit the exports file.

The syntax of an exports file entry looks like this

directory client-ip(Permissions)
Example: /home/xxx/music 172.164.145.555(rw,no\_root\_squash,sync)

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

NFS Options

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

  1. ro: With the help of this option we can provide read only access to the shared files i.e client will only be able to read.

  2. rw: This option allows the client server to both read and write access within the shared directory.

  3. sync: Sync confirms requests to the shared directory only once the changes have been committed.

  4. no_subtree_check: This option prevents the subtree checking. When a shared directory is the subdirectory of a larger file system, nfs performs 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.

  5. no_root_squash: This phrase allows root to connect to the designated directory. Be careful though, sometimes you might not want to give root access.

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

Now, if your server is a fedora machine then you also need to follow these additional steps:

  • Add the list of allowed clients in your /etc/hosts.allow file. The syntax for addition in this file is:
    name-of-service clients_allowed_ip

    In my configuration I had the following entries

    portmap: ALL  
    rpc.mountd: ALL  
    rpc.nfsd: ALL
    

    Here the ALL keyword indicates that everyone can access these services.

  • After setting up the hosts.allow file. We need to configure nfs config file stored in /etc/sysconfig/nfs
    In this config files we need to explicitely specify what ports will be used in NFS.

All in all, your nfs config file must have the following entries (uncommented)

    MOUNTD_PORT=892               
    STATD_PORT=662                          
    STATD_OUTGOING_PORT=2020  
    LOCKD_TCPPORT=32803              
    LOCKD_UDPPORT=32769
  • Now we need to deal with the overhelpful firewall of fedora. We need to adjust the iptables such that the ports used in NFS are open.

To do that, fire up your firewall GUI daemon, and add ports 2049(NFS) and 111(RPC) as open ports. You need to open these ports to TCP as well as UDP protocols.

After this we need to edit the iptables file. Generally its found in /etc/sysconfig/

Add the following entries to your iptables file:

   -A INPUT -m state --state NEW -m udp -p udp --dport 2049 -j ACCEPT     
   -A INPUT -m state --state NEW -m tcp -p tcp --dport 2049 -j ACCEPT  
   -A INPUT -m state --state NEW -m udp -p udp --dport 111 -j ACCEPT 
   -A INPUT -m state --state NEW -m tcp -p tcp --dport 111 -j ACCEPT   
   -A INPUT -m state --state NEW -m udp -p udp --dport 892 -j ACCEPT       
   -A INPUT -m state --state NEW -m tcp -p tcp --dport 892 -j ACCEPT         
   -A INPUT -m state --state NEW -m udp -p udp --dport 875 -j ACCEPT       
   -A INPUT -m state --state NEW -m tcp -p tcp --dport 875 -j ACCEPT         
   -A INPUT -m state --state NEW -m udp -p udp --dport 662 -j ACCEPT       
   -A INPUT -m state --state NEW -m tcp -p tcp --dport 662 -j ACCEPT 
   -A INPUT -m state --state NEW -m tcp -p tcp --dport 32803 -j ACCEPT     
   -A INPUT -m state --state NEW -m udp -p udp --dport 32769 -j ACCEPT
  • Now restart your firewall by:
    service iptables restart

    And we’re done ! , With the Server configuration of course :P

Setting up the NFS client

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

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.0.100 Export list for 192.168.0.100:/nfsshare 192.168.0.101

Now, create the directory that will contain the NFS shared files

mkdir -p /mnt/nfs/home

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.0.100
>>Export list for 192.168.0.100:/nfsshare 192.168.0.101

Then go ahead and mount it:

mount <ip-address_of_host>:<directory_in_exportsfile> <where_to_mount>
Example: mount 12.34.56.789:/home /mnt/nfs/home

If you are having problems then try stopping your firewall daemon by using

service iptables stop

If all goes well then you will be able to access all the information stored in your server.

Comments