Environment
Here, I will be using CentOS 7 minimal and Fedora 27 system. This guide is also applicable for Oracle Linux and older version of Fedora systems.Here are my demo nodes details.
NFS Server Hostname: server.itzgeek.local (CentOS 7)
NFS Server IP Address: 192.168.12.5/24
NFS Client Hostname: client.itzgeek.local (Fedora 27)
NFS Client IP Address: 192.168.12.7/24
Usage of NFS
- File / Folder sharing between *nix systems
- Allows to mount remote filesystems locally
- Can be acted as Centralized Storage system
- It can be used as a Storage Domain ( Datastore) for VMware and other Virtualization Platform.
- Allows applications to share configuration and data files with multiple nodes.
- Allows to have updated files across the share.
Important Services
The following are the important NFS services, included in nfs-utils packages.rpcbind : The rpcbind server converts RPC program numbers into universal addresses.
nfs-server : It enables the clients to access NFS shares.
nfs-lock / rpc-statd : NFS file locking. Implement file lock recovery when an NFS server crashes and reboots.
nfs-idmap : It translates user and group ids into names, and to translate user and group names
into ids
Important Configuration Files
You would be working mainly on below configuration files, to setup NFS server and Clients./etc/exports : It is the main configuration file, controls which file systems are exported to remote hosts and specifies options.
/etc/fstab : This file is used to control what file systems including NFS directories are mounted when the system boots.
/etc/sysconfig/nfs : This file is used to control which ports the required RPC services run on.
/etc/hosts.allow, and /etc/hosts.deny : These files are called TCP wrappers, controls the access to NFS server. It is used by NFS to decide whether or not to accept a connection coming in from another IP address
Install NFS Server
We need to install NFS packages on NFS server, install it using the following command.[root@server ~]# yum install nfs-utils libnfsidmapOnce the packages are installed, enable and start NFS services.
systemctl enable rpcbind systemctl enable nfs-server
systemctl start rpcbind systemctl start nfs-server systemctl start rpc-statd systemctl start nfs-idmapd
Now, let’s create a directory to share with client servers. Here I will be creating a new directory named “nfsfileshare” in “/” partition.
Note: You can also share your existing directory with NFS.
[root@server ~]# mkdir /nfsfileshareAllow client servers to read and write to the created directory.
[root@server ~]# chmod 777 /nfsfileshare/We have to modify “/etc/exports“file to make an entry of directory “/nfsfileshare” that you want to share.
[root@server ~]# vi /etc/exports /nfsfileshare 192.168.12.7(rw,sync,no_root_squash)/nfsfileshare : shared directory
192.168.12.20 : IP address of client machine. We can also use the hostname instead of an IP address. It is also possible to define the range of clients with subnet like 192.168.12.0/24.
rw : Writable permission to shared folder
sync : all changes to the according filesystem are immediately flushed to disk; the respective write operations are being waited for.
no_root_squash : By default, any file request made by user root on the client machine is treated as by user nobody on the server. (Exactly which UID the request is mapped to depends on the UID of user “nobody” on the server, not the client.) If no_root_squash is selected, then root on the client machine will have the same level of access to the files on the system as root on the server.
You can get to know all the option in the man page (man exports) or here.
Export the shared directories using the following command.
[root@server ~]# exportfs -rExtras:
exportfs -v : Displays a list of shares files and export options on a server
exportfs -a : Exports all directories listed in /etc/exports
exportfs -u : Unexport one or more directories
exportfs -r : Reexport all directories after modifying /etc/exports
After configuring NFS server, we need to mount that shared directory in the client-server.
Configure Firewall
We need to configure firewall on NFS server to allow client servers to access NFS shares. To do that, run the following commands on the NFS server.firewall-cmd --permanent --zone public --add-service mountd firewall-cmd --permanent --zone public --add-service rpc-bind firewall-cmd --permanent --zone public --add-service nfs firewall-cmd --reload
Configure NFS client
We need to install NFS packages on NFS client-server to mount remote filesystem, install NFS packages using below command.[root@client ~]# yum -y install nfs-utils libnfsidmapOnce the packages are installed, enable and start NFS services.
systemctl enable rpcbind
systemctl start rpcbind
Before mounting the NFS share, we need to check the available shares on the NFS server. To do that, run the following command on the client-server.
[root@client ~]# showmount -e 192.168.12.5 Export list for 192.168.12.5: /nfsfileshare 192.168.12.7As per the command, the /nfsfileshare is available on 192.168.12.5.
Extras:
showmount -e : Shows the available shares on your local machine (NFS Server).
showmount -e <server-ip or hostname>: Lists the available shares on the remote server
Now, create a mount point to mount the shared folder ‘/nfsfileshare’ which we’ve created before in the server.
[root@client ~]# mkdir /mnt/nfsfileshareUse below command to mount a shared directory “/nfsfileshare” from NFS server “192.168.12.5” in “/mnt/nfsfileshare” on client-server.
[root@client ~]# mount 192.168.12.5:/nfsfileshare /mnt/nfsfileshareVerify the mounted share on client server using “mount” command.
[root@client ~]# mount | grep nfs
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)
192.168.12.5:/nfsfileshare on /mnt/nfsfileshare type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.12.7,local_lock=none,addr=192.168.12.5)
Also, you can use “df” command to check the mounted NFS share.[root@client ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 478M 0 478M 0% /dev
tmpfs tmpfs 489M 0 489M 0% /dev/shm
tmpfs tmpfs 489M 620K 488M 1% /run
tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/mapper/fedora-root xfs 18G 1.3G 17G 8% /
tmpfs tmpfs 489M 4.0K 489M 1% /tmp
/dev/sda1 ext4 477M 93M 355M 21% /boot
tmpfs tmpfs 98M 0 98M 0% /run/user/0
192.168.12.5:/nfsfileshare nfs4 50G 858M 50G 2% /mnt/nfsfileshare
Create a test file on the mounted directory to verify the read and write access on NFS share.[raj@client ~]$ touch /mnt/nfsfileshare/testIf the above command returns no error, you have working NFS setup.
To mount the shares automatically on every reboot, need to modify “/etc/fstab” file of your client system.
Add “green” line at the end.
[root@client ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue May 26 21:30:49 2015
#
# 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
#
/dev/mapper/fedora-root / xfs defaults 0 0
UUID=f748af6c-0de9-4dc0-98e6-959ffc400f2f /boot ext4 defaults 1 2
/dev/mapper/fedora-swap swap swap defaults 0 0
192.168.12.5:/nfsfileshare/ /mnt/nfsfileshare nfs rw,sync,hard,intr 0 0
save and close the file.Reboot the client machine and check the share whether it is automatically mounted or not.
[root@client ~]# rebootVerify the mounted share on client server using “mount” command.
[root@client ~]# mount | grep nfs
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)
192.168.12.5:/nfsfileshare on /mnt/nfsfileshare type nfs4 (rw,relatime,sync,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.12.7,local_lock=none,addr=192.168.12.5)
If you want to unmount that shared directory from your client server
after you are done with the file sharing, you can unmount that
particular directory using“umount” command.[root@client ~]# umount /mnt/nfsfileshareIf you wish not to use static mounts, you can configure AutoFS on CentOS 7 to mount NFS share only when they are accessed by a user.
That’s All. You have successfully setup NFS Server on CentOS 7 / RHEL 7 / Fedora 26.