Saturday, 6 October 2012

Installing and configuring NFS on CentOS


Installing and Configuring NFS on CentOS 6.3


What is NFS ?

The Network File System (NFS) was developed to allow machines to mount a disk partition on a remote machine as if it were a local disk. It allows for fast, seamless sharing of files across a network. 

In this tutorial, we are going to set up a simple NFS Server and Client.

It is assumed that you will be setting up both a server and a client. If you are just setting up a client to work off of somebody else's server (say in your department), you can skip to  the Setting up NFS Client section below.

Setting up the server will be done in three steps
  • Installing the NFS packages on both the Server and Client
  • Setting up the configuration files for NFS
  • Starting the NFS services

Pre-requsites

I am using two machines for this demo purpose. One acting as a NFS Server (hostname: nfs-server) and one acting as my NFS Client (hostname: nfs-client)

It is important that you set the hostnames in both the Servers using the /etc/hosts file.

Edit the file and make the hosts entry in the following format:

<IP ADDRESS> <Hostname> <F.Q. Hostname>

NOTE: Do not provide spaces in between these parameters. Use Tabs. 

You don't need to have an actual Domain Server for FQDNs. Simply provide your own value, as I have done here (xxxxxxxxxx.cloud.com)

NFS on CentOS 6.3 - Editing the Host file

Repeat the same steps on the Client Machine (nfs-client) as well.

NFS on CentOS 6.3 - Editing the Host file


Setting up the NFS Server

Before you begin, make sure your Machine displays its hostname correctly as shown below. Note down the NFS Server machine's IP address too.

# hostname
# ipconfig

NFS on CentOS 6.3 - Checking for Hostname and IP Address

For the sake of this tutorial, I have added a new HDD and mounted it to a newly created folder named "nfs-mount-point". This folder will be presented to other machines (nfs-client) over the network as a NFS Share.

To know more on how to Add an External HDD and Format it on CentOS 6.3, refer THIS GUIDE

NFS on CentOS 6.3 - Checking the attached Hard Disk

We now install the required packages for NFS Service. 

NOTE: If you don't have internet connectivity and cannot download these packages through the Internet, you can create your own CentOS Local Repository by following this simple guide SHOWN HERE

Run the following command to get started:

# yum install nfs-utils

NFS on CentOS 6.3 - Installing Packages

Once the packages and dependencies have been installed successfully, you should see the confirmation as shown below:

NFS on CentOS 6.3 - Installing Packages

The next thing to do is add your Domain Name in one of the configuration files as shown below.

# /etc/idmapd.conf 

Edit line 5: uncomment and change to your domain name
Domain = cloud.com 
 
Save the file and exit it.


NFS on CentOS 6.3 - Editing the configuration file

The next file we need to edit is the exports file located in the /etc folder

This file contains a list of entries; each entry indicates a volume that is shared and how it is shared.

An entry in /etc/exports will typically look like this:

directory machine1(option11,option12)
machine2(option21,option22)

where;
directory

The directory that you want to share. It may be an entire volume though it need not be. If you share a directory, then all directories under it within the same file system will be shared as well.
 
machine1 and machine2
 
Client machines that will have access to the directory. The machines may be listed by their DNS address or their IP address (e.g., machine.company.com or 192.168.0.8 ).
 
optionxx
 
The option listing for each machine will describe what kind of access that machine will have. Important options are: 

  • ro: The directory is shared read only; the client machine will not be able to write it. This is the default.
  • rw: The client machine will have read and write access to the directory.
  • no_root_squash: By default, any file request made by user root on the client machine is treated as if it is made by user nobody on the server. (Exactly which UID the request is mapped to depends on the UID of user "nobody" on the server, not the client.) If no_root_squash is selected, then root on the client machine will have the same level of access to the files on the system as root on the server. This can have serious security implications, although it may be necessary if you want to perform any administrative work on the client machine that involves the exported directories. You should not specify this option without a good reason.
  • no_subtree_check: If only part of a volume is exported, a routine called subtree checking verifies that a file that is requested from the client is in the appropriate part of the volume. If the entire volume is exported, disabling this check will speed up transfers.
  • sync: By default, all but the most recent version (version 1.11) of the exportfs command will use async behavior, telling a client machine that a file write is complete - that is, has been written to stable storage - when NFS has finished handing the write over to the filesystem. This behavior may cause data corruption if the server reboots, and the sync option prevents this.
