ติดตั้ง NFS Server บน VPS Linux แชร์ไฟล์ในเครือข่าย
NFS (Network File System) คือโปรโตคอลแชร์ไฟล์มาตรฐานบน Linux ที่ช่วยให้ VPS หลายเครื่องใช้ไฟล์ร่วมกันราวกับอยู่ใน local disk เหมาะสำหรับ shared storage ระหว่าง app server, web cluster หรือ backup storage ที่ต้องการ performance ดีกว่า Samba ใน Linux environment
บทความนี้ครอบคลุมการติดตั้ง NFS Server บน Ubuntu 22.04/24.04 และ Debian 12 ตั้งค่า /etc/exports, mount ฝั่ง client, ตั้ง autofs และ hardening เพื่อความปลอดภัย
สถาปัตยกรรม NFS
NFS ทำงานแบบ client-server:
- NFS Server — VPS ที่มี storage จริง export directory ให้ client mount
- NFS Client — VPS ที่ mount directory ของ server มาใช้งานเหมือน local
- RPC (portmapper) — ใช้ port 2049 (NFS) + 111 (rpcbind) เป็นหลัก
ติดตั้ง NFS Server (Ubuntu/Debian)
1. ติดตั้ง package
# อัพเดท package list
sudo apt update
# ติดตั้ง NFS kernel server
sudo apt install -y nfs-kernel-server
# ตรวจสอบ service
sudo systemctl status nfs-kernel-server
2. สร้าง directory ที่จะแชร์
# สร้าง directory สำหรับแชร์
sudo mkdir -p /srv/nfs/shared
sudo mkdir -p /srv/nfs/data
# กำหนด ownership (ถ้า client ใช้ nobody/nogroup)
sudo chown nobody:nogroup /srv/nfs/shared
sudo chmod 755 /srv/nfs/shared
# ตรวจสอบ
ls -la /srv/nfs/
3. ตั้งค่า /etc/exports
sudo nano /etc/exports
เพิ่ม exports ตามต้องการ:
# แชร์ให้ IP เดียว (แนะนำสำหรับ production)
/srv/nfs/shared 192.168.1.100(rw,sync,no_subtree_check,no_root_squash)
# แชร์ให้ทั้ง subnet
/srv/nfs/data 192.168.1.0/24(rw,sync,no_subtree_check)
# read-only สำหรับ backup client
/srv/nfs/shared 192.168.1.200(ro,sync,no_subtree_check)
คำอธิบาย export options:
| Option | ความหมาย |
|---|---|
rw | อ่านและเขียนได้ |
ro | อ่านได้อย่างเดียว |
sync | เขียน disk ทันที (ปลอดภัยกว่า async) |
no_subtree_check | ปิด subtree checking (ลด overhead) |
no_root_squash | root บน client = root บน server |
root_squash | root บน client กลายเป็น nobody (default, ปลอดภัยกว่า) |
all_squash | ทุก user map เป็น anonymous |
4. Apply exports และ restart
# โหลด exports ใหม่
sudo exportfs -a
# Restart service
sudo systemctl restart nfs-kernel-server
sudo systemctl enable nfs-kernel-server
# ดู exports ที่ active
sudo exportfs -v
ตั้งค่า Firewall
# เปิด port NFSv4 เฉพาะ IP client
sudo ufw allow from 192.168.1.100 to any port 2049
sudo ufw allow from 192.168.1.0/24 to any port 2049
# ถ้าใช้ NFSv3 ด้วย ต้องเปิด rpcbind
sudo ufw allow from 192.168.1.0/24 to any port 111
sudo ufw reload
sudo ufw status
ติดตั้งและ Mount บน NFS Client
ติดตั้ง NFS client
# Ubuntu/Debian client
sudo apt install -y nfs-common
# CentOS/RHEL client
sudo yum install -y nfs-utils
Mount ทดสอบ
# สร้าง mount point
sudo mkdir -p /mnt/nfs-shared
# Mount ทดสอบ
sudo mount -t nfs 192.168.1.10:/srv/nfs/shared /mnt/nfs-shared
# ทดสอบเขียนไฟล์
echo "NFS test" | sudo tee /mnt/nfs-shared/test.txt
cat /mnt/nfs-shared/test.txt
# ดู mount info
df -h /mnt/nfs-shared
mount | grep nfs
Mount อัตโนมัติด้วย /etc/fstab
# เพิ่มใน /etc/fstab
sudo nano /etc/fstab
# NFS mount (NFSv4)
192.168.1.10:/srv/nfs/shared /mnt/nfs-shared nfs4 defaults,_netdev,rw,hard,timeo=600,retrans=2 0 0
# ทดสอบ fstab
sudo mount -a
df -h | grep nfs
Mount อัตโนมัติด้วย autofs (ดีกว่า fstab สำหรับ network)
# ติดตั้ง autofs
sudo apt install -y autofs
# แก้ไข /etc/auto.master
sudo nano /etc/auto.master
# เพิ่มบรรทัดนี้
/mnt/nfs /etc/auto.nfs --timeout=60
# สร้าง /etc/auto.nfs
sudo nano /etc/auto.nfs
# format: mount_point options server:export
shared -rw,soft,intr 192.168.1.10:/srv/nfs/shared
data -ro 192.168.1.10:/srv/nfs/data
# Restart autofs
sudo systemctl restart autofs
sudo systemctl enable autofs
# ทดสอบ — autofs mount เมื่อเข้าถึง directory
ls /mnt/nfs/shared/
Performance Tuning
# Mount options สำหรับ performance
# rsize/wsize = buffer ขนาดใหญ่ขึ้น (แนะนำ 1M)
sudo mount -t nfs4 -o rsize=1048576,wsize=1048576,hard,timeo=600 \
192.168.1.10:/srv/nfs/shared /mnt/nfs-shared
# ดู NFS stats
nfsstat -c # client stats
nfsstat -s # server stats
# ดู latency
iostat -xm 1 5
async แทน sync ใน exports ถ้าต้องการ write performance สูงสุด แต่มีความเสี่ยงข้อมูลหายหาก server crash ก่อน flush disk — ใช้กับ data ที่ recreate ได้เท่านั้นตรวจสอบว่า NFS Server ทำงานถูกต้อง
# ดู service status
sudo systemctl status nfs-kernel-server
# ดู exports ที่ active พร้อม client info
sudo exportfs -v
# ดู NFS connections ปัจจุบัน
sudo showmount -e localhost
# ดูจาก client ว่า server export อะไรบ้าง
showmount -e 192.168.1.10
# ตรวจสอบ RPC services
rpcinfo -p localhost
# ดู NFS server stats
cat /proc/net/rpc/nfsd
nfsstat -s ดู server statistics และ nfsiostat ดู I/O throughput แบบ real-time หาก mount ค้าง ให้ตรวจสอบ firewall และ rpcbind service ก่อนเสมอแก้ปัญหาที่พบบ่อย
Error: "mount.nfs: Connection timed out"
เกิดจาก firewall บน server บล็อก port 2049 หรือ NFS service ไม่ทำงาน
# ตรวจสอบ NFS service บน server
sudo systemctl status nfs-kernel-server
# ตรวจสอบ port 2049
sudo ss -tlnp | grep 2049
sudo ufw status | grep 2049
# เปิด port ถ้าปิดอยู่
sudo ufw allow from CLIENT_IP to any port 2049
sudo ufw reload
Error: "mount.nfs: access denied by server while mounting"
IP ของ client ไม่ตรงกับที่กำหนดใน /etc/exports
# ตรวจสอบ IP client จริง (อาจเป็น public IP หรือ private)
# บน client:
ip addr show
curl ifconfig.me
# ตรวจสอบ exports บน server
sudo exportfs -v
# แก้ไข /etc/exports ให้ตรง IP แล้ว reload
sudo nano /etc/exports
sudo exportfs -ra
# ดู log สำหรับ debug
sudo tail -f /var/log/syslog | grep nfs
Error: "Stale file handle"
Server restart หรือ export เปลี่ยน ทำให้ mount ที่ client ไม่ valid
# Unmount force บน client
sudo umount -f -l /mnt/nfs-shared
# Mount ใหม่
sudo mount -t nfs4 192.168.1.10:/srv/nfs/shared /mnt/nfs-shared
Error: "rpc.statd is not running"
NFS locking daemon ไม่ทำงาน (ต้องการสำหรับ NFSv3)
# Start rpcbind และ nfs-common
sudo systemctl start rpcbind
sudo systemctl enable rpcbind
sudo systemctl restart nfs-common
# สำหรับ NFSv4 ไม่ต้องการ rpc.statd
# ใช้ mount -t nfs4 แทน nfs
Performance ช้า / Latency สูง
buffer size เล็กเกินไปหรือ network latency สูง
# เพิ่ม rsize/wsize เป็น 1MB
sudo umount /mnt/nfs-shared
sudo mount -t nfs4 -o rsize=1048576,wsize=1048576,hard \
192.168.1.10:/srv/nfs/shared /mnt/nfs-shared
# วัด throughput
dd if=/dev/zero of=/mnt/nfs-shared/test.bin bs=1M count=100 oflag=direct
rm /mnt/nfs-shared/test.bin
คำถามที่พบบ่อย
NFS กับ Samba ต่างกันอย่างไร ควรใช้อันไหน?
NFS ออกแบบมาสำหรับ Linux-to-Linux มี performance ดีกว่าและ overhead ต่ำกว่า เหมาะกับ server-to-server sharing ใน datacenter Samba เหมาะกว่าเมื่อต้องแชร์ไฟล์กับ Windows client หรือต้องการ authentication แบบ Active Directory
NFS ปลอดภัยไหม? ต้องทำอะไรเพิ่ม?
NFS v4 ปลอดภัยกว่า v3 มาก แต่ยังไม่มี encryption ใน transport layer ถ้าต้องการ encryption ควรใช้ NFS over VPN (WireGuard/OpenVPN) หรือ Kerberos (NFSv4 with sec=krb5) สิ่งสำคัญที่สุดคือจำกัด export ให้เฉพาะ IP ที่ต้องการ ไม่ export ไปทั้ง internet
mount ค้างหลัง server reboot ต้องแก้ยังไง?
ใช้ option soft แทน hard ใน fstab ให้ timeout แทนค้าง หรือใช้ _netdev option เพื่อให้ OS รู้ว่าเป็น network mount และรอ network ขึ้นก่อน ถ้าใช้ autofs จะ handle กรณีนี้ได้อัตโนมัติ เพราะ mount เมื่อมีการเข้าถึงจริงเท่านั้น