NFS介绍、NFS服务端安装配置、NFS配置选项

2018-12-08  本文已影响0人  XiaoMing丶

目录

一、NFS介绍
二、NFS服务端安装配置
三、NFS配置选项

一、NFS介绍

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

二、NFS服务端安装配置

//在安装nfs-utills时会自一并安装rpcbind
[root@minglinux-01 ~] yum install -y nfs-utils rpcbind 
[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权限
[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

[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

[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。

[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这个网段。
[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

上一篇 下一篇

猜你喜欢

热点阅读