Network File System (NFS)

Network File System (NFS) is a protocol that allows users to access and share files over a network as if they were on a local file system. It was developed by sun microsystems in the 1980s and is widely used in Unix and Linux Environment for sharing files between systems.

How NFS Works 

Server-Client Model:

  • NFS Server: Hosts the files and directories to be shared.

  • NFS Client: Accesses the shared files and directories


File Sharing:

  • Files and directories are exported by the server and mounted on the client system, allowing users to interact with them transparently.


Protocol:

  • NFS uses Remote Procedure Call (RPC) to handle requests.

  • Typically operates on port 2049.


Versions:

  • NFSv2: Limited to 32-bit file sizes.

  • NFSv3: Introduced support for larger files and asynchronous writes.

  • NFSv4: Enhanced security and performance with support for stateful sessions.

Advantages of NFS

  • Centralized file management.

  • Reduces duplication of files across systems.

  • Users access shared files as if they are on local drives.

  • Compatibility with various operating systems.

Installing and Configuring NFS

nfs-utils Package must be installed on both server and client.

$ sudo yum install nfs-utils -y 

NFS Server Configuration

Create a Shared Directory

  • Create a Directory on server where files are stored and can be shared

$ sudo mkdir -p /nfs/shared

Set File Permissions:

  • Adjust ownership and permissions on server 

$ sudo chown nobody:nobody /nfs/shared

$ sudo chmod 755 /nfs/shared


Configure Exports:

  • Edit the /etc/exports file to define what directories to share and their access permission

$ sudo vim /etc/exports

/nfs/shared   192.168.1.0/24(rw,sync,no_root_squash)


Common Options:

  • rw: Read-Write Access

  • ro: Read-Only Access

  • sync: Writes changes to disk immediately.

  • no_root_squash: Allows root access from clients

  • subtree_check: Ensure security for subdirectory sharing.


Export the shared directory:

  • Use the exportfs command to export the directory

$ sudo exportfs -arv


Start and Enable NFS Services:

$ sudo systemctl start nfs-server

$ sudo systemctl enable nfs-server

Client Configuration

Discover NFS Shares

  • Use the showmount command to discover available shares

$ showmount -e <server-ip/hostname>


Create a Mount Point

$ sudo mkdir -p /mnt/nfs/shared-files


Mount the NFS Share

$ sudo mount <server-ip/hostname>:/nfs/shared /mnt/nfs/shared-files


Verify the Mount

  • Check if the NFS share is mounted or not

$ df -Th


Make Mount Persistent

  •  Add an entry to /etc/fstab for automatic mounting at boot time

$ sudo vim /etc/fstab

<server-ip/hostname>:/nfs/shared  /mnt/nfs/shared-files   nfs   defaults   0   0

Testing the NFS Setup

Create a File 

  • Create a files and directory on Server/Client in shared directory to share them

$ mkdir /nfs/shared/test-dir

$ touch /nfs/shared/test-dir/test-file.txt


Verify 

  • Check if the same directories and files are shared on server/client

$ ls -lR /mnt/nfs/shared-files

Security Considerations

Restrict Access:

  • Use IP-based restrictions in the /etc/exports file

/nfs/shared   192.168.1.24(rw,sync)


Enable Firewalls

  • Allow NFS-Related Ports instead of stopping/disabling the firewall completely

$ sudo firewall-cmd --add-service=nfs --permanent

$ sudo firewall-cmd --reload


Use NFSv4

  • NFSv4 includes stronger authentication and encryption mechanisms

.

Use Kerberos Authentication

  • Configure Kerberos with NFS providers secure access control

Managing and Troubleshooting NFS

Commands for Management

  • Export Filesystem
    $ sudo exportfs -avr

  • List Exported Directories
    $ sudo exportfs

  • Unmount a Share
    $ sudo umount /nfs/shared-files/

  • Check NFS Service Status

$ sudo systemctl status nfs-server

Logs and Debugging

  • Check logs for issues:
    $ sudo journalctl -xe
    $ sudo tail -f /var/log/messages

Performance Tuning

Use Async Option:

Asynchronous writes improve performance bu may reduce data safety

/nfs/shared   192.168.1.24(rw,async)


Adjust NFS Threads:

Increase the number of threads in /etc/nfs.conf file
threads=16


Enable Caching:
Use caching mechanisms like FS-Cache to reduce I/O operations.

Best Practices

  1. Regularly monitor NFS performance using tools like nfsstat

  2. Use NFSv4 for enhanced features and security

  3. Keep backups of critical NFS data.

  4. Optimize permissions to avoid excessive privileges.

NFS Common issues and resolutions


Stale NFS File Handle: 

Occurs when the server restarts or shares are unexported. Remount the share

$ sudo umount -f /nfs/shared-files && sudo mount -a


Permission Denied:

Check /etc/exports for correct client permissions.


NFS Server not starting: Verify dependencies like RPC Services.

$ sudo systemctl start rpcbind

Conclusion

NFS is a powerful and efficient method for sharing files across systems in a linux network. By understanding its installation, configuration, security, and troubleshooting, administrations can leverage NFS for centralized and seamless file management.


No comments:

Post a Comment