Edit the /etc/exports file as shown below *note
/nfs-mount-point *(rw,sync,no_root_squash,no_all_squash)
 
*note
/nfs-mount-point
⇒ shared directory
* ⇒All IP ranges permitted (THIS IS NOT A SECURE WAY TO RUN YOUR NFS. NOT RECOMMENDED FOR PRODUCTION ENVIRONMENTS)
sync ⇒ synchronize
no_root_squash ⇒ enable root privilege
no_all_squash ⇒ enable users' authority
 
Once done, save and exit the file.
  
NFS on CentOS 6.3 - Editing the Configuration file

The NFS server should now be configured and we can start it running. Run the following commands to get the nfs service started

# /etc/rc.d/init.d/rpcbind start

# /etc/rc.d/init.d/nfslock start

# /etc/rc.d/init.d/nfs start 
 
NFS on CentOS 6.3 - Starting the Services

Enable these services to be ON always in all run levels (NOT RECOMENDED IN PRODUCTION ENV)

# chkconfig rpcbind on

# chkconfig nfslock on

# chkconfig nfs on 

NFS on CentOS 6.3 - Starting the Services


Setting up the NFS Client

Before you begin, make sure your Machine displays its hostname correctly as shown below. Note down the NFS Server machine's IP address too.

# hostname
# ipconfig

NFS on CentOS 6.3 - Checking for Hostname and IP Address

We now install the required packages for NFS Service. 

NOTE: If you don't have internet connectivity and cannot download these packages through the Internet, you can create your own CentOS Local Repository by following this simple guide SHOWN HERE

Run the following command to get started:

# yum install nfs-utils

NFS on CentOS 6.3 - Installing Packages

Once the packages and dependencies have been installed successfully, you should see the confirmation as shown below: 

NFS on CentOS 6.3 - Installing Packages

The next thing to do is add your Domain Name in one of the configuration files as shown below.

# /etc/idmapd.conf 

Edit line 5: uncomment and change to your domain name
Domain = cloud.com 
 
NOTE: Provide the same DOMAIN NAME as you did while setting the NFS Server
 
Save the file and exit it.

NFS on CentOS 6.3 - Editing Configuration File

Run the following commands to get the nfs service started on the client machine:

# /etc/rc.d/init.d/rpcbind start 
 
# /etc/rc.d/init.d/rpcidmapd start 

# /etc/rc.d/init.d/nfslock start

# /etc/rc.d/init.d/nfs start

NFS on CentOS 6.3 - Starting the Services


Enable these services to be ON always in all run levels (NOT RECOMENDED IN PRODUCTION ENV)

# chkconfig rpcbind on 

# chkconfig rpcidmapd on

# chkconfig nfslock on

# chkconfig nfs on

NFS on CentOS 6.3 - Starting the Services

Test the NFS Connectivity

In my client machine (nfs-client), create a new folder where you wnat to mount the NFS Share. In my case, i created a folder named "test-mount-point"

The next step is to mount the NFS Share we created in our NFS Server to our Mount Point, test-mount-point. To do so, type the following command:

# mount -t nfsnfs-server.cloud.com:/nfs-mount-point /test-mount-point

You can get un-mount the file system by typing:  

# umount /test-mount-point

Just like you would for a local file system.

NFS on CentOS 6.3 - Mounting the NFS Share

NFS file systems can be added to your /etc/fstab file the same way local file systems can, so that they mount when your system starts up. The only difference is that the file system type will be set to nfs and the dump and fsck order (the last two entries) will have to be set to one. So for our example above, the entry in /etc/fstab would look like: 

NFS on CentOS 6.3 -Creating an entry in fstab configuration file

At this point you should have NFS working, though a few tweaks may still be necessary to get it to work well.

Finally, create a simple file in either your NFS Server or your NFS Client to test whether the NFS share was configured properly or not.

NFS on CentOS 6.3 - Testing NFS Share

You should see the same file in your NFS share on both the machines as shown below

Testing NFS Share

That's it for this tutorial.. Stay tuned for much more coming your way !!

 

No comments :

Post a Comment