linux中NFS服务端的配置和客户端的访问

nfs

1.什么是NFS,它有什么作用
参见百度百科:NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。
作用:在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。


1.NFS服务端的配置和客户端的检测

服务端:
[root@sever ~]# yum install nfs-utils   #下载nfs服务软件
Loaded plugins: langpacks
Package 1:nfs-utils-1.3.0-0.el7.x86_64 already installed and latest version
Nothing to do
[root@sever ~]# systemctl start nfs   #打开服务
[root@sever ~]# systemctl stop firewalld  #关闭火墙
客户端:
[root@client ~]# yum install nfs-utils -y  #下载nfs服务软件
Loaded plugins: langpacks
source7.0                                                | 4.1 kB     00:00     
Package 1:nfs-utils-1.3.0-0.el7.x86_64 already installed and latest version
Nothing to do
[root@client ~]# showmount -e 172.25.254.126   #连接成功
Export list for 172.25.254.126:

这里写图片描述
这里写图片描述

2、服务端共享目录相关系数的设定与客户端挂载

格式:
/共享目录 用户(共享参数,共享参数·····)
eg:/westos *(sync,rw) ——– #对所有用户读写共享/westos目录。
相关参数:
sync ——–> 同步目录
rw ———>读写
ro ———>只读
no_root_squash ——-> 将用户转换为root用户
anonuid=1000,anongid=1000 ——->将用户转换为特定组用户

(1) ro ———>只读
服务端:
[root@sever ~]# vim /etc/exports
[root@sever ~]# cat /etc/exports
/mnt  *(sync,ro)  #只读分享/mnt目录,sync表示同步目录内容,*表示所有用户。
[root@sever ~]# exportfs -rv  #刷新
exporting *:/mnt
客户端:
[root@client ~]# showmount -e 172.25.254.126
Export list for 172.25.254.126:
/mnt *     #此时同步/mnt目录 
[root@client ~]# mount 172.25.254.126:/mnt /mnt   #挂载目录
[root@client ~]# df
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/vda1            10473900 3407468   7066432  33% /
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    2346    451833   1% /home
172.25.254.126:/mnt  10473984 3603072   6870912  35% /mnt  #挂载成功
[root@client ~]# cd /mnt/
[root@client mnt]# ls     
boot  etc  kernel-3.10.0-123.el7.x86_64.rpm  lib
[root@client mnt]# rm -fr kernel-3.10.0-123.el7.x86_64.rpm 
rm: cannot remove ‘kernel-3.10.0-123.el7.x86_64.rpm’: Read-only file system #显示只读

这里写图片描述
这里写图片描述

(2) rw ———>读写
服务端: 
[[root@sever ~]# vim /etc/exports
[root@sever ~]# cat /etc/exports
/mnt  *(sync,rw)   #允许读写
[root@sever ~]# systemctl restart nfs
客户端:
[root@client mnt]# rm -fr kernel-3.10.0-123.el7.x86_64.rpm   
rm: cannot remove ‘kernel-3.10.0-123.el7.x86_64.rpm’: Permission denied
#此时删除被阻止,是因为/mnt目录本身权限
服务端:
[root@sever ~]# ls -ld /mnt/
drwxr-xr-x. 5 root root 76 May 17 08:40 /mnt/
[root@sever ~]# chmod 777 /mnt/

客户端:
[root@client mnt]# rm -fr kernel-3.10.0-123.el7.x86_64.rpm 
[root@client mnt]# rm -fr kernel-3.10.0-123.el7.x86_64.rpm 
[root@client mnt]# touch file  #此时可读写。

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

(3) no_root_squash ——-> 将用户转换为root用户
[root@sever mnt]# ll
total 4
drwxr-xr-x. 2 root      root      4096 May 17 08:40 boot
drwxr-xr-x. 3 root      root        25 May 17 08:40 etc
-rw-r--r--. 1 nfsnobody nfsnobody    0 Jun  7 11:21 file  #此时显示的是匿名用户建立
drwxr-xr-x. 3 root      root        20 May 17 08:40 lib

服务端:

[root@sever mnt]# vim /etc/exports
[root@sever mnt]# cat /etc/exports
/mnt  *(sync,rw,no_root_squash)
[root@sever mnt]# systemctl restart nfs

客户端:
[root@client mnt]# touch file1
[root@client mnt]# ll
total 4
drwxr-xr-x 2 root      root      4096 May 17 08:40 boot
drwxr-xr-x 3 root      root        25 May 17 08:40 etc
-rw-r--r-- 1 nfsnobody nfsnobody    0 Jun  7 11:21 file
-rw-r--r-- 1 root      root         0 Jun  7 11:51 file1   #转换为root建立。
drwxr-xr-x 3 root      root        20 May 17 08:40 lib

这里写图片描述
这里写图片描述

(4) anonuid=1000,anongid=1000 ——->将用户转换为特定组用户
服务端:
[root@sever mnt]# id student
uid=1000(student) gid=1000(student) groups=1000(student),1001(westos)
[root@sever mnt]# vim /etc/exports
[root@sever mnt]# cat /etc/exports
/mnt  *(sync,rw,anonuid=1000,anongid=1000)
[root@sever mnt]# systemctl restart nfs



客户端:
root@client mnt]# cd
[root@client ~]# umount /mnt/
[root@client ~]# mount 172.25.254.126:/mnt /mnt
[root@client ~]# cd /mnt/
[root@client mnt]# touch file4
[root@client mnt]# ll
total 4
drwxr-xr-x 2 root      root      4096 May 17 08:40 boot
drwxr-xr-x 3 root      root        25 May 17 08:40 etc
-rw-r--r-- 1 nfsnobody nfsnobody    0 Jun  7 11:21 file
-rw-r--r-- 1 root      root         0 Jun  7 11:51 file1
-rw-r--r-- 1 root      root         0 Jun  7 11:54 file3
-rw-r--r-- 1 student   student     0 Jun  7 11:56 file4  #此时显示为student用户建立 

