当前位置: 移动技术网 > 科技>操作系统>Linux > Linux基础学习-NFS网络文件系统实时文件共享

Linux基础学习-NFS网络文件系统实时文件共享

2018年08月18日  | 移动技术网科技  | 我要评论
NFS网络文件系统 如果大家觉得Samba服务程序的配置太麻烦了,那么你共享文件的主机都是Linux系统,那么推荐大家在客户端部署nfs服务来共享文件.nfs(网络文件系统)服务可以将远程Linux系统上的文件共享资源挂载到本地主机的目录上,从而使得本地主机客户端基于TCP/IP协议,像是用本地主机 ...

nfs网络文件系统

如果大家觉得samba服务程序的配置太麻烦了,那么你共享文件的主机都是linux系统,那么推荐大家在客户端部署nfs服务来共享文件.nfs(网络文件系统)服务可以将远程linux系统上的文件共享资源挂载到本地主机的目录上,从而使得本地主机客户端基于tcp/ip协议,像是用本地主机上的资源那样读写远程linux系统上的共享文件.

实验环境

主机 操作系统 ip地址
nfs服务器 rhel7.4 192.168.56.15
nfs客户端 centos7.4 192.168.56.5
前期准备这里我把防火墙以及selinux都给关闭了.

1 安装 nfs-server 使用 yum install nfs-utils rpcbind -y 命令.

