create NFS using nfs-utils code example

Example: create NFS using nfs-utils

#The system should be set up as root. You can access the root user by typing
sudo su
#Setting Up the NFS Server
#Step One—Download the Required Software
#Start off by using apt-get to install the nfs programs.
yum install nfs-utils nfs-utils-lib
#Subsequently, run several startup scripts for the NFS server:
chkconfig nfs on 
service rpcbind start
service nfs start
#Step Two—Export the Shared Directory
#The next step is to decide which directory we want to share with the client server. The chosen directory should then be added to the /etc/exports file, which specifies both the directory to be shared and the details of how it is shared.

#Suppose we wanted to share the directory, /home.

#We need to export the directory:

vi /etc/exports
#Add the following lines to the bottom of the file, sharing the directory with the client:

/home           12.33.44.555(rw,sync,no_root_squash,no_subtree_check)
#These settings accomplish several tasks:

#rw: This option allows the client server to both read and write within the shared directory
#sync: Sync confirms requests to the shared directory only once the changes have been committed.
#no_subtree_check: This option prevents the subtree checking. When a shared directory is the subdirectory of a larger filesystem, 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.
#no_root_squash: This phrase allows root to connect to the designated directory
#Once you have entered in the settings for each directory, run the following command to export them:

exportfs -a
#Setting Up the NFS Client
#Step One—Download the Required Software
#Start off by using apt-get to install the nfs programs.

yum install nfs-utils nfs-utils-lib
#Step Two—Mount the Directories
#Once the programs have been downloaded to the the client server, create the directory that will contain the NFS shared files

mkdir -p /mnt/nfs/home
#Then go ahead and mount it

mount 12.34.56.789:/home /mnt/nfs/home
#You can use the df -h command to check that the directory has been mounted. You will see it last on the list.

df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda               20G  783M   18G   5% /
12.34.56.789:/home       20G  785M   18G   5% /mnt/nfs/home
#Additionally, use the mount command to see the entire list of mounted file systems.

mount
#Your list should look something like this:

/dev/sda on / type ext4 (rw,errors=remount-ro)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
12.34.56.789:/home on /mnt/nfs/home type nfs (rw,noatime,nolock,bg,nfsvers=2,intr,tcp,actimeo=1800,addr=12.34.56.789)
#Testing the NFS Mount
#Once you have successfully mounted your NFS directory, you can test that it works by creating a file on the Client and checking its availability on the Server.

#Create a file in the directory to try it out:

touch /mnt/nfs/home/example
#You should then be able to find the files on the Server in the /home.

ls /home
#You can ensure that the mount is always active by adding the directory to the fstab file on the client. This will ensure that the mount starts up after the server reboots.

vi /etc/fstab
12.34.56.789:/home  /mnt/nfs/home   nfs      auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0
#You can learn more about the fstab options by typing in:

man nfs
#After any subsequent server reboots, you can use a single command to mount directories specified in the fstab file:

mount -a
#You can check the mounted directories with the two earlier commands:

df -h
mount
#Removing the NFS Mount
#Should you decide to remove a directory, you can unmount it using the umount command:

cd
sudo umount /directory name
#You can see that the mounts were removed by then looking at the filesystem again.

df -h
#You should find your selected mounted directory gone.