Share on Social Media

Learn how to configure NFS server in Linux with this step-by-step guide. From installation to setup, enable seamless file sharing across your network effortlessly. #centlinux #linux #nfs

What is NFS Server?

An NFS (Network File System) server is a protocol that allows for the sharing of files and directories over a network. Developed by Sun Microsystems in 1984, NFS enables users to access and manipulate files on remote systems as though they were local, providing a seamless and integrated file sharing experience across different machines.

Key Features of NFS:

  1. File Sharing: Allows multiple users and systems to access shared files and directories over a network.
  2. Transparency: Provides transparent access to files on remote systems, making them appear as if they are on the local machine.
  3. Scalability: Can be used in both small and large network environments, scaling efficiently as the network grows.
  4. Compatibility: Supports various operating systems, including different Unix flavors and Linux distributions.
  5. Security: Can be configured with different security measures, such as Kerberos authentication, to ensure secure file access.

Common Use Cases:

  • Centralized File Storage: Storing files on a central server to be accessed by multiple clients.
  • Home Directories: Hosting user home directories on a server to be accessed from any client machine in a network.
  • Backup Solutions: Facilitating centralized backups by accessing and storing data from multiple machines on a central backup server.
  • Collaboration: Enabling multiple users to work on shared projects and files stored on a server.

NFS simplifies network file sharing, making it an essential tool in many enterprise environments.

Recommended Online Training: Learn Bash Shell in Linux for Beginners

745772 0021

Linux Server Specification

In this article, we will configure NFS Server in Linux and mount the shared directory at the Client.

We are using two Red Hat Enterprise Linux (RHEL) 7 servers. We will use one as the NFS Server and the other as the NFS Client.

NFS Server:nfsserver.example.com
NFS Client:nfsclient.example.com
Operating System:RHEL 7.0

Read Also: Configure NFS Share for Group Collaboration

Configure NFS Server in Linux

Connect to nfsserver.example.com using ssh as root user.

To configure NFS Server, we have to install nfs-utils package. Usually, this package is automatically installed during installation of Red Hat Enterprise Linux (RHEL) or CentOS 7. However, you can install it anytime using yum command.

# yum install -y nfs-utils

nfs-utils is already installed on our system.

Create a directory to share with other clients.

# mkdir /nfsshare
# chgrp nfsnobody /nfsshare/
# chmod g+w /nfsshare/

We have created a directory /nfsshare, changed its group to nfsnobody and w rights has been given to group. So, the anonymous users can create files on this shared directory.

Adjust SELinux type of the /nfsshare directory.

# semanage fcontext -a -t nfs_t "/nfsshare(/.*)?"
# restorecon -Rv /nfsshare/
restorecon reset /nfsshare context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:nfs_t:s0

If semanage command does not available on your system then install policycoreutils-python package.

Now export/share this directory to specific clients via NFS.

# echo '/nfsshare nfsclient.example.com(rw,sync)' >> /etc/exports
# exportfs -r

Enable and start the nfs-server service.

# systemctl start nfs-server ; systemctl enable nfs-server
ln -s '/usr/lib/systemd/system/nfs-server.service' '/etc/systemd/system/nfs.target.wants/nfs-server.service'

Allow nfs and other supplementary services through Linux firewall.

# firewall-cmd --permanent --add-service={mountd,nfs,rpc-bind}
success
# firewall-cmd --reload
success

NFS Server has been configured.

Configure NFS Client

Connect to the nfsclient.example.com and install nfs-utils package.

# yum install -y nfs-utils

Create a directory, to mount the shared directory from nfsserver.example.com.

# mkdir /mnt/nfsshare

Check the shared directories from nfsserver.example.com.

# showmount -e nfsserver.example.com
Export list for nfsserver.example.com:
/nfsshare nfsclient.example.com

Persistently mount this shared directory by adding following entry in /etc/fstab.

# echo 'nfsserver.example.com:/nfsshare /mnt/nfsshare nfs defaults,_netdev 0 0' >> /etc/fstab
# mount -a

Check the status of mounted directory.

# mount | grep nfs
nfsserver.example.com:/nfsshare on /mnt/nfsshare 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.116.202,local_lock=none,addr=192.168.116.200,_netdev)

Create a file in this shared directory, to verify the file permissions.

# cd /mnt/nfsshare/
# touch test1
# ls -al
total 0
drwxrwxr-x. 2 root      nfsnobody 18 Jul 31 07:32 .
drwxr-xr-x. 4 root      root      31 Jul 31 07:23 ..
-rw-r--r--. 1 nfsnobody nfsnobody  0 Jul 31 07:32 test1

We have successfully configure NFS server and client on CentOS/RHEL 7 and persistently mount the NFS share on that client.

However, this NFS share is not secured and anyone can write on this directory. For configuring a secure NFS share, you should read our article Configure a Kerberized NFS Server in RHEL 7.

If you are new to Linux and facing difficulty in working at Linux Bash prompt. We recommend that, you should read The Linux Command Line, 2nd Edition: A Complete Introduction by William Shotts.

Final Thoughts

If you found this guide on configuring an NFS server in Linux helpful and need further assistance, feel free to reach out for professional support. I offer expert services to ensure your Linux server setup is seamless and efficient. Visit my Fiverr profile for more details and to get started: Linux Technical Support. Let’s make your server configuration a breeze!

6 thoughts on “How to configure NFS Server in Linux”
  1. Hi Ahmer,
    Thanks for this post. It's much better than the official documents and most of other posts available in the internet.
    Could you please let me know how to create a nfs4 only share?
    Eg: share /share/nfs4 dir to a subnet in ro and to a whole domain in rw.

Leave a Reply