基于linux下的nfs文件系统

NFS概念:

网络文件系统(NFS)是Unix系统和网络附加存储文件管理器常用的网络文件系统,
允许多个客户端通过网络共享文件访问。它可用于提供对共享二进制目录的访问,
也可用于允许用户在同一工作组中从不同客户端访问其文件NFS协议有多个版本
:Linux支持版本4,版本3和版本2,而大多数系统管理员熟悉的是NFSv3。默认
情况下,该协议并不安全,但是更新的版本(如 NFSv4)提供了对更安全的身份验
证 , 甚至可以通过kerberos进行加密。

NFS的常规配置:

服务端:

[root@server ~]# cd /mnt/
[root@server mnt]# ls
file1  file2  file3  file4  file5
[root@server mnt]# yum install nfs-utils -y 安装服务

这里写图片描述

[root@server mnt]# systemctl start nfs  开启服务
[root@server mnt]# systemctl status nfs  查看状态

这里写图片描述

[root@server mnt]# vim /etc/exports  编辑文件

这里写图片描述

[root@server mnt]# cat /etc/exports
/mnt *(sync,ro)  可远程传输只读
[root@server mnt]# exportfs -rv  刷新
exporting *:/mnt

这里写图片描述

客户端:

[root@client ~]# yum install nfs-utils -y   客户端安装服务

这里写图片描述

[root@client ~]# showmount -e 172.25.254.221 显示nfs共享文件
Export list for 172.25.254.221:
/mnt *
[root@client ~]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/vda1             10473900 3182744   7291156  31% /
devtmpfs                469344       0    469344   0% /dev
tmpfs                   484932      80    484852   1% /dev/shm
tmpfs                   484932   12756    472176   3% /run
tmpfs                   484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo      483670    2355    451824   1% /home
//172.25.254.221/DIR  10473900 3157836   7316064  31% /mnt
[root@client ~]# umount /mnt
[root@client ~]# mount 172.25.254.221:/mnt/ /mnt/
[root@client ~]# df
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/vda1            10473900 3182744   7291156  31% /
devtmpfs               469344       0    469344   0% /dev
tmpfs                  484932      80    484852   1% /dev/shm
tmpfs                  484932   12760    472172   3% /run
tmpfs                  484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo     483670    2355    451824   1% /home
172.25.254.221:/mnt  10473984 3157760   7316224  31% /mnt
[root@client ~]# cd /mnt/
[root@client mnt]# rm -fr *  删除的时候为只读
rm: cannot remove ‘file1’: Read-only file system
rm: cannot remove ‘file2’: Read-only file system
rm: cannot remove ‘file3’: Read-only file system
rm: cannot remove ‘file4’: Read-only file system
rm: cannot remove ‘file5’: Read-only file system

这里写图片描述

服务端设置可写:

[root@server mnt]# vim /etc/exports 
/mnt *(sync,rw)  可远程传输可写

这里写图片描述

[root@server mnt]# exportfs -rv 刷新
exporting *:/mnt

这里写图片描述

客户端进行测试:

虽然为可写但是服务端没有赋予权限不可删除,建立文件。

[root@client mnt]# cd
[root@client ~]# umount /mnt/
[root@client ~]# mount 172.25.254.221:/mnt/ /mnt/
[root@client ~]# cd /mnt/
[root@client mnt]# ls
file1  file2  file3  file4  file5
[root@client mnt]# rm -fr *  但是删除操作被拒绝
rm: cannot remove ‘file1’: Permission denied
rm: cannot remove ‘file2’: Permission denied
rm: cannot remove ‘file3’: Permission denied
rm: cannot remove ‘file4’: Permission denied
rm: cannot remove ‘file5’: Permission denied

这里写图片描述

服务端赋予权限:

[root@server mnt]# chmod 777 /mnt/ 添加权限之后就可以建立文件

这里写图片描述

客户端测试建立文件会自动转换用户组:

[root@client mnt]# cd 
[root@client ~]# umount /mnt
[root@client ~]# mount 172.25.254.221:/mnt/ /mnt/
[root@client ~]# cd /mnt/
[root@client mnt]# touch file7  建立文件的用户和组为nfsnobody,会自动转换身份
[root@client mnt]# ll
total 0
-rw-r--r-- 1 root      root      0 Jun  2 02:01 file1
-rw-r--r-- 1 root      root      0 Jun  2 02:01 file2
-rw-r--r-- 1 root      root      0 Jun  2 02:01 file3
-rw-r--r-- 1 root      root      0 Jun  2 02:01 file4
-rw-r--r-- 1 root      root      0 Jun  2 02:01 file5
-rw-r--r-- 1 root      root      0 Jun  2 04:39 file6
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jun  2 04:39 file7 

这里写图片描述

服务端开启不转换用户组:

[root@server mnt]# vim /etc/exports

这里写图片描述

