NFS介绍、NFS服务端安装配置、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的服务端和客户端本身无法直接通信,需要借助RPC来完成。

  • NFS应用场景是:A,B,C三台机器上需要保证被访问到的文件是一样的,A共享数据出来,B和C分别去挂载A共享的数据目录,从而B和C访问到的数据和A上的一致。可以通过NFS实现服务器间实时的数据共享。

  • NFS原理图


    NFS原理
  • NFS服务端启动NFS服务,NFS服务作为服务在系统中启动,但不监听任何端口,监听端口完成TCP/IP通信的工作由RPCbind服务产生的RPC协议实现。服务端借助RPC协议为客户端提供服务,该协议由RPC服务实现。RPC服务在Centos5及之前被称为portmap,6及以后的版本被称为RPCbind。

  • RPC服务监听111端口,当通信时,

  1. 先由客户端和服务端的RPC协议相互通信,
  2. NFS服务端会在RPC协议中注册一个端口,
  3. 服务端的NFS服务告诉本地RPC自己通信的端口,
  4. 服务端RPC服务告诉客户端的RPC服务该通信端口
  5. 最后NFS客户端主机与服务端NFS的端口通信实现数据传输。

二、NFS服务端安装配置

  • NFS服务需要安装两个包(nfs-utils和rpcbind)
//在安装nfs-utills时会自一并安装rpcbind
[root@minglinux-01 ~] yum install -y nfs-utils rpcbind 
  • 客户端也需要安装nfs-utills
[root@minglinux-02 ~] yum install -y nfs-utils
  • 先修改配置文件(默认该文件为空)
[root@minglinux-01 ~] ls /etc/exports
/etc/exports
[root@minglinux-01 ~] cat !$
cat /etc/exports
[root@minglinux-01 ~] vim !$
vim /etc/exports
//写入以下内容
  1 /home/nfstestdir 192.168.162.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

这个配置文件就一行,共分为三部分。第一部分是本地要共享出去的目录,第二部分是允许访问的主机(可以是一个IP,也可以是一个IP段),第三部分就是小括号里面的一些权限选项。此处配置的含义为:共享的目
录为/home/nfstestdir,信任的主机为192.168.162.0/24这个网段,权限为读/写,同步模式,限定所有使用者,并且限定的uid和gid都为1000。

  • 创建共享目录并修改其权限
[root@minglinux-01 ~] mkdir /home/nfstestdir
[root@minglinux-01 ~] chmod 777 /home/nfstestdir 
//为了避免由于权限导致的报错,将共享目录改为777权限
  • 启动rpcbind
[root@minglinux-01 ~] systemctl start rpcbind.service 
[root@minglinux-01 ~] netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      758/sshd            
tcp6       0      0 :::3306                 :::*                    LISTEN      1235/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*     

[root@minglinux-01 ~] ps aux |grep rpcbind
rpc        2132  0.0  0.0  69220  1204 ?        Ss   22:53   0:00 /sbin/rpcbind -w
root       2136  0.0  0.0 112720   984 pts/1    S+   22:55   0:00 grep --color=auto rpcbind

  • 启动NFS服务
[root@minglinux-01 ~] systemctl start nfs
[root@minglinux-01 ~] ps aux| grep nfs
root       1518  0.0  0.0      0     0 ?        S<   23:07   0:00 nfsd4_callbacks]
root       1524  0.0  0.0      0     0 ?        S    23:07   0:00 nfsd]
root       1525  0.0  0.0      0     0 ?        S    23:07   0:00 nfsd]
root       1526  0.0  0.0      0     0 ?        S    23:07   0:00 nfsd]
root       1527  0.0  0.0      0     0 ?        S    23:07   0:00 nfsd]
root       1528  0.0  0.0      0     0 ?        S    23:07   0:00 nfsd]
root       1529  0.0  0.0      0     0 ?        S    23:07   0:00 nfsd]
root       1530  0.0  0.0      0     0 ?        S    23:07   0:00 nfsd]
root       1531  0.0  0.0      0     0 ?        S    23:07   0:00 nfsd]
root       1547  0.0  0.0 112720   984 pts/0    S+   23:07   0:00 grep --color=auto nfs