drwxr-xr-x 3 root      root        20 May 17 08:40 lib

这里写图片描述
这里写图片描述

(5) 指定特定用户访问
服务端:

[root@sever mnt]# vim /etc/exports
[root@sever mnt]# cat /etc/exports
/mnt  *(sync,rw,anonuid=1000,anonuid=1000)
/westos 172.25.254.0/24(sync) 172.25.254.226(sync,rw)  
#允许所有172.25.254网段的用户访问目录,允许172.25.254.226主机进行读写操作。
[root@sever mnt]# chmod 777 /westos
[root@sever mnt]# systemctl restart nfs

Ip为172.25.254.71的用户连接:
[root@foundation71 ~]# mount 172.25.254.126:/westos /mnt
[root@foundation71 ~]# cd /mnt/
[root@foundation71 mnt]# touch file
touch: cannot touch ‘file’: Read-only file system   #命令被阻止。
Ip为172.25.254.226的用户连接:
[root@client ~]# umount /mnt/
[root@client ~]# mount 172.25.254.126:/westos /mnt
[root@client ~]# cd  /mnt/
[root@client mnt]# touch file3
[root@client mnt]# ls
123  256  456  file  file1  file3  hello  word   #可以读写

这里写图片描述
这里写图片描述
这里写图片描述

3、利用 autofs管理nfs服务

客户端远程访问nfs服务器端的文件,需要挂载使用,但是当我们在不需要使用的时候,依然挂载的文件系统就会造成资源的浪费,或者在不用的时候卸载,在用的时候挂载,这样也是非常麻烦的,为了解决上述问题,我们可以在客户端下载配置autofs工具

(1)自动挂载、卸载

服务端:
[root@server /]# vim /etc/exports
[root@server /]# cat /etc/exports
/westos  172.25.254.0/24(sync,rw,no_root_squash)
[root@server westos]# exportfs -rv
[root@sever westos]# ll -ld /westos/
drwxrwxrwx. 2 root root 94 Jun  7 13:48 /westos/