[root@qdlinux ~]# yum install -y nfs-utils rpcbind
loaded plugins: product-id, search-disabled-repos, subscription-manager
this system is not registered with an entitlement server. you can use subscription-manager to register.
dvd                                                                                       | 4.1 kb  00:00:00     
resolving dependencies
--> running transaction check
---> package nfs-utils.x86_64 1:1.3.0-0.48.el7 will be installed
--> processing dependency: gssproxy >= 0.7.0-3 for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> processing dependency: libtirpc >= 0.2.4-0.7 for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> processing dependency: libevent for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> processing dependency: libnfsidmap for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> processing dependency: quota for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> processing dependency: libevent-2.0.so.5()(64bit) for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> processing dependency: libnfsidmap.so.0()(64bit) for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> processing dependency: libtirpc.so.1()(64bit) for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
---> package rpcbind.x86_64 0:0.2.0-42.el7 will be installed
--> running transaction check
---> package gssproxy.x86_64 0:0.7.0-4.el7 will be installed
--> processing dependency: libini_config.so.3(ini_config_1.1.0)(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
--> processing dependency: libini_config.so.3(ini_config_1.2.0)(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
--> processing dependency: libref_array.so.1(ref_array_0.1.1)(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
--> processing dependency: libverto-module-base for package: gssproxy-0.7.0-4.el7.x86_64
--> processing dependency: libbasicobjects.so.0()(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
--> processing dependency: libcollection.so.2()(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
--> processing dependency: libini_config.so.3()(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
--> processing dependency: libref_array.so.1()(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
---> package libevent.x86_64 0:2.0.21-4.el7 will be installed
---> package libnfsidmap.x86_64 0:0.25-17.el7 will be installed
---> package libtirpc.x86_64 0:0.2.4-0.10.el7 will be installed
---> package quota.x86_64 1:4.01-14.el7 will be installed
--> processing dependency: quota-nls = 1:4.01-14.el7 for package: 1:quota-4.01-14.el7.x86_64
--> processing dependency: tcp_wrappers for package: 1:quota-4.01-14.el7.x86_64
--> running transaction check
---> package libbasicobjects.x86_64 0:0.1.1-27.el7 will be installed
---> package libcollection.x86_64 0:0.6.2-27.el7 will be installed
---> package libini_config.x86_64 0:1.3.0-27.el7 will be installed
--> processing dependency: libpath_utils.so.1(path_utils_0.2.1)(64bit) for package: libini_config-1.3.0-27.el7.x86_64
--> processing dependency: libpath_utils.so.1()(64bit) for package: libini_config-1.3.0-27.el7.x86_64
---> package libref_array.x86_64 0:0.1.5-27.el7 will be installed
---> package libverto-tevent.x86_64 0:0.2.5-4.el7 will be installed
---> package quota-nls.noarch 1:4.01-14.el7 will be installed
---> package tcp_wrappers.x86_64 0:7.6-77.el7 will be installed
--> running transaction check
---> package libpath_utils.x86_64 0:0.2.1-27.el7 will be installed
--> finished dependency resolution

dependencies resolved

=================================================================================================================
 package                        arch                  version                           repository          size
=================================================================================================================
installing:
 nfs-utils                      x86_64                1:1.3.0-0.48.el7                  dvd                398 k
 rpcbind                        x86_64                0.2.0-42.el7                      dvd                 59 k
installing for dependencies:
 gssproxy                       x86_64                0.7.0-4.el7                       dvd                105 k
 libbasicobjects                x86_64                0.1.1-27.el7                      dvd                 25 k
 libcollection                  x86_64                0.6.2-27.el7                      dvd                 41 k
 libevent                       x86_64                2.0.21-4.el7                      dvd                214 k
 libini_config                  x86_64                1.3.0-27.el7                      dvd                 63 k
 libnfsidmap                    x86_64                0.25-17.el7                       dvd                 49 k
 libpath_utils                  x86_64                0.2.1-27.el7                      dvd                 27 k
 libref_array                   x86_64                0.1.5-27.el7                      dvd                 26 k
 libtirpc                       x86_64                0.2.4-0.10.el7                    dvd                 89 k
 libverto-tevent                x86_64                0.2.5-4.el7                       dvd                9.0 k
 quota                          x86_64                1:4.01-14.el7                     dvd                179 k
 quota-nls                      noarch                1:4.01-14.el7                     dvd                 91 k
 tcp_wrappers                   x86_64                7.6-77.el7                        dvd                 79 k

transaction summary
=================================================================================================================
install  2 packages (+13 dependent packages)

total download size: 1.4 m
installed size: 4.1 m
downloading packages:
-----------------------------------------------------------------------------------------------------------------
total                                                                            2.5 mb/s | 1.4 mb  00:00:00     
running transaction check
running transaction test
transaction test succeeded
running transaction
  installing : libref_array-0.1.5-27.el7.x86_64                                                             1/15 
  installing : libcollection-0.6.2-27.el7.x86_64                                                            2/15 
  installing : libbasicobjects-0.1.1-27.el7.x86_64                                                          3/15 
  installing : libtirpc-0.2.4-0.10.el7.x86_64                                                               4/15 
  installing : rpcbind-0.2.0-42.el7.x86_64                                                                  5/15 
  installing : 1:quota-nls-4.01-14.el7.noarch                                                               6/15 
  installing : libpath_utils-0.2.1-27.el7.x86_64                                                            7/15 
  installing : libini_config-1.3.0-27.el7.x86_64                                                            8/15 
  installing : tcp_wrappers-7.6-77.el7.x86_64                                                               9/15 
  installing : 1:quota-4.01-14.el7.x86_64                                                                  10/15 
  installing : libevent-2.0.21-4.el7.x86_64                                                                11/15 
  installing : libnfsidmap-0.25-17.el7.x86_64                                                              12/15 
  installing : libverto-tevent-0.2.5-4.el7.x86_64                                                          13/15 
  installing : gssproxy-0.7.0-4.el7.x86_64                                                                 14/15 
  installing : 1:nfs-utils-1.3.0-0.48.el7.x86_64                                                           15/15 
  verifying  : libverto-tevent-0.2.5-4.el7.x86_64                                                           1/15 
  verifying  : rpcbind-0.2.0-42.el7.x86_64                                                                  2/15 
  verifying  : 1:quota-4.01-14.el7.x86_64                                                                   3/15 
  verifying  : libtirpc-0.2.4-0.10.el7.x86_64                                                               4/15 
  verifying  : libnfsidmap-0.25-17.el7.x86_64                                                               5/15 
  verifying  : libini_config-1.3.0-27.el7.x86_64                                                            6/15 
  verifying  : libbasicobjects-0.1.1-27.el7.x86_64                                                          7/15 
  verifying  : libevent-2.0.21-4.el7.x86_64                                                                 8/15 
  verifying  : tcp_wrappers-7.6-77.el7.x86_64                                                               9/15 
  verifying  : libpath_utils-0.2.1-27.el7.x86_64                                                           10/15 
  verifying  : 1:quota-nls-4.01-14.el7.noarch                                                              11/15 
  verifying  : gssproxy-0.7.0-4.el7.x86_64                                                                 12/15 
  verifying  : libcollection-0.6.2-27.el7.x86_64                                                           13/15 
  verifying  : libref_array-0.1.5-27.el7.x86_64                                                            14/15 
  verifying  : 1:nfs-utils-1.3.0-0.48.el7.x86_64                                                           15/15 

installed:
  nfs-utils.x86_64 1:1.3.0-0.48.el7                         rpcbind.x86_64 0:0.2.0-42.el7                        

dependency installed:
  gssproxy.x86_64 0:0.7.0-4.el7        libbasicobjects.x86_64 0:0.1.1-27.el7 libcollection.x86_64 0:0.6.2-27.el7
  libevent.x86_64 0:2.0.21-4.el7       libini_config.x86_64 0:1.3.0-27.el7   libnfsidmap.x86_64 0:0.25-17.el7   
  libpath_utils.x86_64 0:0.2.1-27.el7  libref_array.x86_64 0:0.1.5-27.el7    libtirpc.x86_64 0:0.2.4-0.10.el7   
  libverto-tevent.x86_64 0:0.2.5-4.el7 quota.x86_64 1:4.01-14.el7            quota-nls.noarch 1:4.01-14.el7     
  tcp_wrappers.x86_64 0:7.6-77.el7    

complete!

2 启动 nfs-server

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

启动和启用nfs服务程序,由于在使用nfs服务进行文件共享之前,需要使用rpc(remote procedure call,远程过程调用),服务将nfs服务器的ip地址和端口号等信息发送给客户端.因此,在启动nfs服务之前,还需要顺带重启并启动rpcbind服务,这里将两个服务一并加入到开机启动项中.

3 可以使用 rpcbind -p 查看一下注册的信息.

[root@qdlinux ~]# rpcinfo -p
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   tcp    111  portmapper
    100000    4   udp    111  portmapper
    100000    3   udp    111  portmapper
    100000    2   udp    111  portmapper
    100024    1   udp  43946  status
    100024    1   tcp  38188  status
    100005    1   udp  20048  mountd
    100005    1   tcp  20048  mountd
    100005    2   udp  20048  mountd
    100005    2   tcp  20048  mountd
    100005    3   udp  20048  mountd
    100005    3   tcp  20048  mountd
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100227    3   tcp   2049  nfs_acl
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs
    100227    3   udp   2049  nfs_acl
    100021    1   udp  35724  nlockmgr
    100021    3   udp  35724  nlockmgr
    100021    4   udp  35724  nlockmgr
    100021    1   tcp  44097  nlockmgr
    100021    3   tcp  44097  nlockmgr
    100021    4   tcp  44097  nlockmgr

4 查看系统相关进程信息

[root@qdlinux ~]# ps -ef | egrep "rpc|nfs"
rpc        1959      1  0 16:37 ?        00:00:00 /sbin/rpcbind -w
rpcuser    2039      1  0 16:38 ?        00:00:00 /usr/sbin/rpc.statd
root       2047      2  0 16:38 ?        00:00:00 [rpciod]
root       2057      1  0 16:38 ?        00:00:00 /usr/sbin/rpc.mountd
root       2058      1  0 16:38 ?        00:00:00 /usr/sbin/rpc.idmapd
root       2068      2  0 16:38 ?        00:00:00 [nfsd4_callbacks]
root       2074      2  0 16:38 ?        00:00:00 [nfsd]
root       2075      2  0 16:38 ?        00:00:00 [nfsd]
root       2076      2  0 16:38 ?        00:00:00 [nfsd]
root       2077      2  0 16:38 ?        00:00:00 [nfsd]
root       2078      2  0 16:38 ?        00:00:00 [nfsd]
root       2079      2  0 16:38 ?        00:00:00 [nfsd]
root       2080      2  0 16:38 ?        00:00:00 [nfsd]
root       2081      2  0 16:38 ?        00:00:00 [nfsd]
root       2135   1633  0 16:52 pts/0    00:00:00 grep -e --color=auto rpc|nfs

5 nfs服务端配置共享目录

参数 作用
ro 只读
rw 读写
root_squash 当nfs客户端以root管理员访问时,映射为nfs服务器的匿名用户
no_root_squash 当nfs客户端以root管理员访问时,映射为nfs服务器的root管理员
all_squash 无论nfs客户端使用什么账户访问,均映射为nfs服务器的匿名用户
sync 同时将数据写入到内存与硬盘中,保证不丢失数据
async 优先将数据保存到内存,然后再写入到硬盘,这样效率更高,但可能会丢失数据
[root@qdlinux ~]# vim /etc/exports
/nfsfile 192.168.56.*(rw,sync,all_squash)

//创建需要共享的目录
[root@qdlinux ~]# mkdir /nfsfile

[root@qdlinux ~]# systemctl restart nfs-server.service 
[root@qdlinux ~]# cat /var/lib/nfs/etab 
/nfsfile    192.168.56.*(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,secure,root_squash,all_squash)

//查看一下id=65534是哪一个用户
[root@qdlinux ~]# grep "65534" /etc/passwd
nfsnobody:x:65534:65534:anonymous nfs user:/var/lib/nfs:/sbin/nologin

//修改一下`/nfsfile`的权限,这里也可以把目录所属主和所属组改为nfsnobody或者直接给成777权限
[root@qdlinux ~]# chmod -rf nfsnobody:nfsnobody /nfsfile/

//在目录下写入一个文件用于测试
[root@qdlinux ~]# echo "welecome to qdlinux" > /nfsfile/readme
[root@qdlinux ~]# ll  /nfsfile/
total 4
-rw-r--rwx 1 root root 20 aug 16 17:30 readme
[root@qdlinux ~]# 

nfs客户端的配置步骤十分简单,先使用 showmount 命令查询nfs服务器的远程共享信息,其输出格式为"共享的目录名称 允许使用客户端地址".

参数 作用
-e 显示nfs服务器的共享李彪
-a 显示本机挂载的文件资源的情况
-v 显示版本号
[root@qdlinux ~]# showmount -e 192.168.56.15
export list for 192.168.56.15:
/nfsfile 192.168.56.*

6 客户端测试

[root@qdlinux ~]# yum install nfs-utils rpcbind -y
[root@qdlinux ~]# systemctl start rpcbind
[root@qdlinux ~]# systemctl is-enabled rpcbind
enabled
[root@qdlinux ~]# systemctl is-active rpcbind
active

//查询一下服务是否运行
[root@qdlinux ~]# systemctl status rpcbind
● rpcbind.service - rpc bind service
   loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
   active: active (running) since thu 2018-08-16 14:05:32 cst; 3h 48min ago
 main pid: 568 (rpcbind)
    tasks: 1
   cgroup: /system.slice/rpcbind.service
           └─568 /sbin/rpcbind -w

aug 16 14:05:27 qdlinux.com systemd[1]: starting rpc bind service...
aug 16 14:05:32 qdlinux.com systemd[1]: started rpc bind service.

//在客户端使用`showmount -e`命令查询一下
[root@qdlinux ~]# showmount -e 192.168.56.15 nfs服务器共享列表
export list for 192.168.56.15:
/nfsfile 192.168.56.*

7 nfs客户端挂载

在nfs客户端创建一个挂在目录,使用mount命令加-t参数,指定要挂在的文件系统的类型,并在命令后面写上服务器的ip地址、服务器上的共享目录以及要挂载到本地系统(客户端)的目录.

[root@qdlinux /]# mkdir /data
[root@qdlinux /]# mount -t nfs 192.168.56.15:/nfsfile /data

//现在已经挂载成功了,可以看到在nfs服务器上创建的文件了.

如果希望nfs文件共享服务能一直有效,需要将其写入到 fstab 文件中.

[root@qdlinux data]# vim /etc/fstab
192.168.56.15:/nfsfile                    /data                   nfs     defaults        0 0
1 如果nfs服务器宕机,强制卸载umount -lf /data
2 [root@qdlinux data]# umount /data
  umount.nfs4: /data: device is busy
  出现这种情况时,退出挂载目录然后在卸载

8 autofs自动挂载服务

autofs服务程序是一种linux系统守护进程,当检测到用户试图去访问一个尚未挂载的文件系统时,将自动挂载该文件系统,我们将挂载信息写入/etc/fstab文件后,系统在每次开机时都自动将其挂载,而autofs自动挂载服务是用户需要使用该文件系统时才会去动态挂载,从而节约了网络资源和服务器的硬件资源.

[root@qdlinux /]# yum install autofs -y

直接挂载方式,本地的挂载点是绝对路径

[root@qdlinux /]# vim /etc/auto.master.d/nfs.autofs
/-      /etc/auto.nfs_direct
[root@qdlinux /]# vim /etc/auto.nfs_direct 
/data   -rw,sync,soft   192.168.56.15:/nfsfile

//mount查看一下
/etc/auto.nfs_direct on /data type autofs (rw,relatime,fd=18,pgrp=6217,timeout=300,minproto=5,maxproto=5,direct,pipe_ino=79495)

间接挂载方式

[root@qdlinux ~]# mkdir -p /public/share
[root@qdlinux ~]# chown -rf nfsnobody:nfsnobody /public/share
[root@qdlinux ~]# vim /etc/exports
/public/share 192.168.56.*(rw,sync,all_squash)
//重新启动nfs-server

[root@qdlinux ~]# showmount -e 192.168.56.15
export list for 192.168.56.15:
/public/share 192.168.56.*
/nfsfile      192.168.56.*

[root@qdlinux /]# mkdir /test
[root@qdlinux /]# vim /etc/auto.master.d/share.autofs
/test  /etc/share.nfs_share
[root@qdlinux /]# vim /etc/share.nfs_share
*       -rw,sync,soft   192.168.56.15:/public/&
//重新启动autofs服务

现在可以访问了,客户端当你切换到/test/share时可以创建文件服务端实时同步.

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网