Linux服务篇-NFS-网络文件系统

一、NFS-网络文件系统

1、介绍

NFS(Network File System),即网络文件系统。

网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS.,它允许一个系统在网络上与他人共享目录和文件。通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件。

模式:C/S模式
端口:2049

在这里插入图片描述

2、安装

安装包
nfs-utils:基本的NFS命令与监控程序
portmap:支持安全NFS RPC服务的连接

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

启动

[root@qianshuiliyu ~]# systemctl restart  rpcbind.service   
 [root@qianshuiliyu ~]# systemctl status nfs
 [root@qianshuiliyu ~]# systemctl enable nfs

查看端口
在这里插入图片描述

3、服务端操作

不要在服务器的目录里设置,有些目录特殊会出问题,这边设置没问题,客户端那边会出问题

1、创建测试信息

[root@qianshuiliyu ~]#  mkdir  /nfs
[root@qianshuiliyu ~]# cp /etc/passwd /etc/shadow /nfs/

2、配置文件

[root@qianshuiliyu ~]# vim /etc/exports
/root/nfs/ *(rw)
# * 表示对所有网段开放权限,也可以设置网段或者主机
============================================================
[root@qianshuiliyu ~]# exportfs -rv
exporting *:/root/nfs

重启

[root@qianshuiliyu ~]# systemctl restart nfs
============================================================
# 重新读取配置文件,不中断服务
[root@qianshuiliyu ~]# exportfs -rv
exporting *:/nfs

4、客户端

1、命令
mount -t
-t,指定挂载系统
showmount -e NFS服务器IP

[root@qianshuiliyu_client ~]# man showmount  
# 查看说明,很简单
# 列出挂载信息,有了之后进行挂载
[root@qianshuiliyu_client ~]# showmount -e 192.172.168.100
Export list for 192.172.168.100:
/nfs *

2、在客户端进行挂载

[root@qianshuiliyu_client ~]# mount -t nfs 192.172.168.100:/nfs /mnt/
# 现在系统一般会自动识别
[root@qianshuiliyu_client ~]# mount 192.172.168.100:/nfs /mnt/ 
[root@qianshuiliyu_client ~]# ll /mnt/
总用量 8
-rw-r--r--. 1 root root 1917 10月  4 15:39 passwd
----------. 1 root root 1138 10月  4 15:39 shadow

3、查看,最下面的是

[root@qianshuiliyu_client ~]# df -h
文件系统               容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root   22G  3.7G   19G   17% /
devtmpfs               1.9G     0  1.9G    0% /dev
tmpfs                  1.9G   80K  1.9G    1% /dev/shm
tmpfs                  1.9G  8.9M  1.9G    1% /run
tmpfs                  1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1              497M  108M  390M   22% /boot
192.172.168.100:/nfs    22G  3.7G   19G   17% /mnt

4、设置开机自动挂载

# 修改配置文件或者追加,千万别覆盖
[root@qianshuiliyu_client ~]# echo "192.172.168.100:/nfs /mnt nfs defaults 0 0" >> /etc/fstab

5、权限

1、创建文件

[root@qianshuiliyu_client ~]# cd /mnt/
[root@qianshuiliyu_client mnt]# touch 1
touch: 无法创建"1": 权限不够

2、修改权限

[root@qianshuiliyu ~]# chmod 777 /nfs/

3、客户端创建

[root@qianshuiliyu_client mnt]# touch 1
[root@qianshuiliyu_client mnt]# ls
1  passwd  shadow
[root@qianshuiliyu_client mnt]# 
============================================================
# 服务端也存在
[root@qianshuiliyu ~]# ls /nfs/
1  passwd  shadow

二、配置文件的参数

共享目录是必跟参数外,其他参数都是可选的。
共享目录与客户端之间及客户端与客户端之间需要使用空格符号,但是客户端括号里面的参数与客户端不能空格
空格的

参数说明
ro只读访问
rw读写访问
sync资料同步写入到内存与硬盘当中
async资料会先暂存于内存当中,而非直接写入硬盘
secureNFS通过1024以下的安全TCP/IP端口发送
insecureNFS通过1024以上的端口发送
wdelay如果多个用户要写入NFS目录,则归组写入(默认)
no_wdelay如果多个用户要写入NFS目录,则立即写入,当使用async时,无需此设置。
Hide在NFS共享目录中不共享其子目录
no_hide共享NFS目录的子目录
subtree_check如果共享/usr/bin之类的子目录时,强制NFS检查父目录的权限(默认)
no_subtree_check和上面相对,不检查父目录权限
all_squash共享文件的UID和GID映射匿名用户anonymous,适合公用目录
no_all_squash保留共享文件的UID和GID(默认)
root_squashroot用户的所有请求映射成如anonymous用户一样的权限(默认)
no_root_squashroot用户具有根目录的完全管理访问权限

