学习nfs服务

学习nfs服务

NFS服务端
1.安装nfs服务
nfs-utils:NFS服务的主程序,包括rpc.nfsd rpc.mountd的这两个守护进程,相关配置文件信息
rpcbind:是一个RPC服务进程

[root@localhost chaoge]# yum install nfs-utils rpcbind

2.环境的配置
准备一个NFS服务端,一个NFS客户端,两台linux机器
在NFS服务端的机器上创建一个用户NFS文件共享的文件夹/nfchaoge,且设置好权限,创建测试文件测试.txt和测试1.txt,并设置好权限

[root@localhost ~]# mkdir /nfchaoge
[root@localhost ~]# chmod -Rf 777 /nfchaoge/
[root@localhost ~]# touch /nfchaoge/测试.txt
[root@localhost ~]# touch /nfchaoge/测试1.txt
[root@localhost ~]# ll -d  /nfchaoge/
drwxrwxrwx 2 root root 43 Sep  4 03:55 /nfchaoge/
[root@localhost ~]# chown -R nfsnobody.nfsnobody /nfchaoge/
[root@localhost ~]# ll -d  /nfchaoge/
drwxrwxrwx 2 nfsnobody nfsnobody 43 Sep  4 03:55 /nfchaoge/

3.修改NFS服务配置文件
注:默认配置文件路径/etc/exports
exports配置文件语法
NFS共享目录 NFS客户端地址(参数1、参数2…) 客户端2地址(参数1、参数2…)
语法参数解析:
NFS共享目录:NFS服务器要共享的实际目录,必须要绝对路径
NFS客户端地址:NFS服务器端授权可以访问共享目录的客户端地址
权限参数:ro 只读
rw 读写
root_squash 当nfs客户端以root访问时,它的权限映射为NFS服务端的匿名用户,它的用户ID/GID变成nfsnobody
no_root_squash 当nfs客户端以root访问时,它的权限映射为NFS服务端root
all_squash 所有nfs客户端用户映射为匿名用户,生成常用参数
sync 数据同步写入到内存与硬盘,数据安全,性能差
async 数据写入到内存,再写入硬盘,效率高,可能内存数据会丢失
insecure 允许客户端从1024的端口发送请求

[root@localhost ~]# vim /etc/exports
/nfchaoge *(insecure,rw,sync)

4.重新加载NFS服务(先启动rpcbind服务,在启动nfs服务)

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

5.检查本地的nfs服务的挂载情况

[root@localhost ~]# showmount -e
Export list for localhost.localdomain:
/nfchaoge *

6.检查nfs服务端的共享参数

[root@localhost ~]# cat /var/lib/nfs/etab 
/nfchaoge	*(rw,sync,wdelay,hide,nocrossmnt,insecure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,insecure,root_squash,no_all_squash)

7.把本地端作为客户端,模拟挂载访问

[root@localhost ~]# mount -l|grep mnt
[root@localhost ~]# mount -t nfs 192.168.3.120:/nfchaoge /mnt
[root@localhost ~]# mount -l|grep mnt
192.168.3.120:/nfchaoge on /mnt type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.3.120,local_lock=none,addr=192.168.3.120)
[root@localhost ~]# ls /mnt
测试1.txt  测试.txt

如果不用nfs共享目录了,直接取消挂载即可

[root@localhost ~]# umount /mnt

NFS客户端
注:配置前检查工作
在另一台linux机器上也要安装nfs服务和rpcbind服务

[root@localhost chaoge]# yum install nfs-utils rpcbind

检查防火墙是否关闭

[root@gjp ~]# iptables -L
[root@gjp ~]# iptables -F
[root@gjp ~]# systemctl stop firewalld

确保rpcbind服务正常。处于 active (running)

[root@gjp ~]# systemctl status rpcbind

检查远程挂载情况

[root@gjp ~]# showmount -e 192.168.3.120
Export list for 192.168.3.120:
/nfchaoge *

以nfs协议远程挂载服务端,并检查nfs客户端本地的挂载情况

[root@gjp ~]# mount -t nfs 192.168.3.120:/nfchaoge /ceshi
[root@gjp ~]# mount -l |tail -1
192.168.3.120:/nfchaoge on /ceshi type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.3.130,local_lock=none,addr=192.168.3.120)
[root@gjp ~]# cd /ceshi
[root@gjp ceshi]# ls
测试1.txt  测试.txt  测试到此一游.txt

如果不用nfs共享目录了,取消挂载目录即可
注:不要在挂载目录中取消挂载不然会报如下错误信息

[root@gjp ceshi]# umount /ceshi
umount.nfs4: /ceshi: device is busy
[root@gjp ceshi]# cd 
[root@gjp ~]# umount /ceshi

nfs自动挂载服务
配置开机挂载服务,每次开机都能使用nfs服务
把挂载命令写入到开机自动挂载文件中/etc/fstab

[root@gjp ~]# vim /etc/fstab 
192.168.3.120:/nfchaoge  /ceshi nfs defaults 0 0

autofs自动挂载服务
mount命令用于挂载文件系统
autofs命令:是一种守护进程,它会在后台检测用户是否要访问某一个还未挂载的文件系统,autofs会自动检测该文件系统是否存在,如果存在则进行挂载,如果用户一段时间没有使用该文件系统,autofs自动将其卸载,节省服务器资源。
缺点:
autofs特点是当用户请求时候才挂载文件系统,如果是高并发的业务场景,大量的用户并发访问文件系统,autofs忽然进行大量挂载,会给服务器造成压力,因此在一些高并发场景下,宁愿保持持续挂载,也不用autofs服务。
autofs配置文件是在/etc/auto.master

[root@gjp ~]# vim /etc/auto.master
/-      /etc/auto.home     #####第七行添加,需要创建auto.home配置文件
[root@gjp ~]# vim /etc/auto.home
/ceshi -rw,soft,intr 192.168.3.120:/nfchaoge


启动autofs

[root@gjp ~]# systemctl restart autofs
[root@gjp ceshi]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/sda3                 20G  4.9G   15G  26% /
devtmpfs                 901M     0  901M   0% /dev
tmpfs                    911M     0  911M   0% /dev/shm
tmpfs                    911M  9.6M  902M   2% /run
tmpfs                    911M     0  911M   0% /sys/fs/cgroup
/dev/sda1                197M  113M   85M  57% /boot
tmpfs                    183M     0  183M   0% /run/user/0

当我们进入/ceshi,autofs服务会自动检测,且自动挂载

[root@gjp ~]# cd /ceshi
[root@gjp ceshi]# ls
123.txt  测试1.txt  测试.txt  测试到此一游.txt
[root@gjp ceshi]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/sda3                 20G  4.9G   15G  26% /
devtmpfs                 901M     0  901M   0% /dev
tmpfs                    911M     0  911M   0% /dev/shm
tmpfs                    911M  9.6M  902M   2% /run
tmpfs                    911M     0  911M   0% /sys/fs/cgroup
/dev/sda1                197M  113M   85M  57% /boot
tmpfs                    183M     0  183M   0% /run/user/0
192.168.3.120:/nfchaoge   20G  5.0G   15G  26% /ceshi

基本搞定,我们可以设置多少秒不使用之后,自动取消挂载

[root@gjp ceshi]# vim /etc/autofs.conf
timeout = 300
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值