[root@server mnt]# cat /etc/exports
/mnt *(sync,rw,no_root_squash)  不转换用户身份,也即建立文件的用户和组直接为root
[root@server mnt]# exportfs -rv
exporting *:/mnt

这里写图片描述

客户端进行测试:

[root@client mnt]# cd 
[root@client ~]# umount /mnt/
[root@client ~]# mount 172.25.254.221:/mnt/ /mnt/
[root@client ~]# cd /mnt/
[root@client mnt]# touch file8
[root@client mnt]# ll
total 0
-rw-r--r-- 1 root      root      0 Jun  2 02:01 file1
-rw-r--r-- 1 root      root      0 Jun  2 02:01 file2
-rw-r--r-- 1 root      root      0 Jun  2 02:01 file3
-rw-r--r-- 1 root      root      0 Jun  2 02:01 file4
-rw-r--r-- 1 root      root      0 Jun  2 02:01 file5
-rw-r--r-- 1 root      root      0 Jun  2 04:39 file6
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jun  2 04:39 file7
-rw-r--r-- 1 root      root      0 Jun  2 04:41 file8

这里写图片描述

服务端默认用户组:

[root@server mnt]# vim /etc/exports  共享文件用户组为student
/mnt *(sync,rw,anonuid=1000,anongid=1000)

这里写图片描述

[root@server mnt]# exportfs -rv
exporting *:/mnt

这里写图片描述

客户端进行测试:

[root@client mnt]# cd
[root@client ~]# umount /mnt/
[root@client ~]# mount 172.25.254.221:/mnt/ /mnt/
[root@client ~]# cd /mnt/
[root@client mnt]# rm -fr *
[root@client mnt]# touch file1
[root@client mnt]# ll   建立文件的用户组为student
total 0
-rw-r--r-- 1 student student 0 Jun  2 04:42 file1

这里写图片描述

服务端共享网段:

[root@server mnt]# vim /etc/exports  共享网段
[root@server mnt]# cat /etc/exports
/mnt *(sync,rw,anonuid=1000,anongid=1000)
/westos 172.25.254.0/24(sync) 172.25.254.21(sync,rw)

这里写图片描述

[root@server mnt]# exportfs -rv
exporting 172.25.254.121:/westos
exporting 172.25.254.0/24:/westos
exporting *:/mnt
[root@server mnt]# chmod 777 /westos/  赋予满权限

这里写图片描述

客户端(此处为真机测试,写入的是真机IP)

[root@foundation84 ~]# mount 172.25.254.221:/westos /mnt/  
[root@foundation84 ~]# cd /mnt
[root@foundation84 mnt]# ls
aa  fil  file  file2  xfl
[root@foundation84 mnt]# touch xyy
touch: cannot touch ‘xyy’: Permission denied
[root@foundation84 mnt]# touch xyy   可以建立文件是由于服务端开启了权限
[root@foundation84 mnt]# ll
total 0
-rw-r--r--. 1 kiosk     kiosk     0 Jun  4 12:37 aa
-rw-r--r--. 1      1001      1001 0 Jun  4 12:43 fil
-rw-r--r--. 1      1001      1001 0 Jun  4 12:27 file
-rw-r--r--. 1 root           1001 0 Jun  4 13:14 file2
-rw-r--r--. 1 root           1001 0 Jun  4 12:51 xfl
-rw-r--r--. 1 nfsnobody nfsnobody 0 Jun  4 19:01 xyy

这里写图片描述

autofs自动挂载服务:

autofs可自动挂载卸载文件系统,不会一直闲置占用后台:

在服务端:

[root@server ~]# systemctl start nfs  开启nfs服务,服务端开启即可
[root@server ~]# systemctl stop firewalld  关闭防火墙

在客户端:

[root@client ~]# umount /mnt/  每次都在不用的时候卸载特别麻烦,浪费资源
[root@client ~]# df  查看挂载已经卸载
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3181552   7292348  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932      80    484852   1% /dev/shm
tmpfs                 484932   12756    472176   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2355    451824   1% /home
[root@client ~]# yum install autofs -y  安装autofs自动挂载卸载服务

这里写图片描述

[root@client ~]# cd /net
-bash: cd: /net: No such file or directory
[root@client ~]# systemctl start autofs  服务开启之后/net/目录自动生成
[root@client ~]# cd /net/
[root@client net]# ls
[root@client net]# cd 172.25.254.221
[root@client 172.25.254.221]# pwd
/net/172.25.254.221
[root@client 172.25.254.221]# ls  进入路径之后就可以看到westos/里面的文件
mnt  westos
[root@client 172.25.254.221]# cd westos/
[root@client westos]# ls
aa  bb  fil  file  file2  file3  xfl

这里写图片描述

