NFS,是Network File System的简写,即网络文件系统。网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS. NFS允许一个系统在网络上与他人共享目录和文件。 通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件。

服务端:

#!/bin/bash

# install nfs installation package
yum install nfs-utils -y

# start service
systemctl enable rpcbind.service && systemctl enable nfs-server.service && systemctl start rpcbind.service && systemctl start nfs-server.service

# adding firewall rules
# firewall-cmd --zone=public --permanent --add-service=rpc-bind
# firewall-cmd --zone=public --permanent --add-service=mountd
# firewall-cmd --zone=public --permanent --add-service=nfs
# firewall-cmd --reload

# create an nfs shared directory
mkdir -p /data/nfs

# example change the permission on a shared directory
chmod 777 /data/nfs

# configure nfs directory properties
echo "/data/nfs/ *(insecure,rw,sync,no_root_squash,no_all_squash)" >>/etc/exports

# refresh nfs directory configuration
exportfs -r

# restart nfs service
systemctl restart nfs-server.service

# check the local nfs shared directory
showmount -e localhost

客户端:

#!/bin/bash

# install an nfs client
yum install nfs-utils -y

# start rpcbind service
systemctl enable rpcbind.service && systemctl start rpcbind.service

# input nfs server address
read -r -e -p "Please enter the ip address of the nfs server:" ip
read -r -e -p "please enter the shared directory path of the nfs server:" nfs_path
read -r -e -p "please enter the directory path mounted to the current server:" local_path

# create a locally mounted directory
mkdir -p "$local_path"

# show shared directory of nfs server
showmount -e "$ip"

# mount the shared directory of the nfs server
mount -t nfs "$ip":"$nfs_path" "$local_path"

# add an nfs shared directory to automatic mount upon startup
echo "$ip:$nfs_path $local_path nfs defaults  0 0" >>/etc/fstab

# reload the system configuration
systemctl daemon-reload

# display the mount directory
mount | grep "$ip"

权限说明:

  • rw :读写
  • ro :只读
  • sync :同步模式,内存中数据时时写入磁盘
  • async :不同步,把内存中数据定期写入磁盘中
  • secure :NFS通过1024以下的安全TCP/IP端口发送
  • insecure :NFS通过1024以上的端口发送
  • no_root_squash :加上这个选项后,Root用户就会对共享的目录拥有至高的权限控制,就像是对本机的目录操作一样。不安全,不建议使用
  • root_squash :和上面的选项对应,Root用户对共享目录的权限不高,只有普通用户的权限,即限制了root
  • subtree_check :如果共享/usr/bin之类的子目录时,强制nfs检查父目录的权限(默认)
  • no_subtree_check :和上面相对,不检查父目录权限
  • all_squash :不管使用NFS的用户是谁,他的身份都会被限定成为一个指定的普通用户身份
  • anonuid/anongid :要和root_squash 以及 all_squash一同使用,用于指定使用NFS的用户限定后的UID和GID,前提是本机的/etc/passwd中存在这个UID和GID