安装服务
centos/fedor
安装
yum -y install nfs-utils rpcbind
配置/etc/exports
mkdir /share
chown nobody:nobody /share
vim /etc/exports
/share *(rw,no_root_squash,no_all_squash,sync,anonuid=99,anongid=99)
exportfs -r
ubuntu
安装
apt install -y nfs-kernel-server cifs-utils
配置/etc/exports
mkdir /share
chown nobody:nogroup /share
vim /etc/exports
#20.04
/share *(rw,no_root_squash,no_all_squash,sync,anonuid=99,anongid=99)
#22.04
/share *(rw,no_root_squash,no_all_squash,no_subtree_check,sync,anonuid=99,anongid=99)
exportfs -r
启动服务
centos/fedora
systemctl enable rpcbind.service
systemctl enable nfs.service
systemctl start rpcbind
systemctl start nfs
ubuntu
systemctl restart nfs-kernel-server
注意:
必须重新启动nfs的服务器,否则可能会出现莫名其妙的问题
客户端使用
命令挂载
mount -t nfs localhost:/share ./tt/
mount -t nfs 192.168.3.2:/volume1/kvm_data_disk /data
......
系统自动挂载
vim /etc/fstab
ubuntu:/data /data nfs rsize=8192,wsize=8192,timeo=14,intr 0 0
mount -a
常见错误解决
mac电脑中使用命令挂载
如果你想在mac电脑中使用mount命令连接到nfs服务端的话,必须在exports中配置insecure选项,如下:
/share *(rw,no_root_squash,no_all_squash,sync,anonuid=1000,anongid=1000,insecure)
否则会报如下错误:
mount_nfs: can't mount /share from s2 onto /Users/liyanqiao/docker/mariadb/bak/tt: Operation not permitted
/ext/exports的配置说明
- rw: read-write 可读写
- ro: read-only 只读
- sync: 文件同时写入硬盘和内存
- async: 文件暂存于内存,而不是直接写入内存
- no_root_squash: NFS客户端连接服务器端时如果使用的是root用户的话,那么对服务端分享的目录来说, 也拥有root权限,显然开启这一项是不安全的。
- root_squash: NFS客户端连接服务器端时如果使用的是root用户的话,那么对服务端分享的目录来说,拥 有匿名用户权限,通常他将使用nobody或nfsnobody身份。
- all_squash: 不论NFS客户端连接服务端时使用什么用户,对服务端分享的目录来说都是拥有匿名用户权限。
- anonuid: 匿名用户的UID的值
- anongid: 匿名用户的GID的值
- secure: 限制客户端只能从小于1024的tcp/ip端口连接nfs服务器(默认设置);
- insecure:允许客户端从大于1024的tcp/ip端口连接服务器;
- sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;
- async:将数据先保存在内存缓冲区中,必要时才写入磁盘;
- wdelay:检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率(默认设置);
- no_wdelay:若有写操作则立即执行,应与sync配合使用; subtree:若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置);
- no_subtree:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;