Tuesday, October 4, 2011

Linux NFS-HOWTO share folder from one linux server to other servers

My web site : Info Tech Vedas

Contents

1. Introduction. 2
2. What is NFS?. 2
3. Software Pre-Requisites: Kernel Version and nfs-utils. 2
4. Important NFS Daemons. 4
5. Installing NFS.. 4
6. Configuring NFS on The Server. 5
    The /etc/exports File
    Starting NFS on the Server. 6
7. Configuring NFS on The Client
   Starting NFS on the Client
8. Accessing NFS Server Directories from the Client
    The /etc/fstab File
    Table 1. Possible NFS Mount Options
    Permanently Mounting The NFS Directory. 10

1. Introduction

URL: http://nfs.sourceforge.net/nfs-howto/ar01s03.html
URL: http://www.linuxhomenetworking.com/wiki/index.php

2. 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.
It also gives the potential for unwanted people to access your hard drive over the network (and thereby possibly read your email and delete all your files as well as break into your system) if you set it up incorrectly. So please read Section 6, “Security and NFS” of this document carefully if you intend to implement an NFS setup.
There are other systems that provide similar functionality to NFS. Samba (http://www.samba.org) provides file services to Windows clients. The Andrew File System, originally developed by IBM (http://www.openafs.org) and now open-source, provides a file sharing mechanism with some additional security and performance features. The Coda File System (http://www.coda.cs.cmu.edu/) combines file sharing with a specific focus on disconnected clients. Many of the features of the Andrew and Coda file systems are slated for inclusion in the next version of NFS (Version 4) (http://www.nfsv4.org). The advantage of NFS today is that it is mature, standard, well understood, and supported robustly across a variety of platforms.

3. Software Pre-Requisites: Kernel Version and nfs-utils

The differences between Version 2 NFS, Version 3 NFS, and Version 4 NFS will be explained later on; for now, you might simply take the suggestion that you will need NFS Version 3 if you are installing a dedicated, high-volume, or production file server. NFS Version 2 or 4 should be fine for casual use.
NFS Version 2 has been around for quite some time now (at least since the 1.2 kernel series) however you will need a kernel version of at least 2.2.18 if you wish to do any of the following:
Mix Linux NFS with other operating systems' NFS
Use file locking reliably over NFS
Use NFS Version 3
There are also patches available for kernel versions above 2.2.14 that provide the above functionality. Some of them can be downloaded from the Linux NFS home page. If your kernel version is 2.2.14 - 2.2.17 and you have the source code on hand, you can tell if these patches have been added because NFS Version 3 server support will be a configuration option. However, unless you have some particular reason to use an older kernel, you should upgrade because many bugs have been fixed along the way. Kernel 2.2.19 contains some additional locking improvements over 2.2.18.
Version 3 functionality will also require the nfs-utils package of at least version 0.1.6, and mount version 2.10m or newer. However because nfs-utils and mount are fully backwards compatible, and because newer versions have lots of security and bug fixes, there is no good reason not to install the newest nfs-utils and mount packages if you are beginning an NFS setup.
All 2.4 and higher kernels have full NFS Version 3 functionality. All kernels 2.6 and higher have varying levels of NFS Version 4 functionality. This will be discussed in more detail later on.
In all cases, if you are building your own kernel, you will need to select NFS and the NFS versions support that you want to deploy. Nowadays almost every standard distribution of Linux provides a kernel that supports NFS version 3.
Handling files larger than 2 GB will require a 2.4x kernel or greater and a 2.2.x version or greater of glibc.
All kernels after 2.2.18 support NFS over TCP on the client side. Server-side NFS over TCP only exists in a buggy form as an experimental option in the post-2.2.18 series; patches for 2.4 and 2.5 kernels have been introduced starting with 2.4.17 and 2.5.6. Use of TCP in NFS is\ now mainstream and receives widespread use.
Because so many of the above functionalities were introduced in kernel version 2.2.18, this document was written to be consistent with kernels above this version (including 2.6.x). If you have an older kernel, this document may not describe your NFS system correctly.
As we write this document NFS version 4 support is under active development in the 2.6.x kernel. Its implementation is still very fluid and changing, and it is not recommended for production use. Configuration of NFS version 4 and associated integrated technologies are discussed later in this document.

4. Important NFS Daemons

NFS isn't a single program, but a suite of interrelated programs that work together to get the job done.
 rpcbind: (portmap in older versions of Linux, rpc.portmap) The primary daemon upon which all the others rely, rpcbind manages connections for applications that use the RPC specification. By default, rpcbind listens to TCP port 111 on which an initial connection is made. This is then used to negotiate a range of TCP ports, usually above port 1024, to be used for subsequent data transfers. You need to run rpcbind on both the NFS server and client.
 nfs: Starts the RPC processes needed to serve shared NFS file systems. The nfs daemon needs to be run on the NFS server only.
 nfslock: Used to allow NFS clients to lock files on the server via RPC processes. The nfslock daemon needs to be run on both the NFS server and client.
netfs: Allows RPC processes run on NFS clients to mount NFS filesystems on the server. The netfs daemon needs to be run on the NFS client only.
Now take a look at how to configure these daemons to create functional NFS client/server peering.

5. Installing NFS

Search for :
rpm -qa | grep nfs
Installation using yum:
yum install nfs-utils nfs-utils-lib portmap system-config-nfs

6. Configuring NFS on The Server

i.e.
vi /etc/exports
/var/www/html/bollywoodji/fdfolder 192.168.0.13(ro,sync)
Here “/var/www/html/bollywoodji/fdfolder” is source to share
Share on IP address with options “192.168.0.13(ro,sync)” to target IP
Both the NFS server and NFS client have to have parts of the NFS package installed and running. The server needs rpcbind, nfs, and nfslock operational, as well as a correctly configured /etc/exports file. Here's how to do it.

The /etc/exports File

The /etc/exports file is the main NFS configuration file, and it consists of two columns. The first column lists the directories you want to make available to the network. The second column has two parts. The first part lists the networks or DNS domains that can get access to the directory, and the second part lists NFS options in brackets.
For the scenario you need:
§ Read-only access to the /data/files directory to all networks
§ Read/write access to the /home directory from all servers on the 192.168.1.0 /24 network, which is all addresses from 192.168.1.0 to 192.168.1.255
§ Read/write access to the /data/test directory from servers in the my-site.com DNS domain
§ Read/write access to the /data/database directory from a single server 192.168.1.203.
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.
#/etc/exports
/data/files *(ro,sync)
/home 192.168.1.0/24(rw,sync)
/data/test *.my-site.com(rw,sync)
/data/database 192.168.1.203/32(rw,sync)
After configuring your /etc/exports file, you need to activate the settings, but first make sure that NFS is running correctly.

Starting NFS on the Server

Configuring an NFS server is straightforward:
1) Use the chkconfig command to configure the required nfs and RPC rpcbind daemons to start at boot. You also should activate NFS file locking to reduce the risk of corrupted data.
[root@bigboy tmp]# chkconfig --level 35 nfs on
[root@bigboy tmp]# chkconfig --level 35 nfslock on 
[root@bigboy tmp]# chkconfig --level 35 rpcbind on
2) Use the init scripts in the /etc/init.d directory to start the nfs and RPC rpcbind daemons. The examples use the start option, but when needed, you can also stop and restart the processes with the stop and restart options.
[root@bigboy tmp]# service rpcbind start or service portmap  start 
          or service rpc.portmap start
