Centos NFS服务及自动挂载

NFS服务及自动挂载

文件共享服务器
概念,网络文件系统,通过网络,让不同的系统、机器实现文件共享
工作原理:由多个模块共同来完成,端口随机,通过RPC代理程序来帮助连接NFS服务

一、环境

服务端:192.168.1.111

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)

客户端:192.168.1.107

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)

二、nfs安装配置-服务端

安装环境支持

[root@localhost ~]# yum install nfs-utils -y

创建NFS共享目录

[root@localhost ~]# mkdir -p /data/nfsshare

随便复制一个文件至共享目录

[root@localhost ~]# cp /etc/fstab /data/nfsshare

配置文件,exports此文件名固定

[root@localhost ~]# vim  /etc/exports   
/data/nfsshare 192.168.1.107(ro,sync)   对客户端定义,ro为只读,rw为可写

注:也可以写成网段的形式,要加/24,不可写0.0.0.0,可以多行,如下:
/data/nfsshare 192.168.1.0/24(ro,sync)

数据导出

[root@localhost ~]# exportfs -r

重启服务

[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl restart nfs 

注:#chmod o+w /data/nfsshare 如果客户端要求可写比如共享目录,那么需要加上这一句
关闭selinux及防火墙(这里就不做防火墙配置了,直接关闭)

[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/selinux/config 
SELINUX=disabled
[root@localhost ~]# systemctl stop firewalld

三、nfs安装配置-客户端

安装环境支持

[root@localhost ~]#yum install nfs-utils -y

查看服务端挂载信息

[root@localhost ~]# showmount -e 192.168.1.111
Export list for 192.168.1.111:
/data/nfsshare 192.168.1.0/24

创建本机挂载目录

[root@localhost ~]# mkdir -p /data/nfs

临时挂载

[root@localhost ~]# mount 192.168.1.111:/data/nfsshare /data/nfs

mount -t nfs 192.168.1.111:/data/nfsshare /data/nfs 也可以这样写,加-t nfs
测试

[root@localhost nfs]# ll /data/nfs
total 4
-rw-r--r--. 1 root root 541 May 17 11:01 fstab
-rw-r--r--. 1 root root   0 May 17 11:29 test

四、客户端开机自动挂载(fstab配置文件实现开机自动挂载)

开机自动挂载分**"fstab配置文件"手动开机自动挂载"autofs"工具自动挂载**
fstab配置文件手动开机自动挂载**

[root@localhost ~]# vim /etc/fstab   最后加入一行
192.168.1.111:/data/nfsshare /data/nfs  nfs  defaults 0 0 

五、客户端开机自动挂载(autofs工具实现自动挂载)

autofs为守护进程,自动挂载,客户端通过autofo工具,访问时自动挂载,不使用后,5分钟自行断开,卸载。好处是可以隐藏挂载目录
安装autofs

[root@localhost ~]# yum install autofs -y

启动autofs

[root@localhost ~]# systemctl start autofs

#rpm -qc autofs 查看autofs相关文件

[root@localhost ~]# rpm -qc autofs     查看autofs配置文件
/etc/auto.master			定义挂载点与挂载点配置文件
/etc/auto.misc				定义虚拟目录、挂载源及权限等
/etc/auto.net
/etc/auto.smb
/etc/autofs.conf			autofs配置文件,可定义超时卸载时间等,默认300秒,5分钟
/etc/autofs_ldap_auth.conf
/etc/sysconfig/autofs
/usr/lib/systemd/system/autofs.service

开始配置,按照顺序
1、vim /etc/autofs.conf 不做配置
2、vim /etc/auto.master 定义挂载点及挂载点的配置文件

[root@localhost ~]# vim /etc/auto.master	 
/data/nfs  /etc/auto.nfs

3、vim /etc/auto.nfs 定义挂载点下的虚拟目录

[root@localhost ~]# vim /etc/auto.nfs		定义挂载点下的虚拟目录,可定义多行,一个虚拟目录为一行,定义不同的权限及目录等
cd -fstype=nfs,rw,sync  192.168.1.111:/data/nfsshare
cd1 -fstype=nfs,ro,sync  192.168.1.111:/data/nfsshare 

如此,当cd /data/nfs下时,里面无文件,使用cd cd可进入虚拟目录cd。当对cd进行访问时,如ls,会发生自动挂载,里面的内容为挂载后的内容
重启服务

[root@localhost ~]# systemctl restart autofs

设置自动挂载开机自启动

[root@localhost ~]# systemctl enable autofs
Created symlink from /etc/systemd/system/multi-user.target.wants/autofs.service to /usr/lib/systemd/system/autofs.service.

测试

[root@localhost ~]# 
[root@localhost ~]# cd /data/nfs
[root@localhost nfs]# ls      发现无文件
[root@localhost nfs]# ll cd   对虚拟目录cd访问时,会迅速发生挂载,可以看到里面的文件了
total 4
-rw-r--r--. 1 root root 541 May 17 11:01 fstab
-rw-r--r--. 1 root root   0 May 17 11:29 test

-------------------------end

原创文章 4 获赞 4 访问量 259

猜你喜欢

转载自blog.csdn.net/oToyix/article/details/106173325