NFS Server and Client

Level: Intermediate Module: File Sharing & Print 10 min read Lesson 35 of 66

Overview

  • What you’ll learn: How to install and configure an NFS server on Ubuntu, export directories, mount NFS shares on client systems, and use autofs for automatic on-demand mounting.
  • Prerequisites: Understanding of file sharing protocols (Lesson 34), basic Linux networking and file permissions
  • Estimated reading time: 18 minutes

Introduction

Network File System (NFS) allows you to share directories across a network so that Linux and Unix clients can mount them as if they were local file systems. This transparent access makes NFS invaluable for shared home directories, centralized application data, and distributed storage in cluster environments.

NFS has evolved significantly over the years. NFSv3, while still in use, requires multiple daemon ports and an external locking mechanism. NFSv4 consolidates everything into a single TCP port (2049), integrates locking and security, and supports features like delegation and compound operations that improve performance. In this lesson, we will focus primarily on NFSv4 while noting important NFSv3 differences.

By the end of this lesson, you will be able to configure an NFS server that exports directories to specific clients, mount those exports on client machines both manually and automatically, and troubleshoot common NFS issues.

Installing and Configuring the NFS Server

The NFS server on Ubuntu is provided by the nfs-kernel-server package. Installation is straightforward, and the server begins running immediately after package installation. The core configuration file is /etc/exports, which defines which directories are shared and who can access them.

# Install NFS server packages
$ sudo apt update
$ sudo apt install nfs-kernel-server

# Verify the NFS server is running
$ sudo systemctl status nfs-server
● nfs-server.service - NFS server and services
     Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled)
     Active: active (exited) since Mon 2025-01-15 10:00:00 UTC

# Create directories to share
$ sudo mkdir -p /srv/nfs/shared
$ sudo mkdir -p /srv/nfs/home
$ sudo chown nobody:nogroup /srv/nfs/shared

# Configure exports in /etc/exports
$ sudo nano /etc/exports
# Format: directory  client(options) [client2(options)]
/srv/nfs/shared  192.168.1.0/24(rw,sync,no_subtree_check)
/srv/nfs/home    192.168.1.0/24(rw,sync,no_root_squash,no_subtree_check)
/srv/nfs/shared  10.0.0.5(ro,sync,no_subtree_check)

# Apply the exports configuration
$ sudo exportfs -ra

# Verify active exports
$ sudo exportfs -v
/srv/nfs/shared  192.168.1.0/24(rw,wdelay,root_squash,no_subtree_check,...)
/srv/nfs/home    192.168.1.0/24(rw,wdelay,no_root_squash,no_subtree_check,...)
/srv/nfs/shared  10.0.0.5(ro,wdelay,root_squash,no_subtree_check,...)

Understanding Export Options

The /etc/exports file supports many options that control access and behavior:

  • rw / ro: Read-write or read-only access. Default is ro.
  • sync / async: sync forces the server to write changes to disk before replying, ensuring data integrity. async is faster but risks data loss on crash. Always use sync in production.
  • root_squash / no_root_squash: root_squash (default) maps remote root (UID 0) to the anonymous user, preventing root on clients from having root privileges on the share. Use no_root_squash only for trusted administrative hosts.
  • no_subtree_check: Disables subtree checking, which improves reliability when exporting subdirectories of a file system. Recommended for most configurations.
  • all_squash: Maps all remote users to the anonymous user. Useful for public shares.
  • anonuid / anongid: Sets the UID/GID for anonymous (squashed) users.

Mounting NFS Shares on Client Systems

On the client side, you need the nfs-common package to mount NFS shares. Mounting can be done manually with the mount command, persistently through /etc/fstab, or on-demand using autofs.

# Install NFS client utilities
$ sudo apt install nfs-common

# Discover exports from the server (NFSv3 only — showmount uses mountd)
$ showmount -e 192.168.1.10
Export list for 192.168.1.10:
/srv/nfs/shared  192.168.1.0/24
/srv/nfs/home    192.168.1.0/24

# Manual mount
$ sudo mkdir -p /mnt/shared
$ sudo mount -t nfs4 192.168.1.10:/srv/nfs/shared /mnt/shared

