Mục tiêu chương
Nắm vững cách cấu hình NFS server và client trên Linux
Cài đặt NFS Server
Cài gói nfs-utils, khởi động nfs-server service với systemctl, kiểm tra rpcbind và các NFS daemon đang chạy đúng.
/etc/exports
Cấu hình file /etc/exports với cú pháp: Directory Host(Options). Hiểu các options: rw/ro, root_squash, all_squash, no_subtree_check.
exportfs Command
Dùng exportfs -arv để export ngay sau khi sửa /etc/exports. showmount -e để xem danh sách shares từ client.
Mount NFS Client
Mount NFS share với mount command, cấu hình /etc/fstab cho auto-mount lúc boot với options nfsvers=4, _netdev.
autofs
Cấu hình autofs để tự động mount NFS shares khi truy cập và umount sau khi không dùng, tiết kiệm tài nguyên hệ thống.
Firewall & SELinux NFS
Mở ports 2049 (nfs), 111 (rpcbind), 20048 (mountd) trong firewall. Cấu hình SELinux Booleans: nfs_export_all_ro/rw.
Lý thuyết
Cài đặt và khởi động NFS Server
NFS cho phép chia sẻ filesystem giữa các Linux systems qua mạng. Gói chính là nfs-utils. Service nfs-server tự động khởi động rpcbind nếu cần.
# Fedora / RHEL
$ sudo dnf install nfs-utils
Installed: nfs-utils-2.8.1-1.fc42.x86_64
# Start và enable NFS server
$ sudo systemctl start nfs-server
$ sudo systemctl enable nfs-server
$ sudo systemctl status nfs-server
● nfs-server.service - NFS server and services
Loaded: loaded (enabled; preset: disabled)
Active: active (exited) since Mon 2025-03-09 15:15:11 EDT
# Xem thông tin package
$ rpm -qi nfs-utils | grep -E "Name|Version"
Name : nfs-utils
Version : 2.8.1
$ rpm -ql nfs-utils | grep bin
/usr/sbin/exportfs
/usr/sbin/showmount
/usr/sbin/nfsd
Cấu hình /etc/exports
File /etc/exports xác định các thư mục được chia sẻ qua NFS. Cú pháp: Directory Host(Options) Host(Options). Chú ý: không có space giữa hostname và dấu ngoặc options.
# Chia sẻ /cal cho toàn domain, read/write
/cal *.linuxtoys.net(rw) # Company events
# Chia sẻ /pub cho mọi người, read-only
/pub *(ro,insecure,all_squash) # Public dir
# Chia sẻ /home chỉ cho 2 hosts cụ thể
/home maple(rw,root_squash) spruce(rw,root_squash)
# Các options phổ biến:
# rw - read/write access
# ro - read-only (mặc định)
# root_squash - root client mapped to nobody (mặc định)
# no_root_squash - root giữ quyền root
# all_squash - tất cả users mapped to nobody
# insecure - cho phép port cao hơn 1024
# sync - write đồng bộ (an toàn hơn)
# async - write không đồng bộ (nhanh hơn)
# no_subtree_check - tắt kiểm tra subtree
exportfs và showmount
Sau khi sửa /etc/exports, chạy exportfs để áp dụng ngay (không cần restart NFS). Từ client, dùng showmount để xem shares có sẵn.
# Export tất cả entries trong /etc/exports
$ sudo exportfs -arv
exporting maple:/pub
exporting spruce:/pub
exporting maple:/home
exporting *:/mnt/win
# Xem danh sách đang export hiện tại
$ exportfs -v
/pub *(ro,wdelay,insecure,root_squash,no_subtree_check)
/home maple(rw,wdelay,root_squash,no_subtree_check)
# Từ client: xem NFS shares từ server
$ showmount -e fedora.example.com
Export list for fedora.example.com:
/pub *
/cal *
/home ubuntu
Mount NFS từ Client
Client mount NFS share bằng lệnh mount giống như mount local disk, nhưng dùng cú pháp server:/directory. Thêm vào /etc/fstab để mount tự động lúc boot.
# Tạo mount point
$ sudo mkdir /mnt/maple
# Mount NFS share thủ công
$ sudo mount maple:/stuff /mnt/maple
$ df -h /mnt/maple
Filesystem Size Used Avail Use% Mounted on
maple:/stuff 50G 10G 40G 20% /mnt/maple
# Mount với options cụ thể
$ sudo mount -t nfs4 -o ro,nosuid maple:/pub /mnt/pub
# Umount khi xong
$ sudo umount /mnt/maple
# /etc/fstab cho auto-mount (thêm dòng này)
maple:/stuff /mnt/maple nfs defaults,_netdev 0 0
# Mount tất cả trong fstab
$ sudo mount -a
autofs — Tự động mount NFS
autofs tự động mount NFS share khi truy cập và umount sau khoảng 5 phút không dùng, tiết kiệm tài nguyên. Cấu hình qua /etc/auto.master và các map files.
# Cài autofs
$ sudo dnf install autofs
# Cấu hình /etc/auto.master
/mnt/nfs /etc/auto.nfs --timeout=300
# Tạo /etc/auto.nfs (map file)
data -rw,soft maple:/data
pub -ro maple:/pub
# Khởi động autofs
$ sudo systemctl enable --now autofs
# Truy cập tự động mount (không cần mount thủ công)
$ ls /mnt/nfs/data
[autofs tự động mount maple:/data vào /mnt/nfs/data]
$ ls /mnt/nfs/pub
[autofs tự động mount maple:/pub vào /mnt/nfs/pub]
Firewall và SELinux cho NFS
NFS dùng nhiều ports: 2049 (NFS), 111 (rpcbind), 20048 (mountd). SELinux có Booleans mặc định bật cho phép NFS sharing.
# Mở firewall cho NFS (bao gồm mountd, nfs, rpcbind)
$ sudo firewall-cmd --permanent --add-service=nfs
$ sudo firewall-cmd --permanent --add-service=mountd
$ sudo firewall-cmd --permanent --add-service=rpc-bind
$ sudo firewall-cmd --reload
# Kiểm tra SELinux Booleans NFS
$ getsebool -a | grep nfs
nfs_export_all_ro --> on [mặc định bật]
nfs_export_all_rw --> on [mặc định bật]
use_nfs_home_dirs --> off [cần bật nếu share home]
# Bật share home directories qua NFS
$ sudo setsebool -P use_nfs_home_dirs on
# Set context cho thư mục NFS custom
$ sudo semanage fcontext -a -t public_content_rw_t "/mydata(/.*)?"
$ sudo restorecon -Rv /mydata
Lab Thực Hành
Kịch bản: Server NFS chia sẻ /var/nfsshare, client mount vào /mnt/nfs và cấu hình auto-mount trong /etc/fstab
Cài và khởi động NFS Server
$ sudo dnf install -y nfs-utils
$ sudo systemctl enable --now nfs-server
$ sudo systemctl status nfs-server | grep Active
Active: active (exited)
Tạo thư mục và cấu hình /etc/exports
$ sudo mkdir -p /var/nfsshare
$ sudo chmod 777 /var/nfsshare
$ echo "NFS Test File" | sudo tee /var/nfsshare/test.txt
$ sudo nano /etc/exports
/var/nfsshare 192.168.1.0/24(rw,sync,no_subtree_check)
$ sudo exportfs -arv
exporting 192.168.1.0/24:/var/nfsshare
Mở firewall cho NFS
$ sudo firewall-cmd --permanent --add-service={nfs,mountd,rpc-bind}
success
$ sudo firewall-cmd --reload
$ sudo firewall-cmd --list-services | grep nfs
mountd nfs rpc-bind ssh
Kiểm tra từ client với showmount
$ showmount -e 192.168.1.100
Export list for 192.168.1.100:
/var/nfsshare 192.168.1.0/24
Mount NFS share trên client
$ sudo mkdir /mnt/nfs
$ sudo mount 192.168.1.100:/var/nfsshare /mnt/nfs
$ ls /mnt/nfs
test.txt
$ cat /mnt/nfs/test.txt
NFS Test File
$ df -h | grep nfs
192.168.1.100:/var/nfsshare 50G 10G 40G 20% /mnt/nfs
Cấu hình auto-mount trong /etc/fstab
192.168.1.100:/var/nfsshare /mnt/nfs nfs defaults,_netdev 0 0
$ sudo umount /mnt/nfs
$ sudo mount -a
$ df -h | grep nfs
192.168.1.100:/var/nfsshare 50G 10G 40G 20% /mnt/nfs
Câu hỏi ôn tập
Cú pháp của file /etc/exports là gì? Cho ví dụ chia sẻ /data cho subnet 192.168.1.0/24 với quyền rw.
Cú pháp: Directory Host(Options). Ví dụ: /data 192.168.1.0/24(rw,sync,no_subtree_check). Lưu ý quan trọng: không có space giữa hostname/IP và dấu ngoặc options. Nếu có space, NFS sẽ hiểu là hai entries riêng biệt.
Lệnh nào để áp dụng thay đổi /etc/exports ngay mà không cần restart NFS?
sudo exportfs -arv — -a: export tất cả entries, -r: resync với /etc/exports (gỡ bỏ entries đã xóa), -v: verbose output. Chạy lệnh này sau mỗi lần sửa /etc/exports. Nếu có lỗi cú pháp, exportfs sẽ báo ngay.
root_squash có nghĩa là gì? Khi nào dùng no_root_squash?
root_squash (mặc định): root user trên client được map thành nobody (UID 65534) trên server, ngăn root client có full quyền trên NFS share. no_root_squash: root client giữ quyền root trên server — chỉ dùng trong môi trường tin cậy hoàn toàn vì rủi ro bảo mật cao.
Lệnh showmount dùng để làm gì? Cú pháp để xem NFS shares từ server?
showmount -e servername — liệt kê tất cả NFS shared directories từ server đó. Ví dụ: showmount -e 192.168.1.100. Cũng có thể dùng showmount -a để xem client nào đang mount shares.
Cú pháp /etc/fstab để auto-mount NFS share là gì? Option _netdev dùng để làm gì?
Cú pháp: server:/share /mnt/point nfs defaults,_netdev 0 0. Option _netdev báo cho hệ thống biết filesystem này cần network — systemd sẽ chờ network up trước khi mount, tránh lỗi timeout lúc boot khi network chưa sẵn sàng.
autofs khác gì với /etc/fstab cho NFS? Ưu điểm của autofs?
/etc/fstab mount tất cả lúc boot (dù không dùng). autofs chỉ mount khi có truy cập và tự umount sau thời gian không dùng (timeout). Ưu điểm: tiết kiệm tài nguyên, không bị block khi server NFS down lúc boot, phù hợp cho nhiều shares ít dùng.
Các ports nào cần mở cho NFS server? Lệnh firewall-cmd nào để mở?
- Port 2049 TCP/UDP: NFS daemon (nfsd)
- Port 111 TCP/UDP: rpcbind/portmapper
- Port 20048 TCP/UDP: mountd (cho showmount)
Lệnh: firewall-cmd --permanent --add-service={nfs,mountd,rpc-bind}
SELinux Booleans nào liên quan đến NFS? Mặc định chúng ở trạng thái nào?
- nfs_export_all_ro: on (mặc định) — cho phép share read-only qua NFS
- nfs_export_all_rw: on (mặc định) — cho phép share read/write qua NFS
- use_nfs_home_dirs: off (mặc định) — cần bật để share /home qua NFS
Bật: setsebool -P use_nfs_home_dirs on