[root@bigboy tmp]# service nfs start
[root@bigboy tmp]# service nfslock start
3) Test whether NFS is running correctly with the rpcinfo command. You should get a listing of running RPC programs that must include mountd, portmapper, nfs, and nlockmgr.
[root@bigboy tmp]# rpcinfo -p localhost
   program vers proto   port
    100000    2   tcp    111  portmapper
    100000    2   udp    111  portmapper
    100003    2   udp   2049  nfs
    100003    3   udp   2049  nfs
    100021    1   udp   1024  nlockmgr
    100021    3   udp   1024  nlockmgr
    100021    4   udp   1024  nlockmgr
    100005    1   udp   1042  mountd
    100005    1   tcp   2342  mountd
    100005    2   udp   1042  mountd
    100005    2   tcp   2342  mountd
    100005    3   udp   1042  mountd
    100005    3   tcp   2342  mountd

7. Configuring NFS on The Client

NFS configuration on the client requires you to start the NFS application; create a directory on which to mount the NFS server's directories that you exported via the /etc/exports file, and finally to mount the NFS server's directory on your local directory, or mount point. Here's how to do it all.

Starting NFS on the Client

Three more steps easily configure NFS on the client.
1) Use the chkconfig command to configure the required nfs and RPC rpcbind daemons to start at boot. Activate nfslock to lock the files and reduce the risk of corrupted data.
[root@smallfry tmp]# chkconfig --level 35 netfs on
[root@smallfry tmp]# chkconfig --level 35 nfslock on
[root@smallfry tmp]# chkconfig --level 35 rpcbind on
2) Use the init scripts in the /etc/init.d directory to start the nfs and RPC rpcbind daemons. As on the server, the examples use the start option, but you can also stop and restart the processes with the stop and restart options.
[root@smallfry tmp]# service rpcbind start
[root@smallfry tmp]# service netfs start
[root@smallfry tmp]# service nfslock start
3) Test whether NFS is running correctly with the rpcinfo command. The listing of running RPC programs you get must include status, portmapper, and nlockmgr.
[root@smallfry root]# rpcinfo -p
   program vers proto   port
    100000    2   tcp    111  portmapper
    100000    2   udp    111  portmapper
    100024    1   udp  32768  status
    100024    1   tcp  32768  status
    100021    1   udp  32769  nlockmgr
    100021    3   udp  32769  nlockmgr
    100021    4   udp  32769  nlockmgr
    100021    1   tcp  32769  nlockmgr
    100021    3   tcp  32769  nlockmgr
    100021    4   tcp  32769  nlockmgr
    391002    2   tcp  32770  sgi_fam