# Verify the mount
$ mount | grep nfs
192.168.1.10:/srv/nfs/shared on /mnt/shared type nfs4 (rw,relatime,...)

$ df -h /mnt/shared
Filesystem                      Size  Used Avail Use% Mounted on
192.168.1.10:/srv/nfs/shared     50G   12G   38G  24% /mnt/shared

# Persistent mount via /etc/fstab
$ sudo nano /etc/fstab
# NFS mounts
192.168.1.10:/srv/nfs/shared  /mnt/shared  nfs4  defaults,_netdev  0  0
192.168.1.10:/srv/nfs/home    /mnt/home    nfs4  rw,nosuid,_netdev 0  0

# Mount all entries from fstab
$ sudo mount -a

# Mount options for performance tuning
# rsize/wsize: Read/write block size (default 1048576 = 1MB)
# timeo: Timeout in tenths of a second for retransmission
# retrans: Number of retransmissions before giving up
# hard/soft: hard mount retries indefinitely, soft returns error
$ sudo mount -t nfs4 -o rsize=1048576,wsize=1048576,hard,timeo=600 
    192.168.1.10:/srv/nfs/shared /mnt/shared

The _netdev option in /etc/fstab is crucial for NFS mounts. It tells the system to wait for the network to be available before attempting to mount the file system, preventing boot failures when the NFS server is unreachable.

Automounting with autofs

Autofs provides on-demand mounting of NFS shares. Instead of mounting all shares at boot time, autofs mounts shares only when they are accessed and automatically unmounts them after a period of inactivity. This reduces network traffic and avoids boot delays when NFS servers are unavailable.

# Install autofs
$ sudo apt install autofs

# Configure the master map — defines mount points and their map files
$ sudo nano /etc/auto.master
# Mount point    Map file             Options
/mnt/nfs         /etc/auto.nfs        --timeout=300

# Configure the indirect map — defines individual shares
$ sudo nano /etc/auto.nfs
# Key        Options                            Location
shared       -rw,sync,nfs4                      192.168.1.10:/srv/nfs/shared
home         -rw,nosuid,nfs4                    192.168.1.10:/srv/nfs/home
projects     -ro,nfs4                           192.168.1.10:/srv/nfs/projects

# Restart autofs to apply changes
$ sudo systemctl restart autofs
$ sudo systemctl enable autofs

# Test — simply access the directory to trigger automount
$ ls /mnt/nfs/shared
file1.txt  file2.txt  documents/

# Verify automount is active
$ mount | grep autofs
/etc/auto.nfs on /mnt/nfs type autofs (rw,relatime,...)

# After 300 seconds of inactivity, the mount is automatically removed
$ mount | grep 192.168.1.10
# (empty — unmounted due to timeout)

# Direct maps — mount to specific absolute paths
$ sudo nano /etc/auto.master
/-  /etc/auto.direct

$ sudo nano /etc/auto.direct
/data/shared  -rw,nfs4  192.168.1.10:/srv/nfs/shared

Autofs supports two types of maps: indirect maps create subdirectories under a common mount point (e.g., /mnt/nfs/shared), while direct maps mount to specific absolute paths anywhere in the file system.

NFSv3 vs NFSv4 and Troubleshooting

While NFSv4 is recommended for new deployments, you may encounter NFSv3 in legacy environments. Understanding the differences helps with both migration and troubleshooting.

# Force a specific NFS version when mounting
$ sudo mount -t nfs -o vers=3 192.168.1.10:/srv/nfs/shared /mnt/shared   # NFSv3
$ sudo mount -t nfs4 192.168.1.10:/srv/nfs/shared /mnt/shared            # NFSv4

# NFSv3 requires additional services — check all RPC services
$ rpcinfo -p 192.168.1.10
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100005    3   tcp  20048  mountd
    100021    4   tcp  42135  nlock
    100024    1   tcp  43219  status

# NFSv4 ID mapping — important for correct permission display
$ sudo nano /etc/idmapd.conf
[General]
Domain = example.com

