NFS的基本配置

本文索引:

  • NFS介绍
  • NFS服务器安装配置
  • NFS配置选项

NFS介绍

NFS是网络文件系统(Network File System)的缩写。需要借助网络,实现数据的同步。

NFS最早由Sun公司进行开发,分2,3,4三个版本,2和3版本有Sun公司起草开发,4.0开始由Netapp公司参与主导开发,最新版本为4.1版本。

NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写。

NFS的应用场景是:A,B,C三台机器上需要保证被访问到的文件时一样的,A共享数据出来,B和C分别取挂载A共享的数据目录,从而B和C访问到的数据和A上的一致。


NFS服务端安装配置

  • 软件安装

    服务端安装

    [root@server etc]# yum install -y nfs-utils rpcbind

    客户端安装

    [root@client etc]# yum install -y nfs-utils

  • 编辑/etc/exports文件

    [root@server etc]

    格式:分享的目录 共享的ip(挂载选项)

    /home/nfstestdir 192.168.65.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

  • 创建共享目录并赋予权限

    [root@server etc]# mkdir /home/nfstestdir

    这里为了测试方便给予了777权限,实际工作中需要按需求修改

    [root@server etc]# chmod 777 /home/nfstestdir/

  • 服务端启动nfs服务

    [root@server etc]# systemctl start nfs [root@server etc]# ps aux | grep nfs root 2878 0.0 0.0 0 0 ? S< 19:50 0:00 [nfsd4_callbacks] root 2884 0.0 0.0 0 0 ? S 19:50 0:00 [nfsd] root 2885 0.0 0.0 0 0 ? S 19:50 0:00 [nfsd] root 2886 0.0 0.0 0 0 ? S 19:50 0:00 [nfsd] root 2887 0.0 0.0 0 0 ? S 19:50 0:00 [nfsd] root 2888 0.0 0.0 0 0 ? S 19:50 0:00 [nfsd] root 2889 0.0 0.0 0 0 ? S 19:50 0:00 [nfsd] root 2890 0.0 0.0 0 0 ? S 19:50 0:00 [nfsd] root 2891 0.0 0.0 0 0 ? S 19:50 0:00 [nfsd] root 2896 0.0 0.0 112680 976 pts/0 S+ 19:50 0:00 grep --color=auto nfs

    启动了nfs时会自动启动rpc服务,两者是关联的

    [root@server etc]# ps aux | grep rpc rpc 2644 0.0 0.0 64964 1412 ? Ss 19:28 0:00 /sbin/rpcbind -w rpcuser 2849 0.2 0.1 42380 1748 ? Ss 19:50 0:00 /usr/sbin/rpc.statd root 2850 0.0 0.0 0 0 ? S< 19:50 0:00 [rpciod] root 2862 0.0 0.0 19324 396 ? Ss 19:50 0:00 /usr/sbin/rpc.idmapd root 2868 0.0 0.0 42564 944 ? Ss 19:50 0:00 /usr/sbin/rpc.mountd root 2898 0.0 0.0 112680 976 pts/0 S+ 19:50 0:00 grep --color=auto rpc

  • 设置服务端nfs服务开机启动

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


NFS配置选项

/home/nfstestdir 192.168.65.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
  • rw 可读可写

  • ro 只读

  • sync 同步模式 内存数据实时写入磁盘,可以很快的将数据写入磁盘,相应的会降低磁盘的效率。

  • async 非同步模式 每个一段时间再将数据写入磁盘,缺点是当某一时刻断电,将会导致部分数据丢失。

  • no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大。当客户端需要共享目录的数据时,将该目录挂载到本机的某个目录,客户端的root权限可以随意对共享目录内的数据进行操作。

  • root_squash 相对于no_root_squash参数而言,客户端上的root用户在访问时被转换为某个普通用户,权限被限制。

  • all_squash 客户端上所有用户在使用NFS共享目录时都会被限制以某个普通用户的身份进行数据的操作。

  • anonuid/anongid 配合squash的参数,定义限制后的用户身份。

客户端挂载远程目录

  • showmount -e命令查看远程的共享目录

    防火墙未放开rpc服务的111端口

    [root@client ~]# showmount -e 192.168.65.133 clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

    服务端关闭防火墙和selinux

    [root@server etc]# systemctl stop firewalld [root@server etc]# setenforce 0

    客户端成功访问

    [root@client ~]# showmount -e 192.168.65.133 Export list for 192.168.65.133: /home/nfstestdir 192.168.65.0/24

  • 挂载远程共享目录

    [root@client ~]# mount -t nfs 192.168.65.133:/home/nfstestdir /mnt [root@client ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda3 18G 3.6G 15G 20% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.7M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 197M 109M 88M 56% /boot tmpfs 98M 0 98M 0% /run/user/0 192.168.65.133:/home/nfstestdir 18G 3.4G 15G 20% /mnt

  • 客户端/服务端文件说明

    客户端挂载共享目录后,在该目录下创建一个新用户,其权限如下

    [root@client ~]# cd /mnt/ [root@client mnt]# touch test [root@client mnt]# ls -l 总用量 0 -rw-r--r--. 1 mysql mysql 0 1月 9 20:45 test

    服务器也生成一个文件,其权限如下:

    [root@localhost etc]# ls -l /home/nfstestdir/ 总用量 0 -rw-r--r--. 1 mysql mysql 0 1月 9 20:45 test

    [root@localhost etc]# id mysql uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)

关于为什么文件的属主和属组是mysql的说明:由于在/etc/exports文件内定义了all_squash,anonuid=1000,anongid=1000参数,更新目录下新创建的文件将以服务器上uid/gid为1000的用户最为属主和属组。同样的客户端共享目录下的文件也会以uid/gid为1000的用户作为其属主和属组。


点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

ssh服务认证

系统信息root@nfs01~unamer2.6.32696.el6.x86_64root@nfs01~unamemx86_64root@nfs01~cat/etc/redhatreleaseCentOS

Linux NFS服务详解

1.什么是NFS(NetworkFileSystem) NFS就是NetworkFileSystem的缩写,最早之前是由Sun所发展出来的。他最大的功能就是可以透过网络,让不同的机器、不同的操作系统、可以彼此分享个别的档案(sharefile),所以,也可以简单的将他看做是一个fileserver呢!这个NFSS

NFS 服务器搭建

NFS(网络文件系统)用于Unix/Linux之间的文件共享,NFS在文件传送过程中依赖与RPC(远程过程调用)协议。NFS本身是没有提供信息传送的协议和功能。只要用到NFS的地方都需要启动RPC服务,不论是NFS的服务端还是客户端。在启动NFS服务之前,首先要启动RPC服务(CentOS5是portmap服务,CentOS6.6以后的版本是rp

Linux NFS 详解

目录:1、了解NFS服务2、NFS主要文件3、NFS安装4、NFS配置5、NFS客户端配置   6、固定NFS端口7、Windows挂载NFS1、了解NFS服务   NFS是Network 

14.1 NFS介绍 14.2 NFS服务端安装配置 14.3 NFS配置选项

第14章NFS服务搭建与配置14.1NFS介绍NFS(NetworkFileSystem)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访