Skip to content

Nfs docs

Comprehensive NFS Setup Guide πŸ“šπŸ–₯️

Welcome! πŸŽ‰
In this guide, we will cover everything you need to know about Network File System (NFS) configurationβ€”from setting up the server to configuring the client, troubleshooting, and more! By the end, you'll have a fully functional NFS system and be equipped to handle common issues along the way. πŸŒπŸ’Ύ


πŸ“œ Table of Contents

  1. Introduction to Network File System (NFS)
  2. When is NFS Needed?
  3. How NFS Works (With Mermaid Graph)
  4. NFS Server Configuration
  5. Installing NFS Packages
  6. Starting the NFS Services
  7. Creating NFS Directory & Setting Permissions
  8. Editing the /etc/exports File
  9. Exporting NFS Filesystems
  10. Adjusting Firewall Settings
  11. NFS Client Configuration
  12. Installing NFS Packages on the Client
  13. Starting the Client Services
  14. Firewall Configuration
  15. Mounting NFS Shares
  16. Verifying NFS Setup
  17. Unmounting NFS Filesystems
  18. Troubleshooting Common NFS Issues
  19. Complete Bash Scripts for NFS Setup

1. Introduction to Network File System (NFS) πŸŒπŸ’‘

Network File System (NFS) is a client-server file system protocol that allows users to access files on remote systems as if they were on their local machine. It is ideal for sharing directories across multiple systems in a network, especially for projects requiring real-time collaboration.

2. When is NFS Needed? πŸ€”πŸ”§

NFS is particularly useful in scenarios like:

  • Development Environments: Multiple developers working on a shared codebase.
  • Centralized Data Storage: Allowing clients to access and manipulate files in one centralized location.
  • Backup Solutions: Efficient backups by accessing remote filesystems.

3. How NFS Works (With Mermaid Graph) πŸ–₯οΈπŸ”—

Here's a visual representation of how NFS operates:

graph TD
    Client[Client Machine]
    Server[NFS Server]
    SharedDir[Shared Directory]

    Client -->|Request Access| Server
    Server -->|Authenticate| Client
    Server -->|Provide Access to| SharedDir
    Client -->|Read/Write Files| SharedDir

4. NFS Server Configuration πŸ› οΈπŸ’»

Setting up the NFS server is the first step to creating your networked file system.

Installing NFS Packages πŸ“¦

First, you need to install the NFS server package:

sudo apt update
sudo apt install nfs-kernel-server

Starting the NFS Services πŸš€

Enable and start the NFS server:

sudo systemctl enable nfs-server
sudo systemctl start nfs-server

Creating NFS Directory & Setting Permissions πŸ“‚πŸ”

Create the directory that will be shared across the network and set the appropriate permissions:

sudo mkdir -p /srv/nfs/shared
sudo chown nobody:nogroup /srv/nfs/shared
sudo chmod 755 /srv/nfs/shared

Editing the /etc/exports File ✏️

Open the /etc/exports file and specify the shared directory and the network that will have access:

sudo nano /etc/exports

Add the following line:

/srv/nfs/shared 192.168.1.0/24(rw,sync,no_subtree_check)

Exporting NFS Filesystems πŸ“œ

Export the filesystem so that the NFS server shares the directories:

sudo exportfs -a

Adjusting Firewall Settings πŸ›‘οΈ

Make sure your firewall is configured to allow NFS traffic:

sudo ufw allow from 192.168.1.0/24 to any port nfs
sudo ufw reload

5. NFS Client Configuration πŸ”—πŸ–₯️

Once the server is up and running, the next step is to configure the clients that will access the NFS shares.

Installing NFS Packages on the Client πŸ“¦

Install the required NFS packages:

sudo apt update
sudo apt install nfs-common

Starting the Client Services πŸš€

Enable and start the NFS client services:

sudo systemctl enable nfs-client.target
sudo systemctl start nfs-client.target

Firewall Configuration πŸ›‘οΈ

Allow NFS traffic through the client’s firewall:

sudo ufw allow from 192.168.1.0/24 to any port nfs
sudo ufw reload

Mounting NFS Shares πŸ“‚

Create a mount point and mount the NFS share from the server:

sudo mkdir -p /mnt/nfs/shared
sudo mount 192.168.1.100:/srv/nfs/shared /mnt/nfs/shared

Replace 192.168.1.100 with the IP address of your NFS server.


6. Verifying NFS Setup βœ…πŸ”

To check if the NFS filesystem is mounted properly, run:

df -h | grep nfs

Alternatively, you can use:

mount | grep nfs

7. Unmounting NFS Filesystems βŒπŸ“‚

If you need to unmount the NFS share:

sudo umount /mnt/nfs/shared

8. Troubleshooting Common NFS Issues πŸ› οΈπŸ”

Here are some common issues you might encounter with NFS, along with possible solutions:

  1. Permission Denied πŸ”:

  2. Verify the permissions and ownership of the shared directory.

  3. Check the configuration in /etc/exports.

  4. Mount Timeout πŸ•’:

  5. Ensure the NFS server is running.

  6. Verify network connectivity between the server and client.

  7. Stale File Handles πŸ—‚οΈ:

  8. Unmount and remount the NFS filesystem.

  9. Restart the NFS services.

  10. Checking Logs πŸ“„:

  11. Review log files for errors:

    sudo tail -f /var/log/syslog
    

  12. Firewall Issues πŸ›‘οΈ:

  13. Ensure the firewall allows NFS traffic on both server and client.

  14. Verifying Exports πŸ“:

  15. List exported NFS shares:

    sudo exportfs -v
    

  16. Network Connectivity 🌐:

  17. Ping the NFS server from the client to ensure they can communicate:
    ping 192.168.1.100
    

9. Complete Bash Scripts for NFS Setup πŸ“πŸ“œ

Here are complete scripts for setting up an NFS server and client.

Server Script πŸ–₯️

#!/bin/bash
# NFS Server Setup Script

# Update and install NFS server
sudo apt update
sudo apt install nfs-kernel-server -y

# Enable and start NFS service
sudo systemctl enable nfs-server
sudo systemctl start nfs-server

# Create shared directory and set permissions
sudo mkdir -p /srv/nfs/shared
sudo chown nobody:nogroup /srv/nfs/shared
sudo chmod 755 /srv/nfs/shared

# Configure exports
echo "/srv/nfs/shared 192.168.1.0/24(rw,sync,no_subtree_check)" | sudo tee -a /etc/exports

# Export filesystem
sudo exportfs -a

# Adjust firewall
sudo ufw allow from 192.168.1.0/24 to any port nfs
sudo ufw reload

Client Script πŸ–₯️

#!/bin/bash
# NFS Client Setup Script

# Update and install NFS client
sudo apt update
sudo apt install nfs-common -y

# Enable and start NFS client services
sudo systemctl enable nfs-client.target
sudo systemctl start nfs-client.target

# Adjust firewall
sudo ufw allow from 192.168.1.0/24 to any port nfs
sudo ufw reload

# Create mount point and mount NFS share
sudo mkdir -p /mnt/nfs/shared
sudo mount 192.

168.1.100:/srv/nfs/shared /mnt/nfs/shared