NFS服务配置

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

14.1 服务端配置NFS

在CentOS上使用NFS服务需要安装两个包(nfs-utils和rpcbind),使用yum安装nfs-utils时会一并安装rpcbind。

[root@localhost ~]# yum install -y nfs-utils

CentOS 6之前的版本都是安装portmap包,从CentOS开始,改为安装rpcbind包了。

配置NFS,需要编辑配置文件 /etc/exports

  • 首先修改配置文件(默认该文件为空):
[root@localhost ~]# vim /etc/exports        //写入下面内容
/home/nfstestdir 192.168.100.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

上面配置文件就一行,分为三部分:
1.本地要共享出去的目录
2.允许访问的主机(可以是一个IP,也可以是IP段)
3.小括号里面是一些权限选项

rw:表示读/写
ro:表示只读
sync:同步模式,表示内存中的数据实时写入磁盘
async:非同步模式,表示内存中的数据定期写入磁盘
no_root_squash:加上该选项后,root用户就会对共享的目录拥有最高权限,像操作本机的目录一样
root_squash:与上面对应,表示root用户对共享目录的权限不高,只有普通用户的权限
all_squash:表示不管使用NFS的用户是谁,其身份都被限定为一个指定的普通用户身份
anonuid/anongid:必须和root_squash以及all_squash选项一同使用,用于指定使用NFS的用户被限定后的uid和gid,前提是本机的/etc/passwd中存在相应的uid和gid

  • 创建相关目录并启动NFS服务:
[root@localhost ~]# vim /etc/exports
[root@localhost ~]# mkdir /home/nfstestdir
[root@localhost ~]# systemctl start rpcbind       //启动NFS服务之前,要先启动rpcbind服
[root@localhost ~]# ps aux |grep rpc
rpc        2487  0.0  0.0  65000  1416 ?        Ss   20:36   0:00 /sbin/rpcbind -w
rpcuser    2506  0.0  0.0  42420  1756 ?        Ss   20:36   0:00 /usr/sbin/rpc.statd
root       2507  0.0  0.0      0     0 ?        S<   20:36   0:00 [rpciod]
root       2513  0.0  0.0  19360   404 ?        Ss   20:36   0:00 /usr/sbin/rpc.idmapd
root       2526  0.0  0.0  42608   940 ?        Ss   20:36   0:00 /usr/sbin/rpc.mountd
root       2600  0.0  0.0 112720   984 pts/0    S+   20:45   0:00 grep --color=auto rpc
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# ps aux |grep  nfs
root       2536  0.0  0.0      0     0 ?        S<   20:36   0:00 [nfsd4_callbacks]
root       2542  0.0  0.0      0     0 ?        S    20:36   0:00 [nfsd]
root       2543  0.0  0.0      0     0 ?        S    20:36   0:00 [nfsd]
root       2544  0.0  0.0      0     0 ?        S    20:36   0:00 [nfsd]
root       2545  0.0  0.0      0     0 ?        S    20:36   0:00 [nfsd]
root       2546  0.0  0.0      0     0 ?        S    20:36   0:00 [nfsd]
root       2547  0.0  0.0      0     0 ?        S    20:36   0:00 [nfsd]
root       2548  0.0  0.0      0     0 ?        S    20:36   0:00 [nfsd]
root       2549  0.0  0.0      0     0 ?        S    20:36   0:00 [nfsd]
root       2602  0.0  0.0 112720   984 pts/0    R+   20:46   0:00 grep --color=auto nfs
[root@localhost ~]# systemctl enable rpcbind       //使其开机启动
[root@localhost ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

14.2 客户端挂在NFS

本实验需要两台机器,我用的两台机器的IP分别为192.168.100.140 和192.168.100.150,其中提供NFS服务的是192.168.100.140。在客户端挂载NFS之前,给客户端也安装nfs-utils。
- 先在客户端查看服务端共享了哪些目录:

[root@localhost ~]# showmount -e 192.168.100.140          //使用命令 showmount -e IP 就可以查看NFS的共享情况
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)         //这里有报错,113端口不通,防火墙导致
[root@localhost ~]# systemctl stop firewalld          //这一步在服务端操作,关掉防火墙,最好也关掉SElinux
[root@localhost ~]# showmount -e 192.168.100.140        //这一步在客户端操作,再次查看服务端NFS的共享情况
Export list for 192.168.100.140:
/home/nfstestdir 192.168.100.0/24            //可以看到之前服务器端共享的目录和IP段
  • 然后在客户端上挂在NFS:
[root@localhost ~]# mount -t nfs 192.168.100.140:/home/nfstestdir /mnt/            //-t nfs 指定挂载的类型为nfs
[root@localhost ~]# df -h 
文件系统                          容量  已用  可用 已用% 挂载点
/dev/sda3                          16G  3.4G   13G   22% /
devtmpfs                          1.9G     0  1.9G    0% /dev
tmpfs                             1.9G     0  1.9G    0% /dev/shm
tmpfs                             1.9G  8.7M  1.9G    1% /run
tmpfs                             1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                         197M  113M   85M   58% /boot
tmpfs                             378M     0  378M    0% /run/user/0
192.168.100.140:/home/nfstestdir   16G  3.4G   13G   22% /mnt            //这里可以看到刚挂载的/mnt分区
  • 进入/mnt/目录下,创建测试文件:
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# touch lzx.txt
touch: 无法创建"lzx.txt": 权限不够          //这是因为之前服务端创建的/home/nfstestdir目录权限不合适,挂载后等于被限制uid为1000的用户
[root@localhost ~]# chmod 777 /home/nfstestdir/       //服务端操作,修改目录权限
[root@localhost mnt]# touch lzx.txt           //客户端操作,就可以创建文件了
[root@localhost mnt]# ls -l
总用量 0
-rw-r--r--. 1 mysql mysql 0 712 21:07 lzx.txt          //说明mysql用户的uid和gid都为1000
[root@localhost ~]# tail 10 /etc/passwd          //服务端查看验证
tail: 无法打开"10" 读取数据: 没有那个文件或目录
==> /etc/passwd <==
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/sbin/nologin            //这里mysql用户的uid和gid为1000
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
[root@localhost mnt]# id mysql          //客户端查看
uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)

可以看到,新创建的文件lzx.txt所有者和所属组都为mysql,其uid和gid都为1000


14.3 命令exportfs

exportfs命令常用选项:

-a:表示全部挂载或者卸载
-r:表示重新挂载
-u:表示卸载某一个目录
-v:表示显示共享的目录

当改变/etc/exports配置文件后,使用exportfs命令挂载不需要重启NFS服务。下面做个测试

  • 首先修改服务端配置文件:
[root@localhost ~]# vim /etc/exports        //增加下面一行
/tmp/ 192.168.100.0/24(rw,sync,no_root_squash)
  • 服务端执行exportfs命令:
[root@localhost ~]# exportfs -arv           //服务端直接执行,-arv选项可以直接查看新共享的目录,而不需要重启NFS服务
exporting 192.168.100.0/24:/tmp
exporting 192.168.100.0/24:/home/nfstestdir
[root@localhost mnt]# showmount -e 192.168.100.140       //客户端查看
Export list for 192.168.100.140:
/tmp             192.168.100.0/24
/home/nfstestdir 192.168.100.0/24

上面已经用到了mount命令, -t nfs 指定挂载类型为nfs,另外还有-o nolock(即不加锁)选项
- 在客户端执行命令:

[root@localhost ~]# mkdir /lzx
[root@localhost ~]# mount -t nfs -o nolock 192.168.100.140:/tmp/ /lzx/
  • 可以在客户端上把要挂载的目录写到/etc/fstab文件中,然后挂载时只需要执行mount -a 命令
192.168.100.140:/tmp/ /lzx  nfs defaults,nolock   0 0    //增加该行
  • 之前挂载了NFS,需要先卸载
[root@localhost ~]# umount /lzx 
  • 然后重新挂载
[root@localhost ~]# mount -a

这样操作的好处就是以后开机会自动挂载NFS

还有,服务端之前设置了no_root_squash参数,表示不限制root用户,它在共享目录下创建文件和在本机创建一样

[root@localhost ~]# cd /lzx/
[root@localhost lzx]# ls
mysql.sock  systemd-private-29c690273acd4d8c9e9a6536e096bfae-chronyd.service-ZSMTXM
[root@localhost lzx]# ls -l
总用量 0
srwxrwxrwx. 1 mysql mysql  0 712 20:27 mysql.sock
drwx------. 3 root  root  17 712 17:50 systemd-private-29c690273acd4d8c9e9a6536e096bfae-chronyd.service-ZSMTXM
[root@localhost lzx]# touch 1.txt
[root@localhost lzx]# ll
总用量 0
-rw-r--r--. 1 root  root   0 712 21:43 1.txt           //这里的属主和属组都是root
srwxrwxrwx. 1 mysql mysql  0 712 20:27 mysql.sock
drwx------. 3 root  root  17 712 17:50 systemd-private-29c690273acd4d8c9e9a6536e096bfae-chronyd.service-ZSMTXM

14.4 NFS客户端问题

NFS有2、3、4三个大版本,NFS4可能会存在客户端文件属主属组为nobody的问题。

客户端在挂载共享目录后,不管是root用户还是普通用户,在创建文件时属主、属组为nobody。

两种方法解决该问题:

1.客户端挂载时加上 -o nfsvers=3 ,指定NFS版本为3;

2.客户端和服务端都修改 /etc/idmapd.conf 文件,把#Domain = local.domain.edu改为Domain = xxx.com(这里xxx.com自定义),然后重启rpcidmapd服务(CentOS 7直接重启rpcbind服务即可)。


更多资料参考:
详细讲解NFS配置过程
NFS搭建、配置及故障排除详解

猜你喜欢

转载自blog.csdn.net/miss1181248983/article/details/81045333
今日推荐