Akemi

NFS权限与绑定挂载

2025/12/17

前情提要:
https://akemi.zj.cn/2025/08/26/Pacemaker-NFS/
忘记做nfs迁移测试了

后续的权限挂载问题

使用nfs-client 10.163.2.109挂载10.163.2.150后,发现写入失败,这个问题是因为nfs server共享后,虽然共享权限为rw,但server默认给client的权限为nobody匿名用户,为的是防止client使用root权限直接操作server文件。
(默认选项root_squash,即默认对root权限进行控制

有两种处理方法:

  • nfs server把挂载目录给777权限
  • nfs server共享时指定挂载选项no_root_squash

因为我们使用的是高可用nfs,/mnt/nfs目录不一定在哪个主机上,所以使用第二种方法,直接改pcs配置即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 修改前
[root@nfs-client ~]# mount -t nfs4 10.163.2.150:/mnt/nfs /mnt/hgfs/
[root@nfs-client ~]# ls /mnt/hgfs/
test-file.img
[root@nfs-client ~]# touch /mnt/hgfs/test
touch: cannot touch '/mnt/hgfs/test': Permission denied
[root@nfs-client ~]# chmod +x /mnt/hgfs
chmod: changing permissions of '/mnt/hgfs': Operation not permitted
[root@nfs-client ~]# chmod 777 -R /mnt/hgfs
chmod: changing permissions of '/mnt/hgfs': Operation not permitted
chmod: changing permissions of '/mnt/hgfs/test-file.img': Operation not permitted
chmod: changing permissions of '/mnt/hgfs/.rmtab': Operation not permitted

pcs resource update exportsdir \
clientspec="10.163.2.0/24" \
directory="/mnt/nfs" \
fsid="1" \
options="rw,sync,no_root_squash,no_subtree_check,fsid=1" \
unlock_on_stop="1"

# 修改后
mount -t nfs4 10.163.2.150:/mnt/nfs /mnt/nfs/
touch /mnt/nfs/test
ls /mnt/nfs/
test test-file.img

# /etc/fstab开机启动
10.163.2.150:/mnt/nfs /mnt/nfs nfs4 _netdev,rw,hard 0 0

绑定挂载

绑定挂载的好处:
路径灵活性:多个路径访问同一数据
无缝迁移:应用可以从旧路径逐步迁移到新路径
兼容性:不破坏现有应用
多视图:同一数据在不同路径可以有不同权限(通过mount -o bind,ro)

1
2
3
4
5
mkdir /opt/nfs

mount --bind /mnt/nfs /opt/nfs
ls /opt/nfs/
test test-file.img
CATALOG
  1. 1. 后续的权限挂载问题
  2. 2. 绑定挂载