How to install and configure NFS server and client in RHEL7

Let’s create the VM first:

[root@rhel7 ~]# virt-install \
> --hvm \
> --name nfs \
> --ram 1024 \
> --disk path=/kvm/nfs.img,size=150 \
> --vcpus 1 \
> --os-type linux \
> --os-variant rhel7 \
> --network bridge=virbr0 \
> --graphics none \
> --location 'http://192.168.122.3/rhel7' \
> --extra-args "ks=http://192.168.122.3/ksfiles/rhel7-minimal-ks.cfg \
> console=tty0 console=ttyS0,115200 SERVERNAME=nfs.jefrey.io IPADDR=192.168.122.8"

Starting install...
...
...
[root@nfs ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.122.8  netmask 255.255.255.0  broadcast 192.168.122.255
        inet6 fe80::5054:ff:febf:6868  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:bf:68:68  txqueuelen 1000  (Ethernet)
        RX packets 19017  bytes 1381634 (1.3 MiB)
        RX errors 0  dropped 34  overruns 0  frame 0
        TX packets 16758  bytes 917087 (895.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@nfs ~]# 
[root@nfs ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/vda2  vg00 lvm2 a--  149.50g    0 
[root@nfs ~]# 
[root@nfs ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/vg00-root           117G  1.4G  116G   2% /
devtmpfs                        492M     0  492M   0% /dev
tmpfs                           498M     0  498M   0% /dev/shm
tmpfs                           498M  6.6M  491M   2% /run
tmpfs                           498M     0  498M   0% /sys/fs/cgroup
/dev/mapper/vg00-home          1014M   33M  982M   4% /home
/dev/mapper/vg00-var             10G   81M   10G   1% /var
/dev/mapper/vg00-var_log         10G   35M   10G   1% /var/log
/dev/mapper/vg00-var_log_audit  5.0G   40M  5.0G   1% /var/log/audit
/dev/mapper/vg00-tmp            5.0G   33M  5.0G   1% /tmp
/dev/vda1                       509M   52M  458M  11% /boot
[root@nfs ~]# 
[root@nfs ~]# vi /etc/sysconfig/network
[root@nfs ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=nfs.jefrey.io
GATEWAY=192.168.122.1
GATEWAYDEV=eth0
[root@nfs ~]#

Install the package and continue with the configurations:

[root@nfs ~]# yum -y install nfs-utils
[root@nfs ~]# vi /etc/exports
[root@nfs ~]# cat /etc/exports
/nfsshare 192.168.122.0/24(rw,no_root_squash)
[root@nfs ~]# 
[root@nfs ~]# mkdir /nfsshare
[root@nfs ~]# 
[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# systemctl start nfs-server
[root@nfs ~]# systemctl start nfs-lock
[root@nfs ~]# systemctl start nfs-idmap
[root@nfs ~]# systemctl enable rpcbind
[root@nfs ~]# systemctl enable nfs-server
[root@nfs ~]# systemctl enable nfs-idmap
[root@nfs ~]# 
[root@nfs ~]# exportfs 
/nfsshare         192.168.122.0/24
[root@nfs ~]# 
[root@nfs ~]# firewall-cmd --permanent --add-port=111/tcp
[root@nfs ~]# firewall-cmd --permanent --add-port=54302/tcp
[root@nfs ~]# firewall-cmd --permanent --add-port=20048/tcp
[root@nfs ~]# firewall-cmd --permanent --add-port=2049/tcp
[root@nfs ~]# firewall-cmd --permanent --add-port=46666/tcp
[root@nfs ~]# firewall-cmd --permanent --add-port=42955/tcp
[root@nfs ~]# firewall-cmd --permanent --add-port=875/tcp
[root@nfs ~]# firewall-cmd --reload
[root@nfs ~]#

To do in NFS client:

[root@ns2 ~]# yum -y install nfs-utils
[root@ns2 ~]# mkdir /nfsshare
[root@ns2 ~]# mount -t nfs nfs.jefrey.io:/nfsshare /nfsshare
[root@ns2 ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/vg00-root            17G  916M   16G   6% /
devtmpfs                        492M     0  492M   0% /dev
tmpfs                           498M     0  498M   0% /dev/shm
tmpfs                           498M  6.6M  491M   2% /run
tmpfs                           498M     0  498M   0% /sys/fs/cgroup
/dev/mapper/vg00-home          1014M   33M  982M   4% /home
/dev/mapper/vg00-tmp            5.0G   33M  5.0G   1% /tmp
/dev/mapper/vg00-var             10G   83M   10G   1% /var
/dev/mapper/vg00-var_log         10G   36M   10G   1% /var/log
/dev/mapper/vg00-var_log_audit  5.0G   41M  5.0G   1% /var/log/audit
/dev/vda1                       509M   52M  458M  11% /boot
nfs.jefrey.io:/nfsshare         117G  1.4G  116G   2% /nfsshare
[root@ns2 ~]#

Add the following in /etc/fstab should you want it to persist across reboots:

nfs.jefrey.io:/nfsshare    /nfsshare            nfs        defaults    0 0

Testing from the client:

[root@ns2 ~]# echo "Test file from `hostname`" > /nfsshare/testfile
[root@ns2 ~]# ls -lart /nfsshare/
total 8
dr-xr-xr-x. 18 root root 4096 Jul 12 18:24 ..
drwxr-xr-x.  2 root root   21 Jul 12 18:33 .
-rw-r--r--.  1 root root   29 Jul 12 18:33 testfile
[root@ns2 ~]# 
[root@ns2 ~]# cat /nfsshare/testfile 
Test file from ns2.jefrey.io
[root@ns2 ~]#

Visible in the server:

[root@nfs ~]# ls -lart /nfsshare/
total 8
dr-xr-xr-x. 19 root root 4096 Jul 12 18:17 ..
drwxr-xr-x.  2 root root   21 Jul 12 18:33 .
-rw-r--r--.  1 root root   29 Jul 12 18:33 testfile
[root@nfs ~]# 
[root@nfs ~]# cat /nfsshare/testfile 
Test file from ns2.jefrey.io
[root@nfs ~]#

Done!

Leave a Reply

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