Setting Up NFS/AutoFS
The Network File System (NFS) is a mechanism for storing files on a network. It is a distributed file system that allows users to access files and directories located on remote computers and treat those files and directories as if they were local. AutoFS is a service that automatically mounts (and unmounts) filesystems when accessed, making NFS storage more efficient.
Step 1: Set Up NFS Export on Server
Setup NFS server named (server) on a VM and share /home to only the client.
Install NFS Server Tools and NFS Server
sudo yum install nfs-utils
sudo yum install nfs-kernel-server
Configure Exports
Add the export rule to /etc/exports:
/home client_IP(rw,sync,no_subtree_check)
Replace
client_IPwith the actual IP or hostname of the client.
Restart the Service
sudo systemctl restart nfs-kernel-server
Step 2: Set Up User on Server
Create
tsqaureUser on Server
sudo useradd -m -d /home/tsquare tsquare
sudo passwd tsquare
This user’s home folder now lives on the NFS-exported directory
/home.
Step 3: Mount Export on Client
Install NFS Client
sudo yum install nfs-common
Mount the NFS Share
sudo mount server:/home /mnt/home
Step 4: Enable Persistence via /etc/fstab
Make
/mnt/homePermanent Across Reboot (on client)
Edit the
/etc/fstaband add:
server:/home /mnt/home nfs defaults 0 0
This ensures it mounts on boot.
Step 5: Add Client User Pointing to Mounted Home
Add a User to (client) and Make
/mnt/hometheir Home Directory
Let’s say the user is
tsquare, and their home is mounted over NFS:
sudo user add -d /mnt/home/tsquare -s /bin/bash tsquare
sudo passwd tsquare
Now when
tsquarelogs in, it uses/mnt/home/tsquareas their home, backed by the NFS share.
Step 6: Migrate NFS Mount
On (client) Migrate
/mnt/hometo/homeand Retain Permanent Users
This means you want the server’s
/home(shared via NFS) to be mounted as the local/homedirectory on the client. Start by unmounting/mnt/home:
sudo umount /mnt/home
Update
/etc/fstab:
server:home /home nfs defaults 0 0
Mount it:
sudo mount -a
Now the
/homedirectory on the client is actually coming from the server via NFS.
Note: You must create the
tsquareuser on the client with the same UID/GID as on the server.
sudo useradd -u [UID] -g [GID] -d /home/tsquare -s /bin/bash tsquare
Use
id tsquareon the server to fidn UID/GID.This ensures correct ownership of files when
tsquarelogs into the client.
Step 7: Use AutoFS for Dynamic Mounting
Setup autofs on (client) so that /server/home works. This uses autofs to mount the NFS share on demand.
Install AutoFS
sudo yum install autofs
Edit
/etc/auto.masterand Add
/server /etc/auto.home
Create
/etc/auto.home:
home -fstype=nfs server:/home
Restart AutoFS
sudo systemctl restart autofs
Now when you
cd /server/home, autofs automatically mounts it.
Step 8: Enable Root Access
Share server:/home with your desktop so root can write a file to /home/root as root. On the server:
Allow Root Access in
/etc/exports
/home desktop_IP(rw,sync,no_subtree_check,no_root_squash)
Note:
no_root_squashlets the remote root user act as root on the share – use with caution!
Mount the Share and Write as Root
On the desktop:
sudo mount server:/home /mnt/home
sudo touch /mnt/home/root/hello.txt
Things to Keep in Mind
AutoFS works in tandem with NFS, rather than replacing it.
Be careful with syntax and spacing in nfs configuration files.
Resources
History:
How-To:
Literature: