一、NFS是Network File System的缩写
NFS最早由Sun公司开发,分2,3,4三个版本,2和3由Sun起草开发,4.0开始Netapp公司参与并主导开发,最新为4.1版本
NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写。
NFS应用场景是:A,B,C三台机器上需要保证被访问到的文件是一样的,A共享数据出来,B和C分别去挂载A共享的数据目录,从而B和C访问到的数据和A上的一致
二、NFS服务端安装配置
yum install -y nfs-utils rpcbind
vim /etc/exports //加入如下内容
/home/nfstestdir 192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
保存配置文件后,执行如下准备操作
mkdir /home/nfstestdir
chmod 777 /home/nfstestdir
[root@greg02 ~]#netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
systemctl start rpcbind
systemctl start nfs
[root@greg02 ~]#ps aux |grep nfs
root 2999 0.0 0.0 0 0 ? S< 04:59 0:00 [nfsd4_callbacks]
root 3005 0.0 0.0 0 0 ? S 04:59 0:00 [nfsd]
root 3006 0.0 0.0 0 0 ? S 04:59 0:00 [nfsd]
root 3007 0.0 0.0 0 0 ? S 04:59 0:00 [nfsd]
root 3008 0.0 0.0 0 0 ? S 04:59 0:00 [nfsd]
root 3009 0.0 0.0 0 0 ? S 04:59 0:00 [nfsd]
root 3010 0.0 0.0 0 0 ? S 04:59 0:00 [nfsd]
root 3011 0.0 0.0 0 0 ? S 04:59 0:00 [nfsd]
root 3012 0.0 0.0 0 0 ? S 04:59 0:00 [nfsd]
root 3017 0.0 0.0 112664 964 pts/0 S+ 04:59 0:00 grep --color=auto nfs
systemctl enable rpcbind
systemctl enable nfs
[root@greg02 ~]#systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
三、NFS配置选项
rw 读写
ro 只读
sync 同步模式,内存数据实时写入磁盘
async 非同步模式
no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大
root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid
客户端挂载
yum install -y nfs-utils
showmount -e 192.168.133.130 //该ip为NFS服务端ip
[root@greg02 ~]#showmount -e 192.168.179.130
Export list for 192.168.179.130:
/home/nfstestdir 192.168.133.0/24
服务端和客户端都要关闭selinux和防火墙service iptables stop
mount -t nfs 192.168.179.130:/home/nfstestdir /mnt
df -h
touch /mnt/aminglinux.txt
ls -l /mnt/aminglinux.txt //可以看到文件的属主和属组都为1000
四、exportfs命令
以下操作在客户端
[root@greg02 ~]#exportfs -arv
exporting 192.168.179.131:/tmp
exporting 192.168.179.0/24:/home/nfstestdir
mount -t nfs -onolock 192.168.133.130:/tmp /mnt
[root@greg02 ~]# mount -t nfs 192.168.179.130:/tmp/ /mnt/
[root@greg02 ~]#
[root@greg02 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/cl-root 27G 2.2G 25G 8% /
devtmpfs 901M 0 901M 0% /dev
tmpfs 912M 0 912M 0% /dev/shm
tmpfs 912M 8.7M 904M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 1014M 139M 876M 14% /boot
tmpfs 183M 0 183M 0% /run/user/0
192.168.179.130:/tmp 27G 5.2G 22G 20% /mnt
touch /mnt/test.txt
ls -l !$
-oremount,nfsvers=3
五、客户端文件属主属组nobody
NFS 4版本会有该问题
客户端挂载共享目录后,不管是root用户还是普通用户,创建新文件时属主、属组为nobody
客户端挂载时加上 -o nfsvers=3
[root@greg02 ~]# mount -t nfs -o nfsvers=3 192.168.179.130:/tmp/ /mnt/
[root@greg02 ~]# mount -t nfs -oremount,nfsvers=3 192.168.179.130:/tmp/ /mnt/
客户端和服务端都需要
vim /etc/idmapd.conf //
把“#Domain = local.domain.edu” 改为 “Domain = xxx.com” (这里的xxx.com,随意定义吧),然后再重启rpcidmapd服务