Setting Up an NFS Server for Media Sharing on Ubuntu | Generated by AI

Home PDF

To set up an NFS (Network File System) server on Ubuntu to serve media files, follow these detailed steps. This guide assumes you want to share a directory containing media files (e.g., videos, music) over your network securely and efficiently.


Step 1: Install the NFS Server Software

First, you need to install the NFS server package on your Ubuntu system.

  1. Update the package list to ensure you get the latest versions:
    sudo apt update
    
  2. Install the nfs-kernel-server package, which provides the NFS server functionality:
    sudo apt install nfs-kernel-server
    

This installs the necessary software to run an NFS server.


Step 2: Create and Configure the Media Directory

You need a directory to store your media files and configure it for sharing.

  1. Create a directory to hold your media files (e.g., /media/share):
    sudo mkdir -p /media/share
    
  2. Set the ownership to your user account (replace $USER with your actual username if needed):
    sudo chown $USER:$USER /media/share
    
  3. Adjust permissions to allow access. For media files that are primarily read-only for clients, you can set:
    • Directories: 755 (read and execute for others)
    • Files: 644 (read for others) However, for simplicity, apply 755 recursively if you don’t need fine-grained control:
      sudo chmod -R 755 /media/share
      
    • Note: Media files don’t need to be executable, but 755 on files won’t harm NFS functionality since execution happens locally on the client.
  4. Place your media files in /media/share (e.g., copy videos or music into this directory).

Step 3: Configure NFS Exports

The NFS server uses the /etc/exports file to define which directories to share and who can access them.

  1. Open the exports file with a text editor:
    sudo nano /etc/exports
    
  2. Add a line to share /media/share. The syntax is:
    /path/to/directory client(options)
    
    • Example 1: Share with a specific client IP (e.g., 192.168.1.100) with read-write access:
      /media/share 192.168.1.100(rw,sync,no_subtree_check)
      
    • Example 2: Share with all devices on your subnet (e.g., 192.168.1.0/24) with read-only access:
      /media/share 192.168.1.0/24(ro,sync,no_subtree_check)
      
    • Example 3: Share with any device (less secure, use cautiously):
      /media/share *(rw,sync,no_subtree_check)
      

    Options Explained:

    • rw: Read-write access (use ro for read-only if preferred for media streaming).
    • sync: Ensures data is written to disk before confirming operations (safer but slower; use async for performance at the risk of data loss in crashes).
    • no_subtree_check: Improves performance by disabling subtree checking (recommended for most setups).
  3. Save the file (Ctrl+O, then Enter in nano) and exit (Ctrl+X).

Step 4: Apply the NFS Configuration

After editing the exports file, update the NFS server to reflect your changes.

  1. Export the shares:
    sudo exportfs -a
    
  2. Restart the NFS server to apply the configuration:
    sudo systemctl restart nfs-kernel-server
    
  3. Verify the service is running:
    sudo systemctl status nfs-kernel-server
    
    • Look for “active (running)” in the output.
  4. Check the exported directories:
    sudo exportfs -v
    
    • This should list /media/share with the options you specified.

Step 5: Configure the Firewall

NFS uses specific ports, and you need to ensure your firewall allows traffic to them. Ubuntu often uses UFW (Uncomplicated Firewall).

  1. Default NFS Ports:
    • NFS: 2049 (TCP/UDP)
    • RPC: 111 (TCP/UDP)
    • Other services (mountd, statd, lockd): Dynamic by default, but can be set to static ports.
  2. For simplicity, allow the essential ports with dynamic settings (if you don’t set static ports):
    sudo ufw allow 2049
    sudo ufw allow 111
    
    • Note: This may not cover all NFS traffic if mountd, statd, or lockd use random ports.
  3. Optional: Set Static Ports for Better Control (recommended for precise firewall rules):
    • Edit /etc/default/nfs-kernel-server:
      sudo nano /etc/default/nfs-kernel-server
      

      Add or modify:

      RPCMOUNTDOPTS="-p 4001"
      
    • Edit /etc/default/nfs-common:
      sudo nano /etc/default/nfs-common
      

      Add or modify:

      STATDOPTS="-p 4002"
      
    • Create /etc/modprobe.d/nfs.conf for lockd:
      sudo nano /etc/modprobe.d/nfs.conf
      

      Add:

      options lockd nlm_udp_port=4003 nlm_tcp_port=4003
      
    • Restart the NFS server:
      sudo systemctl restart nfs-kernel-server
      
  4. Allow the static ports in UFW:
    sudo ufw allow 2049/tcp
    sudo ufw allow 2049/udp
    sudo ufw allow 111/tcp
    sudo ufw allow 111/udp
    sudo ufw allow 4001/tcp
    sudo ufw allow 4001/udp
    sudo ufw allow 4002/tcp
    sudo ufw allow 4002/udp
    sudo ufw allow 4003/tcp
    sudo ufw allow 4003/udp
    
  5. Enable UFW if it’s not already active:
    sudo ufw enable
    

Step 6: Verify the Setup

Ensure the NFS server is working correctly.

  1. Check exported shares from the server:
    showmount -e localhost
    
    • You should see /media/share listed with the correct clients/options.
  2. Verify listening ports:
    sudo ss -tuln | grep -E "2049|111|4001|4002|4003"
    
    • Confirm these ports are open and bound to the NFS services.

Step 7: Test from a Client (Optional)

On a client machine (e.g., another Ubuntu system), test mounting the share:

sudo mount -t nfs <server_ip>:/media/share /mnt

Additional Notes for Media Files


Summary of Commands

Here’s a quick reference for the basic setup (without static ports):

sudo apt update
sudo apt install nfs-kernel-server
sudo mkdir -p /media/share
sudo chown $USER:$USER /media/share
sudo chmod -R 755 /media/share
echo "/media/share *(rw,sync,no_subtree_check)" | sudo tee -a /etc/exports
sudo exportfs -a
sudo systemctl restart nfs-kernel-server
sudo ufw allow 2049
sudo ufw allow 111

With this setup, your Ubuntu NFS server should be ready to serve media files over the network. Adjust IP addresses, permissions, and firewall rules based on your security and network requirements.


Back 2025.03.16 Donate