Set up NFS Server on CentOS 7 and Configure Client Automount

NFS server is used to make its data generally available to clients. The automount program is used to manage mount points for autofs.

Software

Software used in this article:

  1. CentOS 7.2
  2. nfs-utils 1.3.0
  3. autofs 5.0.7

NFS Server Installation

Packages rpcbind and tcp_wrappers are installed as dependencies.

# yum install nfs-utils
# systemctl enable rpcbind && systemctl start rpcbind
# systemctl enable nfs-server && systemctl start nfs-server

NFS Server Configuration

Our NFS server has a FQDN of spacewalk.hl.local and resides on 10.8.8.0/24 LAN.

Create a Logical Volume for NFS Shares

Check volume groups:

# vgs
  VG         #PV #LV #SN Attr   VSize   VFree 
  vg_centos7   1   4   0 wz--n- 127.21g 75.22g

Create a 5GB logical volume named lv_nfs in the vg_centos7 group, format as ext4 and mount on /mnt/nfs.

# lvcreate --name lv_nfs --size 5G vg_centos7
# mkfs.ext4 -m 0 /dev/mapper/vg_centos7-lv_nfs
# mkdir -p /mnt/nfs
# mount /dev/mapper/vg_centos7-lv_nfs /mnt/nfs
# mkdir -p /mnt/nfs/public
# chown -R nfsnobody:nfsnobody /mnt/nfs

Don’t forget to add to fstab for permanent mount across reboots.

Configure NFS exports:

# cat /etc/exports
/mnt/nfs  10.8.8.0/24(rw,sync,no_subtree_check,root_squash,all_squash)

Parameters that are used in our case:

  1. rw: allows both read and write requests on the NFS volume,
  2. sync: replies to requests only after the changes have been committed to stable storage,
  3. no_subtree_check: disables subtree checking,
  4. root_squash: maps requests from (root) uid/gid 0 to the nfsnobody uid/gid,
  5. all_squash: maps all uids and gids to the nfsnobody uid/gid.

Export the share:

# exportfs -rav
exporting 10.8.8.0/24:/mnt/nfs

Check:

# showmount -e
Export list for spacewalk.hl.local:
/mnt/nfs 10.8.8.0/24

NFS Server Firewall

Allow NFS, rpcbind (portmapper) and mountd access from 10.8.8.0/24 LAN:

# iptables -A INPUT -s 10.8.8.0/24 -p tcp -m multiport --dport 111,2049,20048 -j ACCEPT
# iptables -A INPUT -s 10.8.8.0/24 -p udp -m multiport --dport 111,2049,20048 -j ACCEPT

Client NFS and Automount Configuration

On a CentOS 7 client machine, install autofs:

# yum install autofs nfs-utils

Add the following line to the file /etc/auto.master:

/nfs  /etc/auto.nfs

Create the file /etc/auto.nfs with the following content:

public  -rw  spacewalk.hl.local:/mnt/nfs/public

Enable and restart the autofs service:

# systemctl enable autofs && systemctl restart autofs

You should notice the /nfs directory created by automount. Change into /nfs directory as a regular user, the folder should be empty:

$ cd /nfs
$ ls -a
.  ..

Now change into /nfs/public directory which is an NFS mount:

$ cd ./public
$ ls -la
total 4
drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Mar 19 17:56 .
drwxr-xr-x. 3 root      root         0 Mar 19 18:11 ..

3 thoughts on “Set up NFS Server on CentOS 7 and Configure Client Automount

Leave a Reply to Si Thu Cancel reply

Your email address will not be published. Required fields are marked *