Step-by-Step Guide to Setting Up NFS Server and Client for file sharing | Linux (6) #LinuxAdmin

Karthi SKarthi S
3 min read

Setup NFS server-client in Centos 7.

#linux#nfs#centos#filesharing

What is NFS ?

In modern networks, efficiently sharing files between systems is crucial. NFS (Network File System) provides a seamless solution, enabling Linux and Unix-based systems to access shared directories as if they were local. Whether for collaborative workspaces, shared resources, or centralized file storage, NFS simplifies file sharing across servers.

How to setup NFS ?

Since it's like a client-server model, we need to setup server and client individually.

Setup NFS-server

In this post, we are doing it in Centos, which uses yum as the package manager.

  1. Installing nfs-utils,
sudo su -
yum install nfs-utils
  1. Choose the directory to share. If not present create one.
mkdir /var/nfs_share_dir
  1. Add permissions and ownership privilages to the shared directory.
chmod -R 755 /var/nfs_share_dir
chown nfsnobody:nfsnobody /var/nfs_share_dir
  • chmod -R 755 /var/nfs_share_dir → “This command sets the directory permissions, ensuring it is readable and executable by everyone, but only writable by the owner.”

  • chown nfsnobody:nfsnobody /var/nfs_share_dir → “This command changes the ownership of the directory to nfsnobody, the default user for NFS access, to handle permissions securely.”

  1. Start the nfs services.
systemctl enable rpcbind
systemctl enable nfs-server
systemctl enable nfs-lock
systemctl enable nfs-idmap
systemctl start rpcbind
systemctl start nfs-server
systemctl start nfs-lock
systemctl start nfs-idmap

5.Configuring the exports file for sharing.
Open the exports file and add these lines.

vi /etc/exports

Fill in the the file-shared path and clients details in /etc/exports.
192.168.48.101 - Client's IP

/var/nfs_share_dir    192.168.48.101(rw,sync,no_root_squash)

6.Restart the service

systemctl restart nfs-server

7.Only for Centos 7,NFS service override

firewall-cmd --permanent --zone=public --add-service=nfs
firewall-cmd --permanent --zone=public --add-service=mountd
firewall-cmd --permanent --zone=public --add-service=rpc-bind
firewall-cmd --reload

Setup NFS-Client(s)

1.Installing nfs-utils

sudo su -
yum install nfs-utils

2.Create a mount point [mount point is mandatory for client side to get it mount from the server(s) file system.

mkdir -p /nfs_share_dir
  1. Mounting the filesystem
mount -t nfs <server ip or hostname>:/var/nfs_share_dir /var/nfs_share_dir

-t  type of filesystem
server IP eg., 193.23.55.131 
server hostname., vls.server.co.in
  1. Now if the client is rebooted, we need to remount again. So, to mount permanently,we need to configure /etc/fstab file.
    Append this to /etc/fstab
192.168.48.100:/var/nfs_share_dir /mnt/nfs/var/nfs_share_dir nfs defaults,nofail 0 0
  • defaults → Mention it refers to default mount options, such as rw, suid, dev, etc.
  • using the nofail or bg options to allow the system to boot even if the NFS server is unavailable.

Troubleshooting common issues people face with NFS:

  • Firewall issues: What if NFS doesn’t work because of firewall restrictions?

  • Permission Denied: Explain that if clients face a permission issue, double-check directory ownership and permissions.

  • Network Connectivity: Mention that the client should ensure it can ping the NFS server or check network connectivity.

Happy Sharing!!

0
Subscribe to my newsletter

Read articles from Karthi S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Karthi S
Karthi S

Evolving around Devops and multi-cloud. Cloud - AWS/Azure Container & orchestration -> Docker & Kubernetes Automating -> golang/shell scripting Certified AZ900 | AZ104 | RHSCA