root_squash(压制):如果用root登录nfs,使其身份自动且换成nfsnobody。
no_root_squash:如果用root登录nfs,使其身份就是root。
all_squash:用户登录nfs时,指定身份为UID/GID的用户,用户要存在
压制root,指定身份,客户noboday,服务指定的身份,有可能不显示

1、实验

[root@qianshuiliyu ~]# vim /etc/exports
/nfs 192.172.168.0/24(rw,no_root_squash)
# 重启
[root@qianshuiliyu ~]# systemctl restart nfs
# 在客户端
[root@qianshuiliyu_client mnt]# touch 5
[root@qianshuiliyu_client mnt]# ll
总用量 8
-rw-r--r--. 1 nfsnobody nfsnobody    0 10月  4 16:07 1
-rw-r--r--. 1 nfsnobody nfsnobody    0 10月  4 16:30 5
-rw-r--r--. 1 root      root      1917 10月  4 15:39 passwd
----------. 1 root      root      1138 10月  4 15:39 shadow
============================================================
# 服务端
[root@qianshuiliyu ~]# ll /nfs/
总用量 8
-rw-r--r--. 1 nfsnobody nfsnobody    0 10月  4 16:07 1
-rw-r--r--. 1 nfsnobody nfsnobody    0 10月  4 16:30 5
-rw-r--r--. 1 root      root      1917 10月  4 15:39 passwd
----------. 1 root      root      1138 10月  4 15:39 shadow

2、再实验

[root@qianshuiliyu ~]# vim /etc/exports
/nfs 192.172.168.0/24(rw,all_squash,anonuid=1000,anongid=1000)
# 重启
[root@qianshuiliyu ~]# systemctl restart nfs

查看客户端
在这里插入图片描述
查看服务端
在这里插入图片描述

3、参数调优

客户端在挂载的时候进行优化或者直接写入配置文件
mount命令

mount说明
-o优化加-o
async 异步同步此参数会提高I/O性能,但会降低数据安全(除非对性能要求很高,对数据可靠性不要求的场合。一般生产环境,不推荐使用)
noatime取消更新文件系统上的inode访问时间,提升I/O性能,优化I/O目的,推荐使用。
nodiratime取消更新文件系统上的directory inode访问时间,高并发环境,推荐显式应用该选项,提高系统性能
intr可以中断不成功的挂载

rsize/wsize 读取(rsize)/写入(wsize)的区块大小(block size),这个设置值可以影响客户端与服务端传输数据的缓冲存储量。一般来说,如果在局域网内,并且客户端与服务端都具有足够的内存,这个值可以设置大一点,比如说32768(bytes),提升缓冲区块将可提升NFS文件系统的传输能力。但设置的值也不要太大,最好是实现网络能够传输的最大值为限。

内和优化
内核优化:

net.core.wmem_default = 8388608 #内核默认读缓存
net.core.rmem_default = 8388608 #内核默认写缓存
net.core.rmem_max = 16777216 #内核最大读缓存
net.core.wmem_max = 16777216 #内核最大写缓存

[root@qianshuiliyu_client ~]# mount -o noatime,nodiratime,intr 192.172.168.100:/nfs /mnt/

在这里插入图片描述

三、自动挂载

1、说明

在客户端使用
通过autofs实现自动挂载
自动挂载涉及到两个文件,auto.master和auto.misc,也可能直接是autofs软件包

[root@qianshuiliyu ~]# yum -y install autofs.x86_64 

/etc/auto.master 文件定义本地挂载点.
/etc/auto.misc 配置文件是用来设置需要挂载的文件系统类型和选项

2、配置

# 创建挂载点
[root@qianshuiliyu ~]# mkdir /gzd
[root@qianshuiliyu ~]# vim /etc/auto.master
/nfs/ /etc/auto.nfs  --timeout=60
#  挂载超时时间,单位为秒。可以修改这个参数。
============================================================
[root@xuegod64 ~]# vim /etc/auto.nfs
nfs   -fstype=nfs	192.172.168.100:/nfs/chuang
# cd切换目录到/nfs/chuang,触发自动挂载,超过时间自动卸载
# 在自动挂载时目录会自动创建

注: 只有cd /tmp/a/nfs 进去, 触发一下,才能自动挂载。 另外 nfs目录,不能提前创建,自动挂载时,系统自动创建nfs目录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅水鲤鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值