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(rw,sync,no_root_squash)

rw:read-write,可读写;    注意,仅仅这里设置成读写客户端还是不能正常写入,还要正确地设置共享目录的权限,参考问题7  
ro:read-only,只读;  
sync:文件同时写入硬盘和内存;  
async:文件暂存于内存,而不是直接写入内存;  
no_root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,也拥有root权限。显然开启这项是不安全的。  
root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,拥有匿名用户权限,通常他将使用nobody或nfsnobody身份;  
all_squash:不论NFS客户端连接服务端时使用什么用户,对服务端分享的目录来说都是拥有匿名用户权限;  
anonuid:匿名用户的UID值,通常是nobody或nfsnobody,可以在此处自行设定;  
anongid:匿名用户的GID值。

数据导出

[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

注:
ubuntu客户端

apt-get install libnfs-dev nfs-common libnfs-utils -y  \
&&	mkdir -p /data/nfs \
&&	showmount -e ip \
&&	mount ip:/data /data/nfs

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

  • 9
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值