Sunday, June 15, 2014

Setup and Configure NFS (Network File System) in RHEL6

NFS stands for "Network File System". It was developed by Sun Microsystems in 1980 to share Files and Printer between Unix or Linux systems. It allows a client to mount the local file system of server over network. By default RHEL6 use NFSv4 if the server supports it.

NFS Services

NFS allows local access to remote files. It uses PORT 2049 and nfs demon. In Network File System, there are  two packages required in RHEL6:
  1. nfs-utils
  2. rpcbind

Installation Of Required Packages

To install those packages run this command as root user:

# yum groupinstall "NFS file server"
OR
# yum install nfs*


If you have not configured yum server then you can install from the package directory by using rpm command as:

# rpm -ivh nfs* --nodeps --force
# rpm -ivh rpcbind* --nodeps --force


Starting the services 

To start the services use these commands as root user in both server and client:

# service nfs restart
# service rpcbind restart

(Note: In these commands restart is for restarting the services. It may possible that services are not started yet, So restart will not bother that services are running or not.)

Configuring the SERVER and CLIENTS

Suppose our server's ip address is 192.168.1.1 and our client's ip address is 192.168.1.2. Then here are the steps to configure the server and client for the NFS service

Server Configuration:

Suppose we want to share the directory "/home/user/nfs_share" for NFS service then follow these steps:
  1. "/etc/exports" is the file where we will type the share files information. Open editor with this file and add entry for the directory to be share in this format
    <Path_of_Directory_To_Share> <Client's_IP_address> <Permissions>
    in our example it will be:

    /home/user/nfs_share   192.168.1.2  (rw,sync)
  2. Save this file and and check with cat command as:

    # cat /etc/exports
    /home/user/nfs_share   192.168.1.2  (rw,sync)
    #

  3. Allow PORT 2049 (NFS4) from the firewall which is in menu:
    "System  >  Administration  >  Firewall"


    Or you can use "setup" command...
  4. Restart both of the service as:

    # service nfs restart
    # service nfs restart

  5. NFS Server is configured successfully.
Note: Common permissions used in "/etc/exports" file
rwread/write permissions
roSread-only permissions
insecureAllows the use of ports over 1024
syncSpecifies that all changes must be written to disk before a command completes
no_wdelayForces the writing of changes immediately
root_squashPrevents root users

Note:
Some valid host entries in "/etc/exports" file
/home/user/nfs_share  *  (rw,sync)
/home/user/nfs_share  *.example.com  (rw,sync)
/home/user/nfs_share   192.168.1.10  (rw,sync)
/home/user/nfs_share   192.168.1.0/255.255.255.0  (rw,sync)
/home/user/nfs_share   192.168.1.0/24  (rw,sync)
/home/user/nfs_share   @netgroup  (rw,sync)

Client Configuration

Server has shared "/home/user/nfs_share" directory, Now Client wants to access the directory then client should follow these simple steps:
  1. create a directory in /mnt folder as following command using root user:

    # mkdir /mnt/shared_files
  2. mount the nfs file system of server to this created directory as:

    # mount -t nfs 192.168.1.1:/home/user/nfs_share /mnt/shared_files
  3. goto "/mnt/shared_files" directory and perform list command. You will see all the file of server's "/home/user/nfs_share" in "/mnt/shared_files".

    # ls  -al  /mnt/shared_files
     
  4. Client is also configured successfully.
Note: To unmount the NFS from clients folder just execute this command
# umount  /mnt/shared_files

Testing NFS Service

If client has write permission then Just create a file in "/mnt/shared" directory from client's system and write something in it. You will see the file in Server's system. If client has read only permission then create file from server's system and try to read the file from client's system.

Important commands for NFS.

  • showmount -e : Shows the available shares on your local machine
  • showmount -e <server-ip or hostname>: 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

No comments:

Post a Comment