Linux中的NFS文件系统

1.安装nfs文件系统的软件
1 客户端和服务端都安装软件然后开启服务
服务端安装服务打开火墙开启服务
[[email protected] ~]# yum install nfs-utils
Linux中的NFS文件系统
Linux中的NFS文件系统
客户端安装服务,开启服务,打开防火墙
[[email protected] ~]# yum install nfs-utils
Linux中的NFS文件系统
[[email protected] ~]# systemctl start nfs
Linux中的NFS文件系统
2 打开两边的防火墙,服务端添加服务
服务端先添加nfs服务
Linux中的NFS文件系统
客户端测试报错
Linux中的NFS文件系统
服务端添加rpc-bind服务

[[email protected] ~]# firewall-cmd --permanent --add-service=rpc-bind 
success
[[email protected] ~]# firewall-cmd --reload
success

Linux中的NFS文件系统
客户端再次查看,出现的报错与上次不同
Linux中的NFS文件系统
服务端添加mountd服务

[[email protected] ~]# firewall-cmd --permanent --add-service=mountd
success
[[email protected] ~]# firewall-cmd --reload
success

Linux中的NFS文件系统
客户端再次查看,查看成功
Linux中的NFS文件系统
服务端列出所有服务

[[email protected] ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources: 
  services: dhcpv6-client http mountd nfs rpc-bind ssh
  ports: 8080/tcp
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 

Linux中的NFS文件系统
3 编辑文件

[[email protected] ~]# vim /etc/exports
/westos  *(ro,sync) (给所有人只读共享)
/westos (要共享的目录)   
刷新nfs服务
[[email protected] ~]# exportfs -rv
exporting *:/westos

Linux中的NFS文件系统
Linux中的NFS文件系统
4 客户端检测

[[email protected] ~]# showmount -e 172.25.254.127
Export list for 172.25.254.127:
/westos *

Linux中的NFS文件系统
2.共享文件在客户端的自动挂载和自动卸载

1)系统默认挂载点,更改自动卸载时间
1 安装软件,开启服务
[[email protected] ~]# yum install autofs.x86_64 -y
Linux中的NFS文件系统
[[email protected] ~]# systemctl start autofs
Linux中的NFS文件系统

[[email protected] ~]# cd /net
[[email protected] net]# cd 172.25.254.127
[[email protected] 172.25.254.127]# ls
westos
[[email protected] 172.25.254.127]# cd westos
[[email protected] westos]# ls
file1  file2  file3

Linux中的NFS文件系统
服务段可以验证,就是共享目录中的内容
Linux中的NFS文件系统

可以看到共享目录已挂载到当前目录下
Linux中的NFS文件系统
[[email protected] westos]# cd
默认的自动卸载是见是300SLinux中的NFS文件系统
Linux中的NFS文件系统
2 编辑配置文件

[[email protected] ~]# vim /etc/autofs
timeout=3
[[email protected] ~]# systemctl restart autofs.service

Linux中的NFS文件系统
Linux中的NFS文件系统
3 再次自动挂载
Linux中的NFS文件系统
2)指定挂载目录

1 编辑主策略文件

[[email protected] ~]# vim /etc/auto.master
/opt                       /etc/auto.westos
最终挂载点的上层目录          子策略文件的名称

Linux中的NFS文件系统
Linux中的NFS文件系统
2 手动建立子策略文件
Linux中的NFS文件系统

westos          172.25.254.127:/westos
最终挂载点        挂载目录(共享文件)

Linux中的NFS文件系统
重启服务
Linux中的NFS文件系统
3 访问目录/opt/westos自动挂载

[[email protected] ~]# cd /opt
[[email protected] opt]# ls
[[email protected] opt]# cd westos
[[email protected] westos]# ls
file1  file2  file3
[[email protected] westos]# df
文件系统                  1K-块    已用    可用 已用% 挂载点
/dev/vda1              10473900 3150140 7323760   31% /
devtmpfs                 927060       0  927060    0% /dev
tmpfs                    942648      80  942568    1% /dev/shm
tmpfs                    942648   17032  925616    2% /run
tmpfs                    942648       0  942648    0% /sys/fs/cgroup
172.25.254.127:/westos 10473984 3639552 6834432   35% /opt/westos

Linux中的NFS文件系统
4 退出目录之后自动卸载
Linux中的NFS文件系统

3.设置挂载的其他参数

1)设置所有用户只读挂载,但某个用户读写挂载

默认所有人的身份都会转换为nfsnobody------>匿名用户

[[email protected] ~]# vim /etc/exports
Linux中的NFS文件系统
Linux中的NFS文件系统
刷新

[[email protected] ~]# exportfs -rv
exporting 172.25.254.227:/westos
exporting *:/westos

Linux中的NFS文件系统

测试:
客户端切换目录自动挂载。建立文件,查看属性,用户身份被转换

Linux中的NFS文件系统
2)不转换身份建立文件

服务端
[[email protected] ~]# vim /etc/exports
/westos *(ro,sync) 172.25.254.227(rw,sync,no_root_squash)
Linux中的NFS文件系统
刷新

[[email protected] ~]# exportfs -rv
exporting 172.25.254.227:/westos
exporting *:/westos

Linux中的NFS文件系统
客户端:
Linux中的NFS文件系统
3)指定加案例文件的用户身份

[[email protected] ~]# vim /etc/exports
/westos *(ro,sync) 172.25.254.227(rw,sync,anonuid=1002)
Linux中的NFS文件系统
刷新

[[email protected] ~]# exportfs -rv
exporting 172.25.254.227:/westos
exporting *:/westos

客户端:
Linux中的NFS文件系统
4)指定新建文件的所有组

服务端;
[[email protected] ~]# vim /etc/exports
/westos *(ro,sync) 172.25.254.227(rw,sync,anonuid=1002,anongid=1000)
Linux中的NFS文件系统
刷新
[[email protected] ~]# exportfs -rv
exporting 172.25.254.227:/westos
exporting *:/westos

客户端测试:
Linux中的NFS文件系统

[[email protected] ~]# id haha
uid=1002(haha) gid=1002(haha) 组=1002(haha)
[[email protected] ~]# id westos
uid=1001(westos) gid=1001(westos) 组=1001(westos)
[[email protected] ~]# id student
uid=1000(student) gid=1000(student) 组=1000(student),10(wheel)

5)指定挂载的版本,默认是ver4

客户端:
[[email protected] ~]# cd /opt/westos
[[email protected] westos]# mount
Linux中的NFS文件系统
Linux中的NFS文件系统
[[email protected] westos]# cd
编辑文件

[[email protected] westos]# vim /etc/auto.westos 
[[email protected] westos]# systemctl restart autofs

Linux中的NFS文件系统

[[email protected] ~]# cd /opt/westos
[[email protected] westos]# mount
Linux中的NFS文件系统
Linux中的NFS文件系统
试验后将设置还原
Linux中的NFS文件系统
6)指定挂载的模式

soft会有响应,返回报错
hard是会无限尝试

服务端:
1 soft
[[email protected] ~]# mount 172.25.254.127:/westos /mnt -o soft
[[email protected] ~]# mount
Linux中的NFS文件系统
Linux中的NFS文件系统
[[email protected] ~]# umount /mnt
2 hard
[[email protected] ~]# mount 172.25.254.127:/westos /mnt
[[email protected] ~]# mount
Linux中的NFS文件系统
Linux中的NFS文件系统