[root@client westos]# df  可以看到用的时候会直接挂载
Filesystem             1K-blocks    Used Available Use% Mounted on
/dev/vda1               10473900 3187188   7286712  31% /
devtmpfs                  469344       0    469344   0% /dev
tmpfs                     484932      80    484852   1% /dev/shm
tmpfs                     484932   12764    472168   3% /run
tmpfs                     484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo        483670    2355    451824   1% /home
172.25.254.221:/westos  10473984 3156480   7317504  31% /net/172.25.254.221/westos
[root@client westos]# cd 
[root@client ~]# pwd 因为默认时间延时为300秒,意味着六分钟之后自动卸载
/root

这里写图片描述

默认时间延迟:

这里写图片描述

[root@client ~]# vim /etc/sysconfig/autofs 编辑文件调整默认时长,意味着默认时间过去会自动解除挂载
TIMEOUT=5改为5秒,时间到了会自动卸载

这里写图片描述

[root@client ~]# systemctl  restart autofs.service  重启服务
[root@client ~]# df挂载自动卸载
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3187208   7286692  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932      80    484852   1% /dev/shm
tmpfs                 484932   12764    472168   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2355    451824   1% /home

这里写图片描述

设定nfs挂载目录:

[root@client ~]# df  查看挂载
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3187192   7286708  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932      80    484852   1% /dev/shm
tmpfs                 484932   12768    472164   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2355    451824   1% /home
[root@client ~]# cd /net/172.25.254.221进入共享IP
[root@client 172.25.254.221]# ls
mnt  westos
[root@client 172.25.254.221]# cd westos/ 进入共享目录
[root@client westos]# df 已经挂载成功,但是挂载点为默认
Filesystem             1K-blocks    Used Available Use% Mounted on
/dev/vda1               10473900 3187192   7286708  31% /
devtmpfs                  469344       0    469344   0% /dev
tmpfs                     484932      80    484852   1% /dev/shm
tmpfs                     484932   12768    472164   3% /run
tmpfs                     484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo        483670    2355    451824   1% /home
172.25.254.221:/westos  10473984 3156480   7317504  31% /net/172.25.254.221/westos
[root@client westos]# cd 自动挂载卸载必须将路径切换出来
[root@client ~]# df  查看挂载消失
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3187192   7286708  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932      80    484852   1% /dev/shm
tmpfs                 484932   12768    472164   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2355    451824   1% /home

这里写图片描述

[root@client ~]# vim /etc/auto.master 编辑主文件
主文件写入最终挂载点的上层目录:14 /nfs     /etc/auto.westos

这里写图片描述

[root@client ~]# vim /etc/auto.westos  编辑子策略文件
字策略文件内容:写入最终挂载点,挂载参数,真实的网络文件系统
[root@client ~]# cat /etc/auto.westos
westos -ro 172.25.254.221:/westos

这里写图片描述

[root@client ~]# systemctl restart autofs.service 重启服务
[root@client ~]# cd /nfs
[root@client nfs]# ls
[root@client nfs]# cd westos 不显示但是可以进入westos目录中
[root@client westos]# df  查看挂载,已经为自己设定的挂载点
Filesystem             1K-blocks    Used Available Use% Mounted on
/dev/vda1               10473900 3187212   7286688  31% /
devtmpfs                  469344       0    469344   0% /dev
tmpfs                     484932      80    484852   1% /dev/shm
tmpfs                     484932   12768    472164   3% /run
tmpfs                     484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo        483670    2355    451824   1% /home
172.25.254.221:/westos  10473984 3156480   7317504  31% /nfs/westos

这里写图片描述

[root@client westos]# mount  查看挂载参数,与子策略文件内容相符合 (ro,relatime,vers=4.0,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.25.254.121,local_lock=none,addr=172.25.254.221)

这里写图片描述

[root@client westos]# vim /etc/auto.westos  修改子策略文件中的挂载参数
[root@client westos]# cat /etc/auto.westos
westos -rw,vers=3 172.25.254.221:/westos

这里写图片描述

[root@client westos]# cd  重启服务时必须将路径切换出来
[root@client ~]# systemctl restart autofs.service  重启服务
[root@client ~]# cd /nfs
[root@client nfs]# cd westos
[root@client westos]# df  挂载点已经设定
Filesystem             1K-blocks    Used Available Use% Mounted on
/dev/vda1               10473900 3187192   7286708  31% /
devtmpfs                  469344       0    469344   0% /dev
tmpfs                     484932      80    484852   1% /dev/shm
tmpfs                     484932   12768    472164   3% /run
tmpfs                     484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo        483670    2355    451824   1% /home
172.25.254.221:/westos  10473984 3156608   7317376  31% /nfs/westos

这里写图片描述

[root@client westos]# mount 查看挂载参数,与子策略文件内容相符合 (rw,relatime,vers=3,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=172.25.254.221,mountvers=3,mountport=20048,mountproto=udp,local_lock=none,addr=172.25.254.221)
[root@client westos]# cd

这里写图片描述

猜你喜欢

转载自blog.csdn.net/aaaaaab_/article/details/80566093