Now Reading
How to Setup Standard Linux FS & Configure NFSv4 Server
0

How to Setup Standard Linux FS & Configure NFSv4 Server

To Set up Standard Linux File Systems and Configure NFSv4 Server

In this article we are going to discuss how to set up Standard Linux File Systems and configure NFSv4 Server. NFS Server will provides a service to share files over a network to Linux Clients.

Before going to configure NFS server, just make sure the NFS server is up and running.

Do the following process once the NFS server is set,

→ To configure NFS server to share a local directory over a network.
→ Automatically Mount those network shares in clients, either via the /etc/fstab file or autofs.

Server and Client Configuration

Before going to configure NFS server note the IP for Server and Client that we are going to use here,

Server IP : 192.168.5.88
Clinet IP : 192.168.5.89

To install package for NFS

To set up NFS server we need to install the NFS server package. The package may differ depending upon your linux distribution. For RedHat based distributions, run the below command to install the NFS packages.

[root@linuxhelp ~]# yum install nfs* -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> Running transaction check
---> Package nfs-utils.x86_64 1:1.3.0-0.el7 will be installed
---> Package nfs4-acl-tools.x86_64 0:0.3.3-13.el7 will be installed
---> Package nfsometer.noarch 0:1.7-0.el7 will be installed
---> Package nfstest.noarch 0:1.0.2-2.el7 will be installed
.
.
.                                                                                              2/4 
  Verifying  : nfs4-acl-tools-0.3.3-13.el7.x86_64                                                                                       3/4 
  Verifying  : nfsometer-1.7-0.el7.noarch                                                                                               4/4 

Installed:
  nfs-utils.x86_64 1:1.3.0-0.el7    nfs4-acl-tools.x86_64 0:0.3.3-13.el7    nfsometer.noarch 0:1.7-0.el7    nfstest.noarch 0:1.0.2-2.el7   

Complete!

Now the installation is successfully completed.

To Export NFS Network Shares

The /etc/exports file is used for exporting the NFS share to remote hosts. Mention the absolute path for the exporting NFS share to our clients with client IP address or hostname with permissions. We are going to share the /nfsshare directory, so its ownership permission should be set to nfsnobody to make our shares writable for our clients.

[root@linuxhelp ~]# ls -l /
total 32
lrwxrwxrwx.   1 root root    7 Apr 26  2016 bin -> usr/bin
dr-xr-xr-x.   4 root root 4096 Apr 26 14:33 boot
drwxr-xr-x.  19 root root 3260 Apr 26 14:33 dev
drwxr-xr-x. 128 root root 8192 Apr 26 16:46 etc
drwxr-xr-x.   3 root root   18 Apr 26  2016 home
lrwxrwxrwx.   1 root root    7 Apr 26  2016 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Apr 26  2016 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Jun 10  2014 media
drwxr-xr-x.   2 root root    6 Jun 10  2014 mnt
drwxr-xr-x.   2 root root    6 Apr 26 16:48 nfsshare
drwxr-xr-x.   3 root root   15 Apr 26  2016 opt
dr-xr-xr-x. 290 root root    0 Apr 26  2016 proc
dr-xr-x---.  16 root root 4096 Apr 26 16:12 root
drwxr-xr-x.  37 root root 1180 Apr 26 16:46 run
lrwxrwxrwx.   1 root root    8 Apr 26  2016 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Jun 10  2014 srv
dr-xr-xr-x.  13 root root    0 Apr 26  2016 sys
drwxrwxrwt.  18 root root 4096 Apr 26 16:47 tmp
drwxr-xr-x.  13 root root 4096 Apr 26  2016 usr
drwxr-xr-x.  22 root root 4096 Apr 26  2016 var
[root@linuxhelp ~]# chown nfsnobody: /nfsshare/
[root@linuxhelp ~]# ls -l /
total 32
lrwxrwxrwx.   1 root      root         7 Apr 26  2016 bin -> usr/bin
dr-xr-xr-x.   4 root      root      4096 Apr 26 14:33 boot
drwxr-xr-x.  19 root      root      3260 Apr 26 14:33 dev
drwxr-xr-x. 128 root      root      8192 Apr 26 16:46 etc
drwxr-xr-x.   3 root      root        18 Apr 26  2016 home
lrwxrwxrwx.   1 root      root         7 Apr 26  2016 lib -> usr/lib
lrwxrwxrwx.   1 root      root         9 Apr 26  2016 lib64 -> usr/lib64
drwxr-xr-x.   2 root      root         6 Jun 10  2014 media
drwxr-xr-x.   2 root      root         6 Jun 10  2014 mnt
drwxr-xr-x.   2 nfsnobody nfsnobody    6 Apr 26 16:48 nfsshare
drwxr-xr-x.   3 root      root        15 Apr 26  2016 opt
dr-xr-xr-x. 290 root      root         0 Apr 26  2016 proc
dr-xr-x---.  16 root      root      4096 Apr 26 16:12 root
drwxr-xr-x.  37 root      root      1180 Apr 26 16:46 run
lrwxrwxrwx.   1 root      root         8 Apr 26  2016 sbin -> usr/sbin
drwxr-xr-x.   2 root      root         6 Jun 10  2014 srv
dr-xr-xr-x.  13 root      root         0 Apr 26  2016 sys
drwxrwxrwt.  18 root      root      4096 Apr 26 16:58 tmp
drwxr-xr-x.  13 root      root      4096 Apr 26  2016 usr
drwxr-xr-x.  22 root      root      4096 Apr 26  2016 var
[root@linuxhelp ~]# vim /etc/exports
/nfsshare	192.168.5.89(rw,sync,no_root_squash,no_subtree_check)