$ sudo systemctl restart nfs-idmapd

# Troubleshooting: check server-side logs
$ sudo journalctl -u nfs-server -f
$ sudo journalctl -u nfs-mountd -f

# Troubleshooting: check connectivity
$ sudo nfsstat -s           # Server statistics
$ sudo nfsstat -c           # Client statistics
$ sudo nfsstat -m           # Mount information on client

# Troubleshooting: check firewall
$ sudo ufw status
$ sudo ufw allow from 192.168.1.0/24 to any port 2049 proto tcp
$ sudo ufw allow from 192.168.1.0/24 to any port 111 proto tcp  # NFSv3 only

# Common issues:
# 1. "mount.nfs: access denied" — check /etc/exports and run exportfs -ra
# 2. "mount.nfs: No route to host" — check firewall rules
# 3. Permission denied on files — check UID/GID mapping and squash options
# 4. Stale file handle — unmount and remount, or use hard mount option

Key Takeaways

  • The NFS server is configured through /etc/exports; use exportfs -ra to apply changes without restarting the service.
  • Export options like root_squash, sync, and no_subtree_check control security and performance; understand each before deploying to production.
  • Client mounts can be manual, persistent via /etc/fstab (with _netdev), or on-demand via autofs for better resource utilization.
  • NFSv4 is strongly preferred over NFSv3 because it uses a single port (2049/tcp), has built-in locking, and supports Kerberos security.
  • Troubleshoot NFS issues by checking exports (exportfs -v), connectivity (rpcinfo), firewall rules, and UID/GID mapping (idmapd.conf).

What’s Next

In the next lesson, you will learn how to configure Samba as a file server on Ubuntu, enabling cross-platform file sharing between Linux and Windows systems using the SMB protocol.

繁體中文

概述

  • 學習目標:如何在 Ubuntu 上安裝和配置 NFS 伺服器、匯出目錄、在客戶端系統上掛載 NFS 共享,以及使用 autofs 進行按需自動掛載。
  • 先決條件:了解檔案共享協定(第 34 課)、基本的 Linux 網路和檔案權限知識
  • 預計閱讀時間:18 分鐘

簡介

網路檔案系統 (NFS) 允許您透過網路共享目錄,讓 Linux 和 Unix 客戶端可以像掛載本地檔案系統一樣掛載它們。這種透明存取使 NFS 在共享主目錄、集中式應用程式資料和叢集環境中的分散式儲存方面具有不可替代的價值。

NFS 多年來有了顯著的發展。NFSv3 雖然仍在使用,但需要多個守護程式連接埠和外部鎖定機制。NFSv4 將一切整合到單一 TCP 連接埠 (2049),整合了鎖定和安全性,並支援委託和複合操作等提升效能的功能。在本課程中,我們將主要關注 NFSv4,同時指出重要的 NFSv3 差異。

完成本課程後,您將能夠配置匯出目錄到特定客戶端的 NFS 伺服器,在客戶端機器上手動和自動掛載這些匯出,以及排除常見的 NFS 問題。

安裝和配置 NFS 伺服器

Ubuntu 上的 NFS 伺服器由 nfs-kernel-server 套件提供。安裝很簡單,伺服器在套件安裝後立即開始運行。核心配置檔案是 /etc/exports,它定義了哪些目錄被共享以及誰可以存取它們。

# 安裝 NFS 伺服器套件
$ sudo apt update
$ sudo apt install nfs-kernel-server

# 驗證 NFS 伺服器正在運行
$ sudo systemctl status nfs-server

# 建立要共享的目錄
$ sudo mkdir -p /srv/nfs/shared
$ sudo mkdir -p /srv/nfs/home
$ sudo chown nobody:nogroup /srv/nfs/shared

# 在 /etc/exports 中配置匯出
$ sudo nano /etc/exports
# 格式:目錄  客戶端(選項) [客戶端2(選項)]
/srv/nfs/shared  192.168.1.0/24(rw,sync,no_subtree_check)
/srv/nfs/home    192.168.1.0/24(rw,sync,no_root_squash,no_subtree_check)

