14.4 exportfs命令

时间:2022-04-27
本文章向大家介绍14.4 exportfs命令,主要内容包括exportfs命令、exportfs命令、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

exportfs命令

  • 常用选项
  • -a 全部挂载或者全部卸载
  • -r 重新挂载
  • -u 卸载某一个目录
  • -v 显示共享目录
  • 以下操作在服务端上 -vim /etc/exports //增加
/tmp/ 192.168.133.0/24(rw,sync,no_root_squash)
  • exportfs -arv //不用重启nfs服务,配置文件就会生效
  • 以下操作在客户端
  • mkdir /aminglinux
  • mount -t nfs -onolock 192.168.133.130:/tmp /aminglinux
  • touch /aminglinux/test.txt
  • ls -l !$
  • -oremount,nfsvers=3

exportfs命令

  • exportfs命令和nfs-utils这个包一起安装的
  • 例子:
    • 假设在第一次配置nfs的共享目录,之后需要新增、更改某些机器或共享的目录;
    • 首先需要更改配置文件,然后重启NFS服务,但如果远程客户端正在使用NFS服务,正在挂载着,如果你需要先停止nfs服务,那远程的客户端就会挂起,就会很大的影响,造成服务异常,进程异常,有很大可能导致系统坏掉
  • nfs服务不能随便重启,要重启,就需要先去服务器上,把挂载的目录卸载下来
    • 在卸载目录的时候,若是在当前目录下去卸载会提示umount.nfs4: /mnt: device is busy
      • 方法一:退出该目录后,再去卸载
      • 方法二:在目录下卸载的时候,加 -l 选项,表示 lazy 懒惰的意思
    • 在卸载目录后,在重启nfs服务
  • 若是挂载了很多台机器,那么每台机器都需要去卸载,就会很麻烦,降低了工作效率
    • 方法:使用exportfs命令,重新加载下
  • exportfs命令
    • -a 全部挂载或者全部卸载
    • -r 重新挂载
    • -u 卸载某一个目录
    • -v 显示共享目录
  1. 在B机器客户端,卸载目录
[root@hf-01 ~]# umount /mnt/
[root@hf-01 ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3        18G  6.5G   12G   37% /
devtmpfs        483M     0  483M    0% /dev
tmpfs           493M     0  493M    0% /dev/shm
tmpfs           493M  6.7M  486M    2% /run
tmpfs           493M     0  493M    0% /sys/fs/cgroup
/dev/sda1       197M  109M   88M   56% /boot
tmpfs            99M     0   99M    0% /run/user/0
[root@hf-01 ~]# 
  1. 然后在A机器服务端,使用exportfs -arv命令
  • exportfs -arv命令,重新使配置文件生效
[root@hanfeng ~]# exportfs -arv
exporting 192.168.202.0/24:/home/nfstestdir
[root@hanfeng ~]# 
  1. 验证,在A机器上的/etc/exports配置文件中,在增加一行,把 /tmp 目录单独共享给192.168.202.131 这个IP
[root@hanfeng ~]# vim /etc/exports

在配置文件中加入
/tmp 192.168.202.131(rw,sync,no_root_squash)

结果如下
/home/nfstestdir  192.168.202.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
/tmp 192.168.202.131(rw,sync,no_root_squash)

保存退出
  1. 然后在A机器服务端执行exportfs -arv命令
[root@hanfeng ~]# exportfs -arv
exporting 192.168.202.131:/tmp
exporting 192.168.202.0/24:/home/nfstestdir
[root@hanfeng ~]# 
  1. 在B机器客户端showmount -e看是否生效——>并没有重启nfs服务,就已经生效
[root@hf-01 ~]# showmount -e 192.168.202.130
Export list for 192.168.202.130:
/home/nfstestdir 192.168.202.0/24
/tmp             192.168.202.131
[root@hf-01 ~]# 
  1. 在B机器客户端挂载,将 tmp目录 挂载到 mnt 目录下
[root@hf-01 ~]# mount -t nfs 192.168.202.130:/tmp/ /mnt/
[root@hf-01 ~]# df -h
文件系统              容量  已用  可用 已用% 挂载点
/dev/sda3              18G  6.5G   12G   37% /
devtmpfs              483M     0  483M    0% /dev
tmpfs                 493M     0  493M    0% /dev/shm
tmpfs                 493M  6.7M  486M    2% /run
tmpfs                 493M     0  493M    0% /sys/fs/cgroup
/dev/sda1             197M  109M   88M   56% /boot
tmpfs                  99M     0   99M    0% /run/user/0
192.168.202.130:/tmp   18G  3.2G   15G   18% /mnt
[root@hf-01 ~]# 
  1. 现在在B机器客户端,查看到的/mnt/目录就是192.168.202.130IP下的tmp目录
[root@hf-01 ~]# ls /mnt/
hanfeng.sock  mysql.sock  php-fcgi.sock  test.com.log
[root@hf-01 ~]# 
  1. 再到A机器上查看tmp目录,会看到两个目录下的文件内容会一样的
[root@hanfeng ~]# ls /tmp/
hanfeng.sock  mysql.sock  php-fcgi.sock  test.com.log
[root@hanfeng ~]# 
  1. 这时在B机器客户端的mnt目录下创建文件,并查看目录下的文件,能看到新建的1212.txt文件的属主和属组都是root
[root@hf-01 ~]# vim /mnt/1212.txt
[root@hf-01 ~]# ls -l /mnt/
总用量 8
-rw-r--r-- 1 root  root    12 1月  17 2018 1212.txt
srw-rw-rw- 1 root  root     0 1月  17 2018 hanfeng.sock
srwxrwxrwx 1 mysql mysql    0 1月  17 2018 mysql.sock
srw-rw-rw- 1 root  root     0 1月  17 2018 php-fcgi.sock
-rw-r--r-- 1 root  root  2383 1月   8 19:41 test.com.log
[root@hf-01 ~]# 
  1. 这时再到A机器服务端查看tmp目录,会看到1212.txt文件的属主和属组也是root
[root@hanfeng ~]# ls -l /tmp/
总用量 8
-rw-r--r--. 1 root  root    12 1月  17 21:53 1212.txt
srw-rw-rw-. 1 root  root     0 1月  17 18:44 hanfeng.sock
srwxrwxrwx. 1 mysql mysql    0 1月  17 18:44 mysql.sock
srw-rw-rw-. 1 root  root     0 1月  17 18:44 php-fcgi.sock
-rw-r--r--. 1 root  root  2383 1月   8 19:41 test.com.log
[root@hanfeng ~]# 
  1. 这就是因为在A机器服务端的配置文件中,使用了no_root_squash ,所以root用户不受约束,在B机器上到挂载点下,到共享目录下,就可以像在本地磁盘使用root用户一样,是不受限制的(通常情况下,不限制root用户的比较多)