客户端:
[root@client ~]# showmount -e 172.25.254.126
Export list for 172.25.254.126:
/westos 172.25.254.0/24
[root@client ~]# mount 172.25.254.126:/westos/ /mnt/
[root@client ~]# df
Filesystem             1K-blocks    Used Available Use% Mounted on
/dev/vda1               10473900 3182340   7291560  31% /
devtmpfs                  469332       0    469332   0% /dev
tmpfs                     484920      80    484840   1% /dev/shm
tmpfs                     484920   12760    472160   3% /run
tmpfs                     484920       0    484920   0% /sys/fs/cgroup
/dev/mapper/vg0-vo        483670    2366    451813   1% /home
172.25.254.126:/westos  10473984 3158656   7315328  31% /mnt
[root@client ~]# cd /mnt/
[root@client mnt]# ls
file
[root@client mnt]# touch file1
[root@client mnt]# cd
[root@client ~]# umount /mnt/
[root@client ~]# df
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3182344   7291556  31% /
devtmpfs              469332       0    469332   0% /dev
tmpfs                 484920      80    484840   1% /dev/shm
tmpfs                 484920   12756    472164   3% /run
tmpfs                 484920       0    484920   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2366    451813   1% /home
[root@client ~]# yum install autofs.x86_64 
Loaded plugins: langpacks
rhel_dvd                                                 | 4.1 kB     00:00     
Resolving Dependencies
--> Running transaction check
---> Package autofs.x86_64 1:5.0.7-40.el7 will be installed
--> Processing Dependency: libhesiod.so.0()(64bit) for package: 1:autofs-5.0.7-40.el7.x86_64
--> Running transaction check
---> Package hesiod.x86_64 0:3.2.1-3.el7 will be installed
[root@client ~]# cd /net         #此时没有/net目录。
-bash: cd: /net: No such file or directory
[root@client ~]# systemctl start autofs  #开启服务后,会自动建立/net目录。
[root@client ~]# cd /net/     
[root@client net]# ls    #此时无法看到里面内容,必须手动输入服务端ip进入。
[root@client net]# cd 172.25.254.126
[root@client 172.25.254.126]# ls
westos
[root@client 172.25.254.126]# cd westos/  #进入共享目录
[root@client westos]# df
Filesystem             1K-blocks    Used Available Use% Mounted on
/dev/vda1               10473900 3188420   7285480  31% /
devtmpfs                  469332       0    469332   0% /dev
tmpfs                     484920      80    484840   1% /dev/shm
tmpfs                     484920   12764    472156   3% /run
tmpfs                     484920       0    484920   0% /sys/fs/cgroup
/dev/mapper/vg0-vo        483670    2366    451813   1% /home
172.25.254.126:/westos  10473984 3164288   7309696  31%  /net/172.25.254.126/westos
#此时会主动挂载这个目录,退出目录后会默认在300秒后自动卸载。
[root@client westos]# vim /etc/sysconfig/autofs #编辑配置文件,可修改其默认卸载时间。
# minutes to be consistent with earlier autofs releases.
#
TIMEOUT=5  #修改为默认5秒后卸载。
#
# NEGATIVE_TIMEOUT
[root@client westos]# systemctl restart autofs  #重启服务。
[root@client ~]# df
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3412928   7060972  33% /
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    2346    451833   1% /home
[root@client ~]# cd /net/172.25.254.126/westos/
[root@client westos]# df
Filesystem             1K-blocks    Used Available Use% Mounted on
/dev/vda1               10473900 3412908   7060992  33% /
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    2346    451833   1% /home
172.25.254.126:/westos  10473984 3573888   6900096  35% /net/172.25.254.126/westos  #自动挂载
[root@client westos]# cd     #退出
[root@client ~]# df
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3412908   7060992  33% /
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    2346    451833   1% /home 
#5秒后自动卸载。

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

(2)修改挂载目录

客户端:
root@client hello]# vim /etc/auto.master
#
/misc   /etc/auto.misc
/nfs    /etc/auto.westos   
#在挂载目录的上层目录/nfs下挂载,/etc/auto.westos文件中写挂载点和相关参数。
[root@client hello]# vim /etc/auto.westos

hello -ro 172.25.254.126:/westos  
#在/nfs/hello挂载只读172.25.254.126:/westos。

[root@client ~]# cd /nfs    #此时没有此目录。
-bash: cd: /nfs: No such file or directory
[root@client ~]# systemctl restart autofs.service  #重启服务会自动建立此目录。
[root@client ~]# cd /nfs/
[root@client nfs]# ls
[root@client nfs]# cd hello
[root@client hello]# ls
file  file1
[root@client hello]# df     #自动挂载。
Filesystem             1K-blocks    Used Available Use% Mounted on
/dev/vda1               10473900 3188012   7285888  31% /
devtmpfs                  469332       0    469332   0% /dev
tmpfs                     484920      80    484840   1% /dev/shm
tmpfs                     484920   12764    472156   3% /run
tmpfs                     484920       0    484920   0% /sys/fs/cgroup
/dev/mapper/vg0-vo        483670    2366    451813   1% /home
172.25.254.126:/westos  10473984 3164288   7309696  31% /nfs/hello
[root@client hello]# mount

172.25.254.126:/westos on /nfs/hello type nfs4 (ro,relatime,vers=4.0##此时显示只读挂载,版本为vers=4.0##,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.25.254.126,local_lock=none,addr=172.25.254.126)
[root@client hello]# vim /etc/auto.westos
hello -rw,vers=3 172.25.254.126:/westos    #vers版本为3
[root@client hello]# cd
[root@client ~]# systemctl restart autofs.service
[root@client ~]# cd /nfs/hello
[root@client hello]# df
Filesystem             1K-blocks    Used Available Use% Mounted on
/dev/vda1               10473900 3188184   7285716  31% /
devtmpfs                  469332       0    469332   0% /dev
tmpfs                     484920      80    484840   1% /dev/shm
tmpfs                     484920   12764    472156   3% /run
tmpfs                     484920       0    484920   0% /sys/fs/cgroup
/dev/mapper/vg0-vo        483670    2366    451813   1% /home
172.25.254.126:/westos  10473984 3164416   7309568  31% /nfs/hello
[root@client hello]# mount
172.25.254.126:/westos on /nfs/hello type nfs (rw,relatime,vers=3,##此时显示为只读,版本号为vers=3rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=172.25.254.126,mountvers=3,mountport=20048,mountproto=udp,local_lock=none,addr=172.25.254.126)

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

猜你喜欢

转载自blog.csdn.net/yifan850399167/article/details/80616542