linux---nfs共享文件和autofs服务的基本配置

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


[root@xiaoqin ~]   ##服务端

[root@client ~]   ##客户端


一.nfs服务的安装

客户端服务端都要下载

yum install nfs-utils -y

systemctl restart nfs


二.nfs的共享文件

服务端:

扫描二维码关注公众号,回复: 2342864 查看本文章

root@xiaoqin ~]# vim /etc/exports  (配置文件,自己写入)


/mnt   *(sync,ro) 共享mnt目录,共享给所有人,sync允许数据同步,只读

[root@xiaoqin ~]# exportfs -rv  (刷新,一般刷新就可以了,重启很慢)

exporting *:/mnt


客户端:

[root@client ~]# showmount -e 172.25.30.235  (查看服务端共享文件)


客户端怎么使用

root@client ~]# mount 172.25.30.235:/mnt/ /mnt/ (将其挂载到/mnt下就可以使用)


[root@client ~]# cd /mnt

[root@client mnt]# ls

file1  file2  file3  file4  file5  (因为只有只读权限,所以无法对文件进行更改)

[root@client mnt]# rm -fr *



服务端可写

服务端:

[root@xiaoqin ~]# vim /etc/exports  (配置文件,自己写入)

客户端:

[root@client ~]# mount 172.25.30.235:/mnt/ /mnt/

[root@client ~]# 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

[root@client ~]# cd /mnt

[root@client mnt]# ls

file1  file2  file3  file4  file5

[root@client mnt]# rm -fr file1

rm: cannot remove ‘file1’: Permission denied  (不是服务本身拒绝而是因为/mnt只允许超级用户可写)


服务端:

[root@xiaoqin ~]# vim /etc/exports  (配置文件,自己写入)


/mnt   *(sync,rw,no_root_squash)   (当客户端使用超级用户挂载服务端所共享目录时不转换其用户身份,依然保留超级用户权限)

服务端测试:

可以创建新的文件


指定身份

服务端:

[root@xiaoqin ~]# vim /etc/exports  (配置文件,自己写入)


 /mnt   *(sync,rw,anonuid=1000,anongid=1000)  (指定的为student用户)

客户端:

[root@client ~]# mount 172.25.30.235:/mnt/ /mnt/

[root@client ~]# cd /mnt

[root@client mnt]# touch westos1

[root@client 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目录下的文件是共享所得服务端中的文件

root@client ~]# cd /net

-bash: cd: /net: No such file or directory

[root@client ~]# cd /net



服务端:

root@xiaoqin ~]# vim /etc/exports



[root@xiaoqin ~]# chmod 777 /westos

[root@xiaoqin ~]# exportfs -rv (刷新)

exporting 172.25.30.0/24:/westos


客户端:

[root@client net]# cd 172.25.30.235

[root@client 172.25.30.235]# pwd

/net/172.25.30.235

[root@client 172.25.30.235]# ls

westos

[root@client 172.25.30.235]# cd westos

[root@client westos]# ls

file1  file2  file3

[root@client westos]# df



怎么测试自动autofs的卸载:

root@client ~]# vim /etc/sysconfig/autofs (配置文件)  将时间间隔设为5s


[root@client ~]# systemctl restart autofs.service  (刷新)

[root@client ~]

[root@client ~]# cd /net

[root@client net]# cd 172.25.30.235

[root@client 172.25.30.235]# ls

westos

[root@client 172.25.30.235]# cd westos

[root@client westos]# ls

file1  file2  file3

[root@client 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

[root@client westos]# cd

[root@client ~]# 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

[root@client ~]# 

这个实验证明在过5s之后,如果没有设备被使用,autofs会自动卸载这个闲置的设备



2.自定义挂载点:

客户端:

[root@client ~]# vim /etc/auto.master  (主策略挂在点)


/nfs   /etc/auto.westos  (名称任意,是自己建立的最终挂载点)

vim   /etc/auto.westos

westos      -ro    172.25.30.235:/westos  里面自己写的挂在参数,权限

 

root@client ~]# systemctl restart autofs (刷新autofs)

[root@client ~]# cd /nfs

[root@client nfs]# cd westos

[root@client westos]# ls

file1  file2  file3

[root@client westos]# mount  (可以看到挂载设备及其参数)


猜你喜欢

转载自blog.csdn.net/a939029674/article/details/80710292