Wednesday, 4 August 2021

Setup NFS Server on Centos 8


Server and Client OS = CentOS Linux release 8.4.2105
NFS = Network File System

NFS Server Hostname = nfsserver.jk.com
IP Address  = 192.168.111.129/24
NFS Client Hostname = nfsclient.jk.com
IP Address  = 192.168.111.128/24
NFS Directory = /nfsdata
NFS mount point = /mnt/data


NFS Server setup

Install nfs-utils on server. 
[root@nfsserver nfsdata]# yum install nfs-utils

To make nfs-server service enabled on reboot 
[root@nfsserver nfsdata]# systemctl enable nfs-server

Starting nfs server service
[root@nfsserver nfsdata]# systemctl start nfs-server

Check status of nfs-server 
[root@nfsserver nfsdata]# systemctl status nfs-server
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
  Drop-In: /run/systemd/generator/nfs-server.service.d
           └─order-with-mounts.conf
   Active: active (exited) since Wed 2021-08-04 21:31:04 IST; 3s ago
  Process: 2042 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCESS)
  Process: 2040 ExecStopPost=/usr/sbin/exportfs -au (code=exited, status=0/SUCCESS)
  Process: 2039 ExecStop=/usr/sbin/rpc.nfsd 0 (code=exited, status=0/SUCCESS)
  Process: 2067 ExecStart=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssprox>
  Process: 2056 ExecStart=/usr/sbin/rpc.nfsd (code=exited, status=0/SUCCESS)
  Process: 2055 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 2067 (code=exited, status=0/SUCCESS)

Aug 04 21:31:04 nfsserver.jk.com systemd[1]: Starting NFS server and services...
Aug 04 21:31:04 nfsserver.jk.com systemd[1]: Started NFS server and services.
[root@nfsserver nfsdata]#


Firewall requirements
Services to be allowed = rpc-bind, nfs

[root@nfsserver /]# firewall-cmd --permanent --add-service=rpc-bind
success
[root@nfsserver /]# firewall-cmd --permanent --add-service=nfs
success
[root@nfsserver /]# firewall-cmd --reload
success
[root@nfsserver /]# firewall-cmd --list-all
public
  target: default
  icmp-block-inversion: no
  interfaces:
  sources:
  services: cockpit dhcpv6-client mountd nfs rpc-bind ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
[root@nfsserver /]#

Create a new directory on server that will be shared to client.

[root@nfsserver /]# mkdir nfsdata
[root@nfsserver /]#

Add nfs export details to the exports file. 

[root@nfsserver /]# cat /etc/exports
/nfsdata        192.168.111.0/24(rw,no_root_squash,sync)
[root@nfsserver /]#

Export the mounts created on exports file.

[root@nfsserver /]# exportfs -rv
exporting 192.168.111.0/24:/nfsdata
[root@nfsserver /]#


Mount nfs directory on Client machine

Run showmount to see if we are getting the exported directory from client machine.

[root@nfsclient ~]# showmount -e 192.168.111.129
Export list for 192.168.111.129:
/nfsdata 192.168.111.0/24
[root@nfsclient ~]#


[root@nfsclient ~]# mount -t nfs 192.168.111.129:/nfsdata /mnt/data
[root@nfsclient ~]# df -h
Filesystem                Size  Used Avail Use% Mounted on
devtmpfs                  877M     0  877M   0% /dev
tmpfs                     896M     0  896M   0% /dev/shm
tmpfs                     896M  8.7M  887M   1% /run
tmpfs                     896M     0  896M   0% /sys/fs/cgroup
/dev/mapper/cl_test-root   37G  1.7G   36G   5% /
/dev/sda1                1014M  195M  820M  20% /boot
tmpfs                     180M     0  180M   0% /run/user/0
192.168.111.129:/nfsdata   37G  1.7G   36G   5% /mnt/data

Create a test file under the nfs mount in client
[root@nfsclient ~]# cd /mnt/data/
[root@nfsclient data]# touch testnew
[root@nfsclient data]# ls
testnew
[root@nfsclient data]# echo "This is a new test file" > testnew
[root@nfsclient data]# cat testnew
This is a new test file
[root@nfsclient data]#

Make mount point persistent over reboot.

To ensure mount point come up during reboot - add it to fstab as below,

[root@nfsclient ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sat Jul 31 05:53:32 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cl_test-root /                       xfs     defaults        0 0
UUID=1e6b0524-90a5-49d8-aff0-466bac22a3b4 /boot                   xfs     defaults        0 0
/dev/mapper/cl_test-swap none                    swap    defaults        0 0
192.168.111.129:/nfsdata        /mnt/data       nfs     defaults        0 0
[root@nfsclient ~]#

Lets unmount and validate if it is getting mounted automatically.

[root@nfsclient ~]# umount /mnt/data

Now issue mount -a to see if it mounts automatically.

[root@nfsclient ~]# mount -a
[root@nfsclient ~]# df -h
Filesystem                Size  Used Avail Use% Mounted on
devtmpfs                  877M     0  877M   0% /dev
tmpfs                     896M     0  896M   0% /dev/shm
tmpfs                     896M  8.7M  887M   1% /run
tmpfs                     896M     0  896M   0% /sys/fs/cgroup
/dev/mapper/cl_test-root   37G  1.7G   36G   5% /
/dev/sda1                1014M  195M  820M  20% /boot
tmpfs                     180M     0  180M   0% /run/user/0
192.168.111.129:/nfsdata   37G  1.7G   36G   5% /mnt/data
[root@nfsclient ~]#


Verify if server showing newly created file and its content.

[root@nfsserver nfsdata]# cat testnew
This is a new test file
[root@nfsserver nfsdata]# pwd
/nfsdata
[root@nfsserver nfsdata]#





No comments:

Post a Comment