一、NFS服务
NFS:网络文件系统,是Unix系统和网络附加存储文件管理常用的文件系统,允许多个客户端通过网络共享文件访问;
二、NFS管理
1.服务端安装nfs
yum install nfs-utils

2.打开nfs,进行配置
[root@localhost ~]# systemctl start nfs

[root@localhost ~]# mkdir /nfsdir

[root@localhost ~]# vim /etc/exports


/nfsdir *(sync) ##所有人同步数据,只读挂载
刷新nfs配置
[root@localhost ~]# exportfs -rv

exporting *:/nfsdir
客户端安装nfs
查看可使用的挂载点
[root@foundation8 ~]# showmount -e 172.25.254.108

Export list for 172.25.254.108:
/nfsdir *
挂载并编辑
[root@foundation8 ~]# mount 172.25.254.108:/nfsdir /mnt
[root@foundation8 ~]# touch /mnt/file
touch: cannot touch ‘/mnt/file’: Read-only file system

3.服务端给指定主机权限
[root@localhost ~]# vim /etc/exports
/nfsdir *(sync) 172.25.254.8(rw,sync)

##允许所有人同步,8主机具有读写权限
[root@localhost ~]# exportfs -rv

exporting 172.25.254.8:/nfsdir
exporting *:/nfsdir
客户端挂载并编辑
[root@foundation8 ~]# mount 172.25.254.108:/nfsdir /mnt

[root@foundation8 ~]# touch /mnt/file
touch: cannot touch ‘/mnt/file’: Permission denied
##权限拒绝,跟/nfsdir目录权限有关



三、配置自动挂载/卸载 ##客户端
1.安装autofs服务

2.开启autofs服务,自动生成/net

3.实现自动挂载 ##切换到/net/172.25.254.108/nfsdir即可
[root@foundation8 ~]# cd /net/
[root@foundation8 net]# ls
[root@foundation8 net]# cd 172.25.254.108
[root@foundation8 172.25.254.108]# cd nfsdir/
[root@foundation8 nfsdir]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 120529408 32637428 87891980 28% /
devtmpfs 1868076 0 1868076 0% /dev
tmpfs 1881152 496 1880656 1% /dev/shm
tmpfs 1881152 9000 1872152 1% /run
tmpfs 1881152 0 1881152 0% /sys/fs/cgroup
/dev/sda1 508588 149316 359272 30% /boot
/dev/loop0 3654720 3654720 0 100% /var/www/html/rhel7.0
/dev/loop1 3947824 3947824 0 100% /var/www/html/rhel7.2
tmpfs 376232 24 376208 1% /run/user/1000
172.25.254.108:/nfsdir 10473984 3187072 7286912 31% /net/172.25.254.108/nfs
4.当挂载点不使用时,会自动卸载,默认时间为300S
编辑配置文件,变更自动卸载时间
[root@foundation8 172.25.254.108]# vim /etc/autofs.conf
10 # timeout - set the default mount timeout in secons. The internal
11 # program default is 10 minutes, but the default installed
12 # configuration overrides this and sets the timeout to 5
13 # minutes to be consistent with earlier autofs releases.
14 #
15 timeout = 10

5.设置自动挂载目录
[root@foundation8 ]# vim /etc/auto.master

7 /misc /etc/auto.misc
8 /mnt /etc/auto.mnt (挂载的上级目录)

[root@foundation8 ]# vim /etc/auto.mnt
[root@foundation8 mnt]# cat /etc/auto.mnt
nfsdir -ro,vers=3 172.25.254.108:/nfsdir

重启后,自动以配置参数挂载
[root@foundation8 ]# cd /mnt
[root@foundation8 mnt]# ls
[root@foundation8 mnt]# cd nfsdir

[root@foundation8 nfsdir]# df
6.自动挂载的权限设置
服务端修改配置文件,指定root用户挂载
[root@localhost ~]# vim /etc/exports
[root@localhost ~]# exportfs -rv
exporting 172.25.254.8:/nfsdir
exporting *:/nfsdir
[root@localhost ~]# cat /etc/exports
/nfsdir *(sync) 172.25.254.8(rw,sync,no_root_squash)
客户端配置
[root@foundation8 mnt]# cat /etc/auto.mnt
nfsdir -rw,vers=3 172.25.254.108:/nfsdir
[root@foundation8 mnt]# systemctl restart autofs.service
[root@foundation8 mnt]# cd nfsdir
[root@foundation8 nfsdir]# ls
[root@foundation8 nfsdir]# touch file1
[root@foundation8 nfsdir]# ls
file1
服务端修改配置文件,指定用户user和组group
[root@localhost ~]# vim /etc/exports
[root@localhost ~]# exportfs -rv
exporting 172.25.254.8:/nfsdir
exporting *:/nfsdir
[root@localhost ~]# cat /etc/exports
/nfsdir *(sync) 172.25.254.8(rw,sync,anonuid=1000,anongid=1000)
设定权限 chmod 777 /nfsdir/
客户端配置
[root@foundation8 mnt]# cat /etc/auto.mnt
nfsdir -rw,vers=3 172.25.254.108:/nfsdir
[root@foundation8 mnt]# cd nfsdir
[root@foundation8 nfsdir]# touch file2
[root@foundation8 nfsdir]# ll
total 0
-rw-r--r-- 1 root root 0 May 4 15:57 file1
-rw-r--r-- 1 kiosk kiosk 0 May 4 15:59 file2