After exporting the share we need to reload and update the exports share to our kernel. To do that run the below commands. To view the exports share use showmount or exportfs command.

[root@linuxhelp ~]# exportfs -r
[root@linuxhelp ~]# exportfs -a
[root@linuxhelp ~]# exportfs -s
/nfsshare  192.168.5.89(rw,wdelay,root_squash,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)

A list of most-frequent options used while exporting the NFS share:

1. ro : read-only, Remote clients can mount the exported file with read permissions only.
2. rw : read-write, it allows remote hosts to make write changes in the exported file.
3. root_squash : prevents remote root users from privileges in the server and allots them the user ID. If you like to “squash” all then use the option all_squash.
4. wdelay : write delay, NFS server delays committing changes to disk if it guess another related write request is imminent.
5. subtree_check : option verifies that a requested file is located in that exported subdirectoryif not disabling this option with no_subtree_check will fast up transfers. 8. fsid=0 | root denotes that the specified file is the root of multiple exported directories.
6. anonuid / anongid : is explicitly sets the UID and GID.
7. sync : represents the server replies to requests after changes have been committed to the hard disk.

To Open Firewall for NFS shares

Now its time to open our firewall for NFS server to allow our clients to view and mount our exports share. By opening the firewall for rpc-bind service, our clients will be able to view the available share by showmount command and mountd is a daemon for remotely mount our share to clients.

[root@linuxhelp ~]# firewall-cmd --permanent --add-service=nfs
success
[root@linuxhelp ~]# firewall-cmd --permanent --add-service=rpc-bind 
success
[root@linuxhelp ~]# firewall-cmd --permanent --add-service=mountd
success
[root@linuxhelp ~]# firewall-cmd --reload
success

To restart the NFS service

After exporting and opening the firewall, We need to start and enable the nfs services by executing the below command.

[root@linuxhelp ~]# systemctl start nfs-server
[root@linuxhelp ~]# systemctl enable nfs-server
ln -s '/usr/lib/systemd/system/nfs-server.service' '/etc/systemd/system/nfs.target.wants/nfs-server.service'
[root@linuxhelp ~]# systemctl status nfs-server
nfs-server.service - NFS Server
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled)
   Active: active (exited) since Tue 2016-04-26 17:06:58 IST; 23s ago
 Main PID: 42483 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service

Apr 26 17:06:58 linuxhelp systemd[1]: Starting NFS Server...
Apr 26 17:06:58 linuxhelp systemd[1]: Started NFS Server.

