Linux中的NFS文件系统
1.安装nfs文件系统的软件
1 客户端和服务端都安装软件然后开启服务
服务端安装服务打开火墙开启服务
[[email protected] ~]# yum install nfs-utils
客户端安装服务,开启服务,打开防火墙
[[email protected] ~]# yum install nfs-utils
[[email protected] ~]# systemctl start nfs
2 打开两边的防火墙,服务端添加服务
服务端先添加nfs服务
客户端测试报错
服务端添加rpc-bind服务
[[email protected] ~]# firewall-cmd --permanent --add-service=rpc-bind
success
[[email protected] ~]# firewall-cmd --reload
success
客户端再次查看,出现的报错与上次不同
服务端添加mountd服务
[[email protected] ~]# firewall-cmd --permanent --add-service=mountd
success
[[email protected] ~]# firewall-cmd --reload
success
客户端再次查看,查看成功
服务端列出所有服务
[[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:
3 编辑文件
[[email protected] ~]# vim /etc/exports
/westos *(ro,sync) (给所有人只读共享)
/westos (要共享的目录)
刷新nfs服务
[[email protected] ~]# exportfs -rv
exporting *:/westos
4 客户端检测
[[email protected] ~]# showmount -e 172.25.254.127
Export list for 172.25.254.127:
/westos *
2.共享文件在客户端的自动挂载和自动卸载
1)系统默认挂载点,更改自动卸载时间
1 安装软件,开启服务
[[email protected] ~]# yum install autofs.x86_64 -y
[[email protected] ~]# systemctl start autofs
[[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
服务段可以验证,就是共享目录中的内容
可以看到共享目录已挂载到当前目录下
[[email protected] westos]# cd
默认的自动卸载是见是300S
2 编辑配置文件
[[email protected] ~]# vim /etc/autofs
timeout=3
[[email protected] ~]# systemctl restart autofs.service
3 再次自动挂载
2)指定挂载目录
1 编辑主策略文件
[[email protected] ~]# vim /etc/auto.master
/opt /etc/auto.westos
最终挂载点的上层目录 子策略文件的名称
2 手动建立子策略文件
westos 172.25.254.127:/westos
最终挂载点 挂载目录(共享文件)
重启服务
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
4 退出目录之后自动卸载
3.设置挂载的其他参数
1)设置所有用户只读挂载,但某个用户读写挂载
默认所有人的身份都会转换为nfsnobody------>匿名用户
[[email protected] ~]# vim /etc/exports
刷新
[[email protected] ~]# exportfs -rv
exporting 172.25.254.227:/westos
exporting *:/westos
测试:
客户端切换目录自动挂载。建立文件,查看属性,用户身份被转换
2)不转换身份建立文件
服务端
[[email protected] ~]# vim /etc/exports
/westos *(ro,sync) 172.25.254.227(rw,sync,no_root_squash)
刷新
[[email protected] ~]# exportfs -rv
exporting 172.25.254.227:/westos
exporting *:/westos
客户端:
3)指定加案例文件的用户身份
[[email protected] ~]# vim /etc/exports
/westos *(ro,sync) 172.25.254.227(rw,sync,anonuid=1002)
刷新
[[email protected] ~]# exportfs -rv
exporting 172.25.254.227:/westos
exporting *:/westos
客户端:
4)指定新建文件的所有组
服务端;
[[email protected] ~]# vim /etc/exports
/westos *(ro,sync) 172.25.254.227(rw,sync,anonuid=1002,anongid=1000)
刷新
[[email protected] ~]# exportfs -rv
exporting 172.25.254.227:/westos
exporting *:/westos
客户端测试:
[[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
[[email protected] westos]# cd
编辑文件
[[email protected] westos]# vim /etc/auto.westos
[[email protected] westos]# systemctl restart autofs
[[email protected] ~]# cd /opt/westos
[[email protected] westos]# mount
试验后将设置还原
6)指定挂载的模式
soft会有响应,返回报错
hard是会无限尝试
服务端:
1 soft
[[email protected] ~]# mount 172.25.254.127:/westos /mnt -o soft
[[email protected] ~]# mount
[[email protected] ~]# umount /mnt
2 hard
[[email protected] ~]# mount 172.25.254.127:/westos /mnt
[[email protected] ~]# mount