# 套用匯出配置
$ sudo exportfs -ra

# 驗證活動匯出
$ sudo exportfs -v

了解匯出選項

  • rw / ro:讀寫或唯讀存取。預設為 ro
  • sync / async:sync 強制伺服器在回覆前將變更寫入磁碟,確保資料完整性。async 更快但在當機時有資料遺失風險。生產環境始終使用 sync
  • root_squash / no_root_squash:root_squash(預設)將遠端 root (UID 0) 映射到匿名使用者。僅對受信任的管理主機使用 no_root_squash
  • no_subtree_check:停用子樹檢查,在匯出檔案系統子目錄時提高可靠性。建議用於大多數配置。
  • all_squash:將所有遠端使用者映射到匿名使用者。適用於公開共享。

在客戶端系統上掛載 NFS 共享

在客戶端,您需要 nfs-common 套件來掛載 NFS 共享。掛載可以透過 mount 命令手動完成、透過 /etc/fstab 持久化,或使用 autofs 按需掛載。

# 安裝 NFS 客戶端工具
$ sudo apt install nfs-common

# 從伺服器探索匯出
$ showmount -e 192.168.1.10

# 手動掛載
$ sudo mkdir -p /mnt/shared
$ sudo mount -t nfs4 192.168.1.10:/srv/nfs/shared /mnt/shared

# 透過 /etc/fstab 持久掛載
192.168.1.10:/srv/nfs/shared  /mnt/shared  nfs4  defaults,_netdev  0  0

# 掛載所有 fstab 項目
$ sudo mount -a

/etc/fstab 中的 _netdev 選項對 NFS 掛載至關重要。它告訴系統在嘗試掛載檔案系統之前等待網路可用,防止在 NFS 伺服器無法連線時啟動失敗。

使用 autofs 自動掛載

Autofs 提供按需掛載 NFS 共享的功能。autofs 不在啟動時掛載所有共享,而是僅在存取時才掛載共享,並在閒置一段時間後自動卸載它們。這減少了網路流量並避免了 NFS 伺服器不可用時的啟動延遲。

# 安裝 autofs
$ sudo apt install autofs

# 配置主映射 — 定義掛載點及其映射檔案
$ sudo nano /etc/auto.master
/mnt/nfs         /etc/auto.nfs        --timeout=300

# 配置間接映射 — 定義個別共享
$ sudo nano /etc/auto.nfs
shared       -rw,sync,nfs4                      192.168.1.10:/srv/nfs/shared
home         -rw,nosuid,nfs4                    192.168.1.10:/srv/nfs/home

# 重新啟動 autofs 以套用變更
$ sudo systemctl restart autofs
$ sudo systemctl enable autofs

# 測試 — 只需存取目錄即可觸發自動掛載
$ ls /mnt/nfs/shared

Autofs 支援兩種類型的映射:間接映射在共同掛載點下建立子目錄,而直接映射掛載到檔案系統中任何位置的特定絕對路徑。

NFSv3 與 NFSv4 及故障排除

雖然 NFSv4 是新部署的建議選擇,但您可能在舊環境中遇到 NFSv3。了解差異有助於遷移和故障排除。

# 掛載時強制指定 NFS 版本
$ sudo mount -t nfs -o vers=3 192.168.1.10:/srv/nfs/shared /mnt/shared   # NFSv3
$ sudo mount -t nfs4 192.168.1.10:/srv/nfs/shared /mnt/shared            # NFSv4

# NFSv4 ID 映射 — 對正確的權限顯示很重要
$ sudo nano /etc/idmapd.conf
[General]
Domain = example.com

# 故障排除:檢查伺服器端日誌
$ sudo journalctl -u nfs-server -f

# 故障排除:檢查防火牆
$ sudo ufw allow from 192.168.1.0/24 to any port 2049 proto tcp

# 常見問題:
# 1. "mount.nfs: access denied" — 檢查 /etc/exports 並執行 exportfs -ra
# 2. "mount.nfs: No route to host" — 檢查防火牆規則
# 3. 檔案權限被拒絕 — 檢查 UID/GID 映射和 squash 選項
# 4. Stale file handle — 卸載並重新掛載