Mounting the NFS share to Clients

Now it’s time to mount the NFS share in our client machine. To view the available shares use showmount command. To mount the share permanently create an entry in /etc/fstab file and run mount -a to mount all entries in fstab, as follows.

[root@linuxhelp ~]# showmount -e 192.168.5.88
Export list for 192.168.5.88:
/nfsshare 192.168.5.89
[root@linuxhelp ~]# vim /etc/fstab 

192.168.5.88:/nfsshare	/mnt	   nfs    defaults   0 0
[root@linuxhelp ~]# mount -a
[root@linuxhelp ~]# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda3                18G  3.3G   15G  19% /
devtmpfs                486M     0  486M   0% /dev
tmpfs                   494M  140K  494M   1% /dev/shm
tmpfs                   494M  7.1M  487M   2% /run
tmpfs                   494M     0  494M   0% /sys/fs/cgroup
/dev/sda1               497M  116M  382M  24% /boot
/dev/sr0                3.9G  3.9G     0 100% /run/media/root/CentOS 7 x86_64
192.168.5.88:/nfsshare   18G  3.5G   15G  20% /mnt

After mounting the share locally move to that mount path and try to create some file to check write permissions is applied or not.

[root@linuxhelp ~]# cd /mnt
[root@linuxhelp mnt]# ls
[root@linuxhelp mnt]# touch file1
[root@linuxhelp mnt]# ls
file1

To Automount the share using Autofs

Autofs is used for automatically mounting and unmounting the shares. Autofs automounts the share when the share is not accessible or available at system boot time. To configure autofs service install the autofs package shown below.

[root@linuxhelp ~]# yum install autofs -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package autofs.x86_64 1:5.0.7-54.el7 will be installed
.
.
Running transaction
  Installing : 1:autofs-5.0.7-54.el7.x86_64                                                                                             1/1 
  Verifying  : 1:autofs-5.0.7-54.el7.x86_64                                                                                             1/1 

Installed:
  autofs.x86_64 1:5.0.7-54.el7         

To edit auto.master and auto.misc files

After installing autofs package we need to edit the two files auto.master and auto.misc located under the /etc directory as follows. In auto.misc file, mention any name for your share.

[root@linuxhelp ~]# vim /etc/auto.master

/mnt		/etc/auto.misc		--timeout=60
[root@linuxhelp ~]# vim /etc/auto.misc

Share	--rw,sync	192.168.5.88:/nfsshare

To start autofs service

After creating entry in auto.master and auto.misc files just start and enable the autofs service by running the below command.

[root@linuxhelp ~]# systemctl start autofs
[root@linuxhelp ~]# systemctl enable autofs
ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service'
[root@linuxhelp ~]# systemctl status autofs
autofs.service - Automounts filesystems on demand
   Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled)
   Active: active (running) since Tue 2016-04-26 17:30:22 IST; 20s ago
 Main PID: 41682 (automount)
   CGroup: /system.slice/autofs.service
           └─41682 /usr/sbin/automount --pid-file /run/autofs.pid

Apr 26 17:30:22 linuxhelp systemd[1]: Starting Automounts filesystems on demand...
Apr 26 17:30:22 linuxhelp systemd[1]: Started Automounts filesystems on demand.

After starting the autofs service, run the following command to view the mount points for your nfs share.

[root@linuxhelp ~]# mount | grep share
192.168.5.88:/nfsshare on /mnt type nfs4 (rw,relatime,vers=4.0,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.5.89,local_lock=none,addr=192.168.5.88)
[root@linuxhelp ~]# ls -l /mnt
total 0
drwxr-xr-x. 2 nfsnobody nfsnobody 18 Apr 26 17:21 share
[root@linuxhelp ~]# ls -l /mnt/share
total 0
-rw-r--r--. 1 nfsnobody nfsnobody 0 Apr 26 17:21 file1

As you can see a new directory will be created in the name of our share name (share) under our mount point /mnt

Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.

For more help topics browse our website www.linuxhelp.com

Be the first to comment, we value your suggestions. For further queries please comment below.

Leave a Response