萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Linux 伺服器間 NFS 檔案共享完整配置指南

架構角色

  • NFS Server:匯出(export)目錄提供共享
  • NFS Client:掛載(mount)遠端目錄至本地路徑

Server 端配置

1. 安裝套件

# CentOS/RHEL
yum install nfs-utils rpcbind -y

# Debian/Ubuntu
apt-get install nfs-kernel-server -y

2. 啟用服務

systemctl enable --now rpcbind nfs-server
# 或舊版
service rpcbind start
service nfs start

3. 驗證服務監聽

netstat -ntpl | grep -E '(111|2049)'
# 111: rpcbind (portmapper)
# 2049: nfsd

4. 編輯匯出設定 /etc/exports

/home/temp *(ro,sync,no_root_squash,all_squash)
/mnt/inst *(ro,sync)

格式:<共享路徑> <允許客戶端>(<選項>)

常用選項對照表

選項說明
rw / ro讀寫 / 唯讀
sync / async同步寫入磁碟 / 非同步(效能較好但掉電風險)
no_root_squash客戶端 root 保持 root 權限(不安全,慎用)
root_squash (預設)客戶端 root 映射為匿名用戶 (nfsnobody)
all_squash所有用戶映射為匿名用戶
anonuid=XXX / anongid=XXX指定匿名用戶 UID/GID
insecure允許非保留埠口(>1024)發起請求

5. 套用匯出設定

exportfs -arv  # -a 全部、-r 重新匯出、-v 詳細輸出

6. 驗證匯出列表

showmount -e 127.0.0.1
# 或
exportfs -v

Client 端掛載

1. 安裝客戶端工具

yum install nfs-utils -y   # 含 mount.nfs
apt-get install nfs-common -y

2. 掛載指令

# 臨時掛載
mount -t nfs 192.168.111.1:/mnt/inst /mnt

# 開機自動掛載 (/etc/fstab)
192.168.111.1:/mnt/inst  /mnt  nfs  defaults,_netdev  0  0

3. 驗證

mount | grep nfs
df -h /mnt
ls /mnt

防火牆與 SELinux

  • Server 需開放:TCP/UDP 111 (rpcbind)、2049 (nfs)、20048 (mountd) — firewall-cmd --add-service=nfs --permanent
  • SELinux:setsebool -P nfs_export_all_rw 1 或設定正確 context

效能調優建議

  • Client 掛載加參數:rsize=1048576,wsize=1048576,hard,intr,timeo=600
  • Server 端 /etc/sysconfig/nfs 調整 RPCNFSDCOUNT(執行緒數)
  • 專用網路介面隔離 NFS 流量
copyright © 萬盛學電腦網 all rights reserved