重點摘要

  • NFS 伺服器透過 /etc/exports 配置;使用 exportfs -ra 在不重新啟動服務的情況下套用變更。
  • root_squashsyncno_subtree_check 等匯出選項控制安全性和效能;在部署到生產環境之前了解每個選項。
  • 客戶端掛載可以是手動的、透過 /etc/fstab(使用 _netdev)持久化的,或透過 autofs 按需掛載以更好地利用資源。
  • 強烈建議使用 NFSv4 而非 NFSv3,因為它使用單一連接埠 (2049/tcp)、有內建鎖定機制,並支援 Kerberos 安全性。
  • 透過檢查匯出(exportfs -v)、連線性(rpcinfo)、防火牆規則和 UID/GID 映射(idmapd.conf)來排除 NFS 問題。

下一步

在下一課中,您將學習如何在 Ubuntu 上將 Samba 配置為檔案伺服器,使用 SMB 協定實現 Linux 和 Windows 系統之間的跨平台檔案共享。

日本語

概要

  • 学習内容:Ubuntu での NFS サーバーのインストールと設定、ディレクトリのエクスポート、クライアントシステムでの NFS 共有のマウント、autofs を使用したオンデマンド自動マウントの方法。
  • 前提条件:ファイル共有プロトコルの理解(レッスン34)、基本的な Linux ネットワーキングとファイルパーミッション
  • 推定読了時間:18分

はじめに

Network File System (NFS) を使用すると、ネットワーク経由でディレクトリを共有し、Linux および Unix クライアントがローカルファイルシステムと同様にマウントできます。この透過的なアクセスにより、NFS は共有ホームディレクトリ、集中アプリケーションデータ、クラスタ環境での分散ストレージにおいて非常に有用です。

NFS は長年にわたって大幅に進化してきました。NFSv3 はまだ使用されていますが、複数のデーモンポートと外部ロックメカニズムが必要です。NFSv4 はすべてを単一の TCP ポート (2049) に統合し、ロックとセキュリティを統合し、デリゲーションやパフォーマンスを向上させる複合操作などの機能をサポートしています。

このレッスンを終えると、特定のクライアントにディレクトリをエクスポートする NFS サーバーの設定、クライアントマシンでの手動および自動マウント、一般的な NFS 問題のトラブルシューティングができるようになります。

NFS サーバーのインストールと設定

Ubuntu の NFS サーバーは nfs-kernel-server パッケージで提供されます。コア設定ファイルは /etc/exports で、共有するディレクトリとアクセスできるユーザーを定義します。

# NFS サーバーパッケージのインストール
$ sudo apt update
$ sudo apt install nfs-kernel-server

# 共有するディレクトリの作成
$ sudo mkdir -p /srv/nfs/shared
$ sudo mkdir -p /srv/nfs/home
$ sudo chown nobody:nogroup /srv/nfs/shared

# /etc/exports でエクスポートを設定
$ sudo nano /etc/exports
/srv/nfs/shared  192.168.1.0/24(rw,sync,no_subtree_check)
/srv/nfs/home    192.168.1.0/24(rw,sync,no_root_squash,no_subtree_check)

# エクスポート設定の適用
$ sudo exportfs -ra

# アクティブなエクスポートの確認
$ sudo exportfs -v

エクスポートオプションの理解

  • rw / ro:読み書きまたは読み取り専用アクセス。デフォルトは ro
  • sync / async:sync はサーバーに応答前にディスクへの書き込みを強制し、データ整合性を確保します。本番環境では常に sync を使用。
  • root_squash / no_root_squash:root_squash(デフォルト)はリモート root (UID 0) を匿名ユーザーにマッピングします。信頼できる管理ホストにのみ no_root_squash を使用。
  • no_subtree_check:サブツリーチェックを無効にし、ファイルシステムのサブディレクトリをエクスポートする際の信頼性を向上させます。
  • all_squash:すべてのリモートユーザーを匿名ユーザーにマッピング。公開共有に有用。

クライアントシステムでの NFS 共有のマウント