8. Accessing NFS Server Directories from the Client

i.e.
vi /etc/fstab
 
192.168.0.110:/var/www/html/bollywoodji/fdfolder 
 /var/www/html/bollywoodji/fdfolder nfs 
 rsize=8192,wsize=8192,timeo=110,intr 0 0 
 
Restart:
/etc/init.d/portmap restart  or  /etc/init.d/rpcbind restart
/etc/init.d/nfslock restart
/etc/init.d/nfs restart
 
cd /var/www/html/bollywoodji/
Make folder fdfolder 
mount -a
Note: Please make folder before edit “fstab”
In most cases, users want their NFS directories to be permanently mounted. This requires an entry in the /etc/fstab file in addition to the creation of the mount point directory.

The /etc/fstab File

The /etc/fstab file lists all the partitions that need to be auto-mounted when the system boots. Therefore, you need to edit the /etc/fstab file if you need the NFS directory to be made permanently available to users on the NFS. For the example, mount the /data/files directory on server bigboy (IP address 192.16801.100) as an NFS-type filesystem using the local /mnt/nfs mount point directory.
#/etc/fstab
#Directory                   Mount Point    Type  Options       Dump FSCK
192.168.1.100:/data/files   /mnt/nfs        nfs  soft,nfsvers=2  0   0
This example used the soft and nfsvers options; Table 29.1 outlines these and other useful NFS mounting options you may want to use. See the NFS man pages for more details.

Table 1. Possible NFS Mount Options

Option
Description
bg
Retry mounting in the background if mounting initially fails
fg
Mount in the foreground
soft
Use soft mounting
hard
Use hard mounting
rsize=n
The amount of data NFS will attempt to access per read operation. The default is dependent on the kernel. For NFS version 2, set it to 8192 to assure maximum throughput.
wsize=n
The amount of data NFS will attempt to access per write operation. The default is dependent on the kernel. For NFS version 2, set it to 8192 to assure maximum throughput.
nfsvers=n
The version of NFS the mount command should attempt to use
tcp
Attempt to mount the filesystem using TCP packets: the default is UDP.
intr
If the filesystem is hard mounted and the mount times out, allow for the process to be aborted using the usual methods such as CTRL-C and the kill command.

The steps to mount the directory are fairly simple, as you'll see.

Permanently Mounting The NFS Directory

You'll now create a mount point directory, /mnt/nfs, on which to mount the remote NFS directory and then use the mount -a command activate the mount. Notice how before mounting there were no files visible in the /mnt/nfs directory, this changes after the mounting is completed:
[root@smallfry tmp]# mkdir /mnt/nfs
[root@smallfry tmp]# ls /mnt/nfs
[root@smallfry tmp]# mount -a
[root@smallfry tmp]# ls /mnt/nfs
ISO  ISO-RedHat  kickstart  RedHat
[root@smallfry tmp]#
Each time your system boots, it reads the /etc/fstab file and executes the mount -a command, thereby making this a permanent NFS mount.

1 comment: