码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-11-01 23:53:59      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:14.1 nfs介绍 14.2 nfs服务端安装配置 14.3 nfs配置选项

14.1 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上的一致

  • 例如: 一个站点,上面传输了很多图片,用户访问一个图片时,需要从A机器调用,但A机器负载高,所以就弄多两台B机器C机器同时提供服务;正常的话,需要到A机器上才能拿到数据,但是B机器和C机器做了负载均衡,分担了相同的服务器,那么用户也有可能到B机器或者C机器上;那么用户请求到B机器上的时候,如何才能获取到A机器上的数据呢;要么把A机器的数据传输到B机器上,但是这个不能时时更新,(用户上传的数据是存放在A机器上,但用户请求的时候数据是请求到B机器上)这样就会导致用户请求获取的数据访问不到;那么NFS服务就可以解决这个问题,将A机器的数据共享到B机器、C机器。有NFS服务以后,上传到A机器上的数据,B机器或C机器上就能马上看到和调用

  • 技术分享

  • NFS原理图: 服务端需要启动一个NFS服务,服务端要想给客户端提供服务,需要借助RPC协议,RPC协议是由rpcbind服务实现;在centos 5或者之前的版本叫portmap服务;之后的版本叫rpcbind服务;NFS服务默认不会监听任何端口;最终实现NFS服务,需要借助rpcbind服务产生的RPC协议,RPC协议默认监听的端口是111; 整个流程为:服务端的NFS服务监听一个端口通过RPC协议监听的端口,告诉客户端RPC协议,然后客户端通过本机的RPC端口回传数据信息到服务端NFS监听的端口,最终实现通信

  • 技术分享

14.2 NFS服务端安装配置

  • 步骤如下

  1. yum install -y nfs-utils rpcbind

  2. vim /etc/exports //加入如下内容

  3. /home/nfstestdir 192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

  4. 保存配置文件后,执行如下准备操作

  5. mkdir /home/nfstestdir

  6. chmod 777 /home/nfstestdir

  7. systemctl start rpcbind

  8. systemctl start nfs

  9. systemctl enable rpcbind

  10. systemctl enable nfs

  • 首先要准备俩台机器,一台服务端,一台客户端

  • 技术分享

  • 在客户端 192.168.202.132 安装 nfs-utils yum install -y nfs-utils

  • 在服务端 192.168.202.131 上安装 nfs-utils ,rpcbind yum install -y nfs-utils rpcbind

  • 首先服务端上安装nfs-utils ,rpcbind,客户端安装 nfs-utils

```
[root@localhost ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror


已安装:
  nfs-utils.x86_64 1:1.3.0-0.48.el7_4                                         rpcbind.x86_64 0:0.2.0-42.el7                                        

作为依赖被安装:
  gssproxy.x86_64 0:0.7.0-4.el7      keyutils.x86_64 0:1.5.8-3.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-libevent.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   

完毕!
[root@localhost ~]# 

刚刚改了下主机名 hostname
[root@aming-01 ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * epel: ftp.riken.jp
 * extras: mirrors.163.com
 * updates: mirrors.163.com
软件包 1:nfs-utils-1.3.0-0.48.el7_4.x86_64 已安装并且是最新版本
软件包 rpcbind-0.2.0-42.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@aming-01 ~]# 

```
- 客户端安装
```
[root@aming-02 ~]# yum install -y nfs-utils
已安装:
  nfs-utils.x86_64 1:1.3.0-0.48.el7_4                                                                                       

作为依赖被安装:
  gssproxy.x86_64 0:0.7.0-4.el7          keyutils.x86_64 0:1.5.8-3.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-libevent.x86_64 0:0.2.5-4.el7    quota.x86_64 1:4.01-14.el7              
  quota-nls.noarch 1:4.01-14.el7         rpcbind.x86_64 0:0.2.0-42.el7             tcp_wrappers.x86_64 0:7.6-77.el7        

完毕!
[root@aming-02 ~]# 
```
- 安装完了之后在服务端 编辑 vim /etc/exports
- 创建一个/home/nfstestdir 目录 把它的权限改为777
```
[root@aming-01 ~]# vim /etc/exports

/home/nfstestdir 192.168.202.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
~                                                                                                                           
                          
                                                                                                                          
:wq 

[root@aming-01 ~]# vim /etc/exports
[root@aming-01 ~]# mkdir /home/nfstestdir
[root@aming-01 ~]# chmod 777 /home/nfstestdir
[root@aming-01 ~]# 

```
- 在启动rpcbind之前 来看下 现在启动的服务
- 发现rpnbind安装完之后就自动帮你起来
```
[root@aming-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:80              0.0.0.0:*               LISTEN      954/nginx: master p 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      953/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1774/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      954/nginx: master p 
tcp6       0      0 :::3306                 :::*                    LISTEN      1480/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      953/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1774/master         
[root@aming-01 ~]# 
```
- 看看客户端的开启的服务
```
[root@aming-02 ~]# 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      832/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1385/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1161/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      832/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1385/master         
[root@aming-02 ~]# 

[root@aming-02 ~]# ps aux |grep rpc
rpc        2426  0.0  0.1  64964  1052 ?        Ss   22:16   0:00 /sbin/rpcbind -w
root       2547  0.0  0.0 112680   980 pts/0    S+   22:29   0:00 grep --color=auto rpc
[root@aming-02 ~]# 

```
- 既然已经启动了rpcbind 就开始启动nfs
- 实际上在启动nfs的时候 它会自动帮你启动rpc
```
[root@aming-01 ~]# systemctl start nfs
[root@aming-01 ~]# ps aux | grep nfs
root       2457  0.0  0.0      0     0 ?        S<   22:31   0:00 [nfsd4_callbacks]
root       2463  0.0  0.0      0     0 ?        S    22:31   0:00 [nfsd]
root       2464  0.0  0.0      0     0 ?        S    22:31   0:00 [nfsd]
root       2465  0.0  0.0      0     0 ?        S    22:31   0:00 [nfsd]
root       2466  0.0  0.0      0     0 ?        S    22:31   0:00 [nfsd]
root       2467  0.0  0.0      0     0 ?        S    22:31   0:00 [nfsd]
root       2468  0.0  0.0      0     0 ?        S    22:31   0:00 [nfsd]
root       2469  0.0  0.0      0     0 ?        S    22:31   0:00 [nfsd]
root       2470  0.0  0.0      0     0 ?        S    22:31   0:00 [nfsd]
root       2474  0.0  0.0 112680   980 pts/0    R+   22:31   0:00 grep --color=auto nfs
[root@aming-01 ~]# 
[root@aming-01 ~]# ps aux | grep rpc
root        376  0.0  0.0      0     0 ?        S<   22:02   0:00 [rpciod]
rpc        2431  0.0  0.1  64964  1432 ?        Ss   22:30   0:00 /sbin/rpcbind -w
rpcuser    2445  0.0  0.1  42380  1752 ?        Ss   22:31   0:00 /usr/sbin/rpc.statd
root       2446  0.0  0.0  19324   652 ?        Ss   22:31   0:00 /usr/sbin/rpc.idmapd
root       2447  0.0  0.0  42564   948 ?        Ss   22:31   0:00 /usr/sbin/rpc.mountd
root       2476  0.0  0.0 112680   980 pts/0    R+   22:31   0:00 grep --color=auto rpc
[root@aming-01 ~]# 

```
- 如果想要开机就启动,还需要执行这个命令systemctl enable nfs  systemctl enable rpcbind
```
[root@aming-01 ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@aming-01 ~]# 


[root@aming-01 ~]# cat /etc/exports
/home/nfstestdir 192.168.202.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
[root@aming-01 ~]# 

```

14.3 NFS配置选项

  • rw 读写

  • ro 只读

  • sync 同步模式,内存数据实时写入磁盘

  • async 非同步模式

  • no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大

  • root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户

  • all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户

  • anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid

  • 客户端挂载

  • yum install -y nfs-utils

  • showmount -e 192.168.133.130 //该ip为NFS服务端ip

  • mount -t nfs 192.168.133.130:/home/nfstestdir /mnt

  • df -h

  • touch /mnt/aminglinux.txt

  • ls -l /mnt/aminglinux.txt //可以看到文件的属主和属组都为1000

  • 先去客户端,shwomount -e 192.168.202.131 该ip为NFS服务端ip

```
[root@aming-02 ~]# show
showconsolefont  showkey          showmount        
[root@aming-02 ~]# showmount -e 192.168.202.131
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
[root@aming-02 ~]# 
```
- 报错了,看下防火墙是否开启,报错找不到指定的主机
之前已经确认过了111端口已经被监听,但是还是访问不了;那就可能是防火墙导致
关闭防火墙,
-  先去服务端把防火墙关掉,还有selinux
```
[root@aming-01 ~]# systemctl stop firewalld
[root@aming-01 ~]# setenforce 0
[root@aming-01 ~]# getenforce
Permissive
[root@aming-01 ~]# 
```
-  在把客户端防火墙,selinux 关掉
-  因为NFS服务比较特殊,虽然rpc可以通信,就算iptables把111端口放行但NFS也不一定能正常通信;因为他们用了一个不固定的端口
```
[root@aming-02 ~]# systemctl stop firewalld
[root@aming-02 ~]# getenforce
Enforcing
[root@aming-02 ~]# setenforce 0
[root@aming-02 ~]# getenforce
Permissive
[root@aming-02 ~]# showmount -e 192.168.202.131
Export list for 192.168.202.131:
/home/nfstestdir 192.168.202.0/24
[root@aming-02 ~]# 
```
- 现在就可以去挂载了
```
[root@aming-02 ~]# mount -t nfs 192.168.202.131:/home/nfstestdir /mnt/
[root@aming-02 ~]# df -h
文件系统                          容量  已用  可用 已用% 挂载点
/dev/sda3                          28G  4.2G   24G   15% /
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.202.131:/home/nfstestdir   28G  4.4G   24G   16% /mnt
[root@aming-02 ~]# 
```
- 这就是远程服务端nfs共享的目录
```
[root@aming-02 ~]# cd /mnt/
[root@aming-02 mnt]# ls
[root@aming-02 mnt]# touch aminglinux.111
[root@aming-02 mnt]# ls -l
总用量 0
-rw-r--r--. 1 mysql mysql 0 10月 31 23:16 aminglinux.111
[root@aming-02 mnt]# 
```
  • 先来看下服务端这边有没有更新

[root@aming-01 ~]# ls -l /home/nfstestdir/总用量 0
-rw-r--r--. 1 mysql mysql 0 10月 31 23:16 aminglinux.111
[root@aming-01 ~]#
  • 原因是配置 /etc/export 文件时,配置了anonuid=1000,anongid=1000 客户端显示 1000 用户 是因为客户端机器上并没有1000权限的用户 同样,服务端显示mysql,是因为服务端上的机器mysql 就是1000权限的用户


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

标签:14.1 nfs介绍 14.2 nfs服务端安装配置 14.3 nfs配置选项

原文地址:http://ch71smas.blog.51cto.com/13090095/1978210

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!