クライアント側では、NFS 共有をマウントするために nfs-common パッケージが必要です。マウントは mount コマンドで手動、/etc/fstab で永続化、または autofs でオンデマンドで行えます。

# NFS クライアントユーティリティのインストール
$ sudo apt install nfs-common

# サーバーからのエクスポートを検出
$ showmount -e 192.168.1.10

# 手動マウント
$ sudo mkdir -p /mnt/shared
$ sudo mount -t nfs4 192.168.1.10:/srv/nfs/shared /mnt/shared

# /etc/fstab による永続マウント
192.168.1.10:/srv/nfs/shared  /mnt/shared  nfs4  defaults,_netdev  0  0

# fstab のすべてのエントリをマウント
$ sudo mount -a

/etc/fstab_netdev オプションは NFS マウントに不可欠です。NFS サーバーに到達できない場合のブート失敗を防ぎます。

autofs による自動マウント

Autofs は NFS 共有のオンデマンドマウントを提供します。ブート時にすべての共有をマウントする代わりに、autofs はアクセス時にのみ共有をマウントし、非アクティブ期間後に自動的にアンマウントします。

# autofs のインストール
$ sudo apt install autofs

# マスターマップの設定
$ sudo nano /etc/auto.master
/mnt/nfs         /etc/auto.nfs        --timeout=300

# 間接マップの設定
$ sudo nano /etc/auto.nfs
shared       -rw,sync,nfs4                      192.168.1.10:/srv/nfs/shared
home         -rw,nosuid,nfs4                    192.168.1.10:/srv/nfs/home

# autofs の再起動
$ sudo systemctl restart autofs
$ sudo systemctl enable autofs

# テスト — ディレクトリにアクセスするだけで自動マウントが発動
$ ls /mnt/nfs/shared

Autofs は 2 種類のマップをサポートしています:間接マップは共通のマウントポイント下にサブディレクトリを作成し、直接マップはファイルシステム内の任意の絶対パスにマウントします。

NFSv3 と NFSv4 およびトラブルシューティング

NFSv4 は新規デプロイメントに推奨されますが、レガシー環境では NFSv3 に遭遇する可能性があります。違いを理解することは、移行とトラブルシューティングの両方に役立ちます。

# マウント時に NFS バージョンを強制指定
$ sudo mount -t nfs -o vers=3 192.168.1.10:/srv/nfs/shared /mnt/shared   # NFSv3
$ sudo mount -t nfs4 192.168.1.10:/srv/nfs/shared /mnt/shared            # NFSv4

# NFSv4 ID マッピング
$ sudo nano /etc/idmapd.conf
[General]
Domain = example.com

# トラブルシューティング:サーバー側ログの確認
$ sudo journalctl -u nfs-server -f

# よくある問題:
# 1. "mount.nfs: access denied" — /etc/exports を確認し exportfs -ra を実行
# 2. "mount.nfs: No route to host" — ファイアウォールルールを確認
# 3. ファイルのパーミッション拒否 — UID/GID マッピングと squash オプションを確認
# 4. Stale file handle — アンマウントして再マウント

重要ポイント

  • NFS サーバーは /etc/exports で設定します。exportfs -ra でサービスを再起動せずに変更を適用できます。
  • root_squashsyncno_subtree_check などのエクスポートオプションはセキュリティとパフォーマンスを制御します。
  • クライアントマウントは手動、/etc/fstab_netdev 付き)による永続化、または autofs によるオンデマンドで行えます。
  • NFSv4 は単一ポート (2049/tcp) を使用し、組み込みロックと Kerberos セキュリティをサポートするため、NFSv3 よりも強く推奨されます。
  • NFS の問題は、エクスポート(exportfs -v)、接続性(rpcinfo)、ファイアウォールルール、UID/GID マッピング(idmapd.conf)を確認してトラブルシューティングします。

次のステップ

次のレッスンでは、Ubuntu で Samba をファイルサーバーとして設定し、SMB プロトコルを使用して Linux と Windows システム間のクロスプラットフォームファイル共有を実現する方法を学びます。

You Missed