linux---nfs共享文件和autofs服务的基本配置
NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。
[[email protected] ~] ##服务端
[[email protected] ~] ##客户端
一.nfs服务的安装
客户端服务端都要下载
yum install nfs-utils -y
systemctl restart nfs
二.nfs的共享文件
服务端:
[email protected] ~]# vim /etc/exports (配置文件,自己写入)
/mnt *(sync,ro) 共享mnt目录,共享给所有人,sync允许数据同步,只读
[[email protected] ~]# exportfs -rv (刷新,一般刷新就可以了,重启很慢)
exporting *:/mnt
客户端:
[[email protected] ~]# showmount -e 172.25.30.235 (查看服务端共享文件)
客户端怎么使用:
[email protected] ~]# mount 172.25.30.235:/mnt/ /mnt/ (将其挂载到/mnt下就可以使用)
[[email protected] ~]# cd /mnt
[[email protected] mnt]# ls
file1 file2 file3 file4 file5 (因为只有只读权限,所以无法对文件进行更改)
[[email protected] mnt]# rm -fr *
服务端可写
服务端:
[[email protected] ~]# vim /etc/exports (配置文件,自己写入)
客户端:
[[email protected] ~]# mount 172.25.30.235:/mnt/ /mnt/
[[email protected] ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3152204 7321696 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17028 925620 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
172.25.30.235:/mnt 10473984 3157504 7316480 31% /mnt
[[email protected] ~]# cd /mnt
[[email protected] mnt]# ls
file1 file2 file3 file4 file5
[[email protected] mnt]# rm -fr file1
rm: cannot remove ‘file1’: Permission denied (不是服务本身拒绝而是因为/mnt只允许超级用户可写)
服务端:
[[email protected] ~]# vim /etc/exports (配置文件,自己写入)
/mnt *(sync,rw,no_root_squash) (当客户端使用超级用户挂载服务端所共享目录时不转换其用户身份,依然保留超级用户权限)
服务端测试:
可以创建新的文件
指定身份
服务端:
[[email protected] ~]# vim /etc/exports (配置文件,自己写入)
/mnt *(sync,rw,anonuid=1000,anongid=1000) (指定的为student用户)
客户端:
[[email protected] ~]# mount 172.25.30.235:/mnt/ /mnt/
[[email protected] ~]# cd /mnt
[[email protected] mnt]# touch westos1
[[email protected] mnt]# ll
共享文件到那一个网段:
westos 172.25.30.0/24(sync) 172.25.30.250(sync,rw) 这个172.25.30.0/24这个网段实行数据同步,172.25.30.250实现数据同步的时候保持可写
三.Autofs
utofs----自动挂在工具
autofs是一个守护进程,如果它检测到某个尚未挂载的文件系统,日哦过存在,autofs会自动将其挂载;如果它发现有设备挂载一段时间但是没有被使用,autofs会将其自动卸载
1.客户端安装Autofs软件
yum install autofs -y
当autofs软件没有打开时,net目录不存在,当autofs软件打开是,net目录就会存在,net目录下的文件是共享所得服务端中的文件
[email protected] ~]# cd /net
-bash: cd: /net: No such file or directory
[[email protected] ~]# cd /net
服务端:
[email protected] ~]# vim /etc/exports
[[email protected] ~]# chmod 777 /westos
[[email protected] ~]# exportfs -rv (刷新)
exporting 172.25.30.0/24:/westos
客户端:
[[email protected] net]# cd 172.25.30.235
[[email protected] 172.25.30.235]# pwd
/net/172.25.30.235
[[email protected] 172.25.30.235]# ls
westos
[[email protected] 172.25.30.235]# cd westos
[[email protected] westos]# ls
file1 file2 file3
[[email protected] westos]# df
怎么测试自动autofs的卸载:
[email protected] ~]# vim /etc/sysconfig/autofs (配置文件) 将时间间隔设为5s
[[email protected] ~]# systemctl restart autofs.service (刷新)
[[email protected] ~]# cd /net
[[email protected] net]# cd 172.25.30.235
[[email protected] 172.25.30.235]# ls
westos
[[email protected] 172.25.30.235]# cd westos
[[email protected] westos]# ls
file1 file2 file3
[[email protected] westos]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3158076 7315824 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17004 925644 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
172.25.30.235:/westos 10473984 3158784 7315200 31% /net/172.25.30.235/westos
[[email protected] westos]# cd
[[email protected] ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3158076 7315824 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17004 925644 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
[[email protected] ~]#
这个实验证明在过5s之后,如果没有设备被使用,autofs会自动卸载这个闲置的设备
2.自定义挂载点:
客户端:
[[email protected] ~]# vim /etc/auto.master (主策略挂在点)
/nfs /etc/auto.westos (名称任意,是自己建立的最终挂载点)
vim /etc/auto.westos
westos -ro 172.25.30.235:/westos 里面自己写的挂在参数,权限
[email protected] ~]# systemctl restart autofs (刷新autofs)
[[email protected] ~]# cd /nfs
[[email protected] nfs]# cd westos
[[email protected] westos]# ls
file1 file2 file3
[[email protected] westos]# mount (可以看到挂载设备及其参数)