NFS,网络文件系统。一个系统在网络上与他人共享目录和文件。通过使用NFS,用户和程序可以像访问本地

文件一样访问系统上的文件。
用途:共享文件
优点:节省磁盘空间
组成:一台服务器和一台或多台客户机


linux-NFS文件系统_NFS文件系统


(服务端)

[root@desktop ~]# yum install nfs-untils -y 安装服务
[root@desktop ~]# systemctl start nfs(共享) (不要重启服务,否则客户端会卡)

#注:端口111  
ss -antlupe | grep 111 查看111端口是否开放



加入以下三个服务

[root@desktop ~]# firewall-cmd --permanent --add-service=nfs

[root@desktop ~]# firewall-cmd --permanent --add-service=rpc-bind

[root@desktop ~]# firewall-cmd --permanent --add-service=mountd

记得:操作完,重新加载

[root@desktop ~]# firewall-cmd --reload
success


linux-NFS文件系统_NFS文件系统_03


以上都准备完成后,在服务端建立共享目录

[root@desktop ~]# mkdir /westos/nfs -p  建立(共享)目录
[root@desktop ~]# vim /etc/exports  编辑文件
[root@desktop ~]# cat /etc/exports
/westos/nfs  *(sync)  <----同步到客户端
[root@desktop ~]#
[root@desktop ~]# exportfs -rv 刷新
exporting *:/westos/nfs


linux-NFS文件系统_NFS文件系统_04


(客户端)

如下操作去发现服务器上的设备,及进行挂载

[root@client ~]# showmount -e 172.25.12.10 发现(服务器端)设备

Export list for 172.25.12.10:   看到10主机的设备
/westos/nfs *


linux-NFS文件系统_NFS文件系统_05


*)在客户端如下实现自动挂载、卸载

[root@client ~]# yum install autofs.x86_64 -y 安装服务 (自动挂在,卸载)

[root@client ~]# systemctl start autofs 开启服务(会自动生成/net目录

[root@client ~]# ls -ld /net  查看权限
drwxr-xr-x 2 root root 0 Dec  9 20:01 /net


[root@foundation19 ~]# rpm -qc autofs 可通这个命令查看配置文件


linux-NFS文件系统_NFS文件系统_06

[root@client nfs]# vim /etc/sysconfig/autofs  编辑配置文件 (7.0)

注:vim /etc/autofs.conf   7.2 版本

linux-NFS文件系统_NFS文件系统_07

如上可更改挂载时间

[root@client nfs]# systemctl restart autofs.service  重启服务

linux-NFS文件系统_NFS文件系统_08

如上图,若想改变挂载点位置,可如下操作。


*)cd /westos/(linux)/nfs 实现挂载在此目录下


(服务端)

[root@desktop ~]# vim /etc/exports  编辑此文件
[root@desktop ~]# exportfs -rv
exporting *:/westos/linux/nfs
exportfs: Failed to stat /westos/linux/nfs: No such file or directory
[root@desktop ~]# mkdir -p /westos/linux/nfs 建立现在需要挂载的目录
[root@desktop ~]# exportfs -rv 刷新
exporting *:/westos/linux/nfs
[root@desktop ~]# rm -fr /westos/nfs 删除之前的


(客户端)

[root@client nfs]# vim /etc/auto.master  编辑配置文件(最终挂在点的上层目录

linux-NFS文件系统_NFS文件系统_09



[root@client nfs]# vim /etc/auto.nfs 在指定的文件中写入挂载信息


linux-NFS文件系统_NFS文件系统_10


[root@client nfs]# systemctl restart autofs.service 重启服务


测试:

linux-NFS文件系统_NFS文件系统_11

[root@client nfs]# mount  查看挂载信息


linux-NFS文件系统_NFS文件系统_12


*)使客户主机可写


(服务端)

[root@desktop ~]# vim /etc/exports  编辑文件
[root@desktop ~]# cat /etc/exports (2')
/westos/nfs  *(sync,rw)  加入权限rw
[root@desktop ~]# exportfs -rv  刷新
exporting *:/westos/nfs

[root@desktop ~]# cd /westos/nfs/ 切到此目录
[root@desktop nfs]# ls -ld /westos/nfs/
drwxr-xr-x 2 root root 6 Dec  9 07:45 /westos/nfs/ 其他人没有写的权限
[root@desktop nfs]# chmod 777 /westos/nfs/  给权限
[root@desktop nfs]# ls -ld /westos/nfs/
drwxrwxrwx 2 root root 6 Dec  9 07:45 /westos/nfs/ 其他人有写的权限


linux-NFS文件系统_NFS文件系统_13


linux-NFS文件系统_NFS文件系统_14


*)指定身份创建文件

[root@desktop nfs]# vim /etc/exports

[root@desktop nfs]# exportfs -rv
exporting *:/westos/nfs
[root@desktop nfs]# cat /etc/exports
/westos/nfs  *(sync,rw,anonuid=1001,anongid=1001) 指定身份创建文件
表示客户端新建文件属于指定组和用户

linux-NFS文件系统_NFS文件系统_16


若上面指定的id与用户一直,则所建立文件属于该用户。

linux-NFS文件系统_NFS文件系统_17

*)超户身份写入

[root@desktop nfs]# vim /etc/exports


linux-NFS文件系统_NFS文件系统_18[root@desktop nfs]# exportfs -rv
exporting *:/westos/nfs
[root@desktop nfs]# cat /etc/exports
/westos/nfs  *(sync,rw,anonuid=1001,anongid=1001,no_root_squash)  超户身份(不做转换)表示客户端以ROOT用户身份使用设备新建文件,文件属于root

linux-NFS文件系统_NFS文件系统_19