[root@minglinux-01 ~] ps aux| grep rpc  //启动nfs服务时会自动启动rpc相关的服务,若rpc相关服务未启动则无法正常使用nfs
root        551  0.0  0.0      0     0 ?        S<   23:03   0:00 rpciod]
rpc         585  0.0  0.0  69220  1528 ?        Ss   23:03   0:00 /sbin/rpcbind -w
rpcuser    1504  0.0  0.0  42420  1756 ?        Ss   23:07   0:00 /usr/sbin/rpc.statd
root       1512  0.0  0.0  42608   944 ?        Ss   23:07   0:00 /usr/sbin/rpc.mountd
root       1513  0.0  0.0  45924   548 ?        Ss   23:07   0:00 /usr/sbin/rpc.idmapd
root       1550  0.0  0.0 112724   980 pts/0    S+   23:08   0:00 grep --color=auto rpc

[root@minglinux-01 ~] systemctl enable nfs  //设置nfs服务开机启动
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

客户端与服务端均需要启动rpcbind,但仅服务端需要启动nfs

  • 客户端启动rpcbind
[root@minglinux-02 ~] ps aux |grep rpcbind
root       1871  0.0  0.0 112720   984 pts/0    S+   07:13   0:00 grep --color=auto rpcbind
[root@minglinux-02 ~] systemctl start rpcbind
[root@minglinux-02 ~] ps aux |grep rpcbind
rpc        1879  1.5  0.0  69220  1208 ?        Ss   07:14   0:00 /sbin/rpcbind -w
root       1881  0.0  0.0 112720   984 pts/0    S+   07:14   0:00 grep --color=auto rpcbind
[root@minglinux-02 ~] netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1392/master         
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      887/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1392/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1400/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      887/sshd  

三、NFS配置选项

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

上节提到的配置文件中小括号里面的内容是权限选项,下面简单介绍NFS的一些相关权限选项:

rw:表示读/写。
ro:表示只读。
sync:同步模式,表示内存中的数据实时写入磁盘。
async:非同步模式,表示把内存中的数据定期写入磁盘。
no_root_squash:加上这个选项后,root用户就会对共享的目录拥有至高的权限控制,就像是对本机的目录操作一样。但这样安全性降低。
root_squash:与no_root_squash选项对应,表示root用户对共享目录的权限不高,只有普通用户的权限,即限制了root。
all_squash:表示不管使用NFS的用户是谁,其身份都会被限定为一个指定的普通用户身份。
anonuid/anongid:要和root_squash以及all_squash选项一同使用,用于指定使用NFS的用户被限定后的uid和gid,但前提是本机的/etc/passwd中存在相应的uid和gid。

  • 客户端查看NFS的共享情况
[root@minglinux-02 ~] showmount -e 192.168.162.130  //192.168.162.130为服务端虚拟机IP
Export list for 192.168.162.130:
/home/nfstestdir 192.168.162.0/24
//在nfs服务工作时,rpcbind通过111端口通信,但nfs服务使用的是随机端口。
//防火墙和selinux可能限制端口通信,所以最好关闭selinux和防火墙(服务端和客户端均需要关闭)
上例我们可以看到192.168.162.130的共享目录为/home/nfstestdir,
信任主机为192.168.168.0/24这个网段。
  • 客户端挂载NFS共享目录
[root@minglinux-02 ~] mount -t nfs 192.168.162.130:/home/nfstestdir /mnt/
[root@minglinux-02 ~] df -h
文件系统                          容量  已用  可用 已用% 挂载点
/dev/sda3                          28G  3.1G   25G   11% /
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  140M   58M   71% /boot
tmpfs                             183M     0  183M    0% /run/user/0
192.168.162.130:/home/nfstestdir   28G  6.6G   22G   24% /mnt
  • 客户端在共享目录下创建测试文件
[root@minglinux-02 ~] cd /mnt/
[root@minglinux-02 /mnt] touch ming.txt
[root@minglinux-02 /mnt] ll
总用量 0
-rw-r--r--. 1 mysql mysql 0 12月  8 23:43 ming.txt
[root@minglinux-02 /mnt] id mysql
uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)
  • 服务端查看共享目录
[root@minglinux-01 ~] ll /home/nfstestdir
总用量 0
-rw-r--r-- 1 1000 1000 0 12月  8 23:43 ming.txt  //可以看到文件的属主和属组都为1000

你可能感兴趣的:(NFS介绍、NFS服务端安装配置、NFS配置选项)