新秀篇 ##Linux下NFS文件系统以及配置##

NFS概念:

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

一.NFS的常规配置:

【服务端server】:

[root@server ~]# cd /mnt/                   ##移动路径
[root@server mnt]# ls                     ##查看
file1  file2  file3  file4  file5
[root@server mnt]# yum install nfs-utils -y       ##安装nfs服务
[root@server mnt]# systemctl start nfs               ##开启服务
[root@server mnt]# systemctl status nfs              ##查看状态
/mnt *(sync,ro)  只读
[root@server mnt]# vim /etc/exports              ##编辑文件
[root@server mnt]#           ##cat /etc/exports

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

【客户端desktop】:

[root@client ~]# yum install nfs-utils -y      ##客户端安装服务
[root@client ~]# showmount -e 172.25.254.220    
Export list for 172.25.254.220:
/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.220/DIR  10473900 3157836   7316064  31% /mnt
[root@client ~]# umount /mnt                ##卸载
[root@client ~]# mount 172.25.254.220:/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.220:/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.220:/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.220:/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.220:/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
/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.220:/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.20(sync,rw)
[root@server mnt]# exportfs -rv
exporting 172.25.254.120:/westos
exporting 172.25.254.0/24:/westos
exporting *:/mnt
在虚拟机挂载/westos/进行测试,依次添加权限实验。

二.AUTOFS自动挂载:

【在服务端server】:

[root@server ~]# systemctl start nfs             ##开启服务
[root@server ~]# mkdir /westos                 ##建立要共享的目录
[root@server ~]# chmod 777 /westos/                 ##给最大权限
[root@server ~]# vim /etc/exports                ##编辑文件
/westos   172.25.254.120/24(sync,rw,no_root_squash)       ##root用户具有根目录的完全管理访问权限
[root@server ~]# exportfs -rv                ##刷新服务
exporting 172.25.254.120/24:/westos

【在客户端desktop】:

[root@client ~]# showmount -e 172.25.254.220             ##查看nfs共享状态
Export list for 172.25.254.220:
/westos 172.25.254.120/24
[root@client ~]# yum install autofs -y                        ##安装autofs服务
Loaded plugins: langpacks
rhel_dvd                                                 | 4.1 kB     00:00     
Resolving Dependencies
--> Running transaction check
[root@client ~]# cd /net                             ##没重启的时候,没有自动建立/net
-bash: cd: /net: No such file or directory
[root@client ~]# systemctl start autofs              ##重启服务
[root@client ~]# cd /net/                   ##服务自动建立/net
[root@client net]# ls
[root@client net]# cd 172.25.254.220              
[root@client 172.25.254.220]# pwd
/net/172.25.254.220
[root@client 172.25.254.220]# ls              ##查看有westos共享目录
westos
[root@client 172.25.254.220]# cd westos
[root@client westos]# ls
file1  file2  file4
[root@client westos]# df               ##查看挂载
Filesystem             1K-blocks    Used Available Use% Mounted on
/dev/vda1               10473900 3187308   7286592  31% /
devtmpfs                  469344       0    469344   0% /dev
tmpfs                     484932      84    484848   1% /dev/shm
tmpfs                     484932   12788    472144   3% /run
tmpfs                     484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo        483670    2356    451823   1% /home
172.25.254.220:/westos  10473984 3158528   7315456  31% /net/172.25.254.220/westos                   ##自动进行挂载
[root@client westos]# vim /etc/sysconfig/autofs               ##编辑文件
TIMEOUT=5         ##自动挂载5s不使用自动卸载(默认的为300s)
[root@client westos]# systemctl restart autofs.service                ##重启服务
[root@client westos]# cd                  ##先移动出来
[root@client ~]# df                    ##查看,已经自动卸载
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3187212   7286688  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932      84    484848   1% /dev/shm
tmpfs                 484932   12788    472144   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2356    451823   1% /home

三.配置autofs的指定挂载点:

【在客户端desktop】:

[root@client ~]# vim /etc/auto.master               ##编辑最终挂载点的上层目录
/nfs    /etc/auto.westos
[root@client ~]# vim /etc/auto.westos                 ##最终挂载点的名称
westos   -ro   172.25.254.220:/westos
[root@client ~]# systemctl restart autofs.service            ##重启服务
[root@client ~]# cd /nfs
[root@client nfs]# ls
[root@client nfs]# cd westos
[root@client westos]# df                      ##查看挂载
Filesystem             1K-blocks    Used Available Use% Mounted on
/dev/vda1               10473900 3187228   7286672  31% /
devtmpfs                  469344       0    469344   0% /dev
tmpfs                     484932      84    484848   1% /dev/shm
tmpfs                     484932   12788    472144   3% /run
tmpfs                     484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo        483670    2356    451823   1% /home
172.25.254.220:/westos  10473984 3158656   7315328  31% /nfs/westos            ##挂载点已经更改
[root@client westos]# mount                  ##查看挂载信息是否和配置的相同
172.25.254.220:/westos on /nfs/westos type nfs4 (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.120,local_lock=none,addr=172.25.254.220)

猜你喜欢

转载自blog.csdn.net/china_zgd/article/details/80616179