Empower Users with NFS: A Comprehensive Tutorial on Setting Up an NFS Server and Home Directory Integration
Data has become the invisible fabric that weaves our modern society together, Forget about oil and gold, Data is the most sought After resource in the world, Hence fueling innovation and shaping our future. Efficient Storage solutions hence are essential for businesses and individuals alike. A Network File System is one such solution that aims to provide seamless file sharing and centralized Data storage and management across the network. It follows a client-server-based connection in which the server shares its filesystem with the client across the network.
It's like remotely accessing a file system or a part of a partition from a different server which only allows authorized clients to mount it as a drive.NFS in most cases is used with TCP/IP protocol suite but it also supports UDP protocol. In This blog, you will create an NFS Solution to attach it to a client and access that drive remotely which helps in data protection and durability making it persistent to any Infrastructure disaster and you can easily keep your data safe in a protected Host as an NFS server. Later This NFS will be attached to a General user as a Home directory to take data persistent at the user level.
- I am using RedHat Linux in a local Workstation over Oracle Virtual Box. It will work the same if you are using VMware.
- You need Two Workstations for the Server and Client.
SETTING UP NFS SERVER
Setting up NFS is quite simple, in just a few steps you will be up and running with the NFS server.
STEP1: In the First step you will install nfs-utils which is a utility package for both client and server-required packages for NFS.
yum install nfs-utils
STEP2: Staring the services for nfs-server and enabling it
systemctl start nfs-server
systemctl enable nfs-server
STEP 3: Create a folder that you want to share with the client's systems.
STEP4: In the /etc/exportfs file you have to make an entry for the folder you want to share and the permissions that the client will have while accessing the file system. Example: rw means the client can read and write in the file system when it gets mounted in the client.
You can even use regex “*” notation to allow any client in the network to access this folder or make an entry specific to each client with permission.
STEP5: Check everything is configured fine with exportfs -v command.
After the server setup, you have to restart the services of NFS to make the changes.
systemctl restart nfs-server
In the client, you have to mount this shared /folder in a directory as a mount point.
STEP1: Create a mount point
STEP2: Mount the /folder in the blog's directory using the mount command.
mount <IP-OF-SERVER>:/folder /blogs/t
NOTE: If this command hangs up then it might be your Network Firewall problem or, the server and client are not in the same network i.e. you have to attach the same wireless adapter to both the VM.
For Firewall, you can stop or disable your firewall by:
systemctl disable firewalld
For the Network adapter check your VM settings and enable the same wireless Adapter for both the VM.
Now, Use the below df -h command to check the status of the drives in the client system.
The above output shows that, at the /blog mount point the NFS Filesystem is attached successfully.
Testing The File Sharing
Creating a file in the client blog directory to see if this will also get synced to the shared folder in the server.
Hurray!! As we can see that the file can also be seen from the Server VM.
Making the MOUNT Permanent
But here’s one problem as we reboot the client system, by default the mounted derives are unmounted if not made permanent. So this can be achieved by using the fstab file or with rc.local file.
As the Client system boots, it can be seen that the NFS Attached Filesystem is unmounted automatically. This is because we haven’t put that in any of the filesystem files such as fstab or rc.locals.
Using the rc.local file to do the job
rc.locals file is very popular among Linux administrators to run the custom scripts and commands while the system startup. So we’ll create an entry for the mount script in the rc.local file and when the system loads after the boot it will run the mount command and will attach the NFS automatically.
Add the “mount Nfs filesystem” command in the rc.local file
After making the entry execute the rc.local file once to make the changes
chmod +x /etc/rc.d/rc.local
Now test the final setup by rebooting the system again and checking for the filesystem.
After the boot, we can see that the NFS filesystem is automatically mounted on the /blogs mount point.
With that, You have successfully configured the NFS server in your local system. How about using this filesystem as a Home Directory for users? Let's discuss this in the further section.
ATTACHING NFS DRIVE AS A HOME DIRECTORY TO AN USER
Whenever we create a user using the useradd command the user entries are made in various files across the Linux filesystem. Every User has his own kind of Area or we can say a Place where without the permission of that specific user nobody can access(except root). This area is called the Home directory.
The above picture shows that the Home directory in the root contains the priyanshu folder which is also a home directory for the priyanshu user. Setting up a home directory with NFS is a simple task but the use cases are important and crucial. It provides a whole new level of security to the Home Directory by not storing the data in a single point but at a centralized location and then further extending the clients for it to allow a multi-login field for the client on any device and enabling the same file structure everywhere, something similar which we achieve using the LDAP SERVERS.
Let's start the practicals now!
STEP1: Create a directory in the NFS mount point which further will be used as the Home Directory of the bloguser
STEP2: Now create a user and use -d tag to specify the Home directory as by default it creates the home directory inside the home.
STEP 3: let's create a file now, Oops! we get permission denied, by default it is denied hence you have to make this user the owner of the bloguser directory.
chown bloguser bloguser/
Now you are able to create the file as the bloguser, which is also synced to the NFS server /folder directory.
In the Server /folder, we can see the hii file which was created in the Home Directory of Bloguser in a different VM.
For decades, the Network File System (NFS) has remained a stalwart in the technological landscape, demonstrating its prowess in optimizing storage solutions. While there may exist alternative tools catering to broader use cases, their ultimate objective, much like NFS, is to deliver efficient storage and user management solutions right at our fingertips. If you’re eager to delve further into the world of DevOps tools, be sure to explore my enlightening blogs on Ansible and Terraform.
Thanks for Reading!