NFS详解

独自空忆成欢 提交于 2019-12-05 01:43:44

 

 

 

服务端操作
[root@#quan#Better ~]$rpm -qa | grep nfs  #查看是否安装了nfs
nfs-utils-1.2.3-78.el6_10.1.x86_64
nfs-utils-lib-1.1.5-13.el6.x86_64
nfs4-acl-tools-0.3.3-8.el6.x86_64
nfs-utils-lib-devel-1.1.5-13.el6.x86_64
[root@#quan#Better ~]$rpm -qa | grep rpcbind  #查看是否安装了nfs
rpcbind-0.2.0-13.el6.x86_64
[root@#quan#Better ~]$yum -y install nfs-utils rpcbind   #安装nfs
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: centos.ustc.edu.cn
 * updates: mirrors.aliyun.com
base                                                                            | 3.7 kB     00:00     
extras                                                                          | 3.4 kB     00:00     
updates                                                                         | 3.4 kB     00:00     
Package 1:nfs-utils-1.2.3-78.el6_10.1.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package rpcbind.x86_64 0:0.2.0-13.el6 will be updated
---> Package rpcbind.x86_64 0:0.2.0-16.el6 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================
 Package                Arch                  Version                        Repository           Size
=======================================================================================================
Updating:
 rpcbind                x86_64                0.2.0-16.el6                   base                 51 k

Transaction Summary
=======================================================================================================
Upgrade       1 Package(s)

Total download size: 51 k
Downloading Packages:
rpcbind-0.2.0-16.el6.x86_64.rpm                                                 |  51 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Updating   : rpcbind-0.2.0-16.el6.x86_64                                                         1/2 
  Cleanup    : rpcbind-0.2.0-13.el6.x86_64                                                         2/2 
  Verifying  : rpcbind-0.2.0-16.el6.x86_64                                                         1/2 
  Verifying  : rpcbind-0.2.0-13.el6.x86_64                                                         2/2 

Updated:
  rpcbind.x86_64 0:0.2.0-16.el6                                                                        

Complete!
[root@#quan#Better ~]$mkdir /quan   #建立共享目录
[root@#quan#Better ~]$ll -d /quan
drwxr-xr-x 2 root root 4096 Sep 17 16:30 /quan
[root@#quan#Better ~]$chmod 666 /quan   #修改共享目录的权限
[root@#quan#Better ~]$ll -d /quan
drw-rw-rw- 2 root root 4096 Sep 17 16:30 /quan
[root@#quan#Better ~]$vim /etc/exports    #修改nfs 的配置文件

[root@#quan#Better quan]$cat /etc/exports
/quan *(rw,no_root_squash,no_all_squash,sync)

[root@#quan#Better ~]$export
export    exportfs  
[root@#quan#Better ~]$exportfs -r    #平滑生效
[root@#quan#Better ~]$service nfs start  #启动nfs
[root@#quan#Better ~]$service rpcbind start    #启动rpcbind
[root@#quan#Better ~]$rpcinfo -p localhost   #查看 RPC 服务的注册状况
   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  35929  status
    100024    1   tcp  56297  status
    100011    1   udp    875  rquotad
    100011    2   udp    875  rquotad
    100011    1   tcp    875  rquotad
    100011    2   tcp    875  rquotad
    100005    1   udp  41810  mountd
    100005    1   tcp  47423  mountd
    100005    2   udp  33792  mountd
    100005    2   tcp  57060  mountd
    100005    3   udp  33529  mountd
    100005    3   tcp  34835  mountd
    100003    2   tcp   2049  nfs
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100227    2   tcp   2049  nfs_acl
    100227    3   tcp   2049  nfs_acl
    100003    2   udp   2049  nfs
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs
    100227    2   udp   2049  nfs_acl
    100227    3   udp   2049  nfs_acl
    100021    1   udp  52934  nlockmgr
    100021    3   udp  52934  nlockmgr
    100021    4   udp  52934  nlockmgr
    100021    1   tcp  45172  nlockmgr
    100021    3   tcp  45172  nlockmgr
    100021    4   tcp  45172  nlockmgr
选项与参数:
-p :针对某 IP (未写则预设为本机) 显示出所有的 port 与 porgram 的信息;
-t :针对某主机的某支程序检查其 TCP 封包所在的软件版本;
-u :针对某主机的某支程序检查其 UDP 封包所在的软件版本;
#server 端先自我测试一下是否可以联机,利用 showmount 这个指令来查阅! [root@#quan#Better ~]$showmount -e localhost Export list for localhost: /quan * 
-a :显示目前主机与客户端的 NFS 联机分享的状态;
-e :显示某部主机的 /etc/exports 所分享的目录数据。
[root@#quan#Better ~]$cd /quan/ [root@#quan#Better quan]$ls [root@#quan#Better quan]$echo "quan >" test quan > test [root@#quan#Better quan]$echo "quan " >> test [root@#quan#Better quan]$ls test

 

[root@#quan#Better quan]$cat /etc/exports 
/quan *(rw,no_root_squash,no_all_squash,sync)

NFS配置文件权限参数说明(/etc/exports)


1、rw:表示可读写权限。

2、ro:表示只读权限。

3、sync:请求或写入数据时,数据同步写入到NFS Server的硬盘后才返回。(优点:数据安全不会丢。缺点:性能比启用该参数要差)。

4、async:写入时数据会先写到内存缓冲区,只到硬盘有空档才会再写入磁盘,这样可以提升写入效率!风险:若服务器宕机或不正常关机,会损失

      缓冲区中未写入磁盘的数据(解决办法:服务器主板电池或加UPS不间断电源)。

5、no_root_squash:访问NFS Server共享目录的用户如果是root的话,它对该共享目录具有root权限,这个配置原本是为无盘客户端准备的。用户应避免使用。

6、root_squash:如果访问NFS Server共享目录的用户是root,则它的权限将压缩成匿名用户,同时它的UID和GID通常会变成nfsnobody账号身份。

7、all_squash:不管访问NFS Server共享目录的用户身份如何,它的权限都将被压缩成匿名用户,同时它的UID和GID都会变成nfsnobody账号身份。在

        早期多个NFS客户端同时读写NFS Server数据时,这个参数很有用。

在生产中配置NFS的重要技巧:

1、确保所有客户端服务器对NFS共享目录具备相同的用户访问权限

  a、all_squash把所有客户端都压缩成固定的匿名用户(UID相同)。

  b、就是anonuid,anongid指定的UID和GID的用户。

2、所有的客户端和服务端都需要有一个相同的GID和UID用户,即nfsnobody(UID必须相同)。

anonuid:参数以anon*开头即指anonymous匿名用户,这个用户的UID设置值通常为nfsnobody的UID值,当然也可以自行设置这个UID值。

但是UID必须存在于/etc/passwd中。在多NFS Clients时,如果Web Server共享一个NFS目录,通过这个参数可以使得不同的NFS

Clients写入的数据对所有NFS Clients保持统一的用户权限,即为配置的匿名UID对应的用户权限,这个参数很有用,一般默认即可。

anongid:同anonuid,区别就是把uid(用户id)换成gid(组id)。

 

 

 

#客户端设置

[root@MiWiFi-R1CM-srv ~]# yum -y install nfs-utils   #安装客户端
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: ap.stykers.moe
 * extras: mirrors.aliyun.com
 * updates: ap.stykers.moe
base                                                                            | 3.7 kB     00:00     
extras                                                                          | 3.4 kB     00:00     
updates                                                                         | 3.4 kB     00:00     
Package 1:nfs-utils-1.2.3-78.el6_10.1.x86_64 already installed and latest version
Nothing to do
[root@MiWiFi-R1CM-srv ~]# mkdir /lys  #建立挂载目录
[root@MiWiFi-R1CM-srv ~]# showmount -e 192.168.31.66  #查看服务器抛出的共享目录信息
Export list for 192.168.31.66:
/quan *
[root@MiWiFi-R1CM-srv ~]# mount -t nfs 192.168.31.66:/quan /lys -o proto=tcp -o nolock   #挂载目录
[root@MiWiFi-R1CM-srv ~]# df -h   #查看挂载信息
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda5             21G  3.9G   16G  20% /
tmpfs                491M   80K  491M   1% /dev/shm
/dev/sda1            2.0G   36M  1.8G   2% /boot
/dev/sda2            4.8G   10M  4.6G   1% /data_for_quan
192.168.31.66:/quan   21G  6.2G   14G  32% /lys
[root@MiWiFi-R1CM-srv ~]# ls
anaconda-ks.cfg  Downloads           keepalived-1.2.1         nginx-1.12.0         Public
Desktop          install.log         keepalived-1.2.1.tar.gz  nginx-1.12.0.tar.gz  Templates
Documents        install.log.syslog  Music                    Pictures             Videos
[root@MiWiFi-R1CM-srv ~]# cd /l
lib/        lib64/      lost+found/ lys/        
[root@MiWiFi-R1CM-srv ~]# cd /lys/
[root@MiWiFi-R1CM-srv lys]# ls
test
[root@MiWiFi-R1CM-srv lys]# cat test 
quan 
[root@MiWiFi-R1CM-srv lys]# umount /l
lib/        lib64/      lost+found/ lys/        
[root@MiWiFi-R1CM-srv lys]# umount /lys/  #卸载挂载目录
umount.nfs: /lys: device is busy
umount.nfs: /lys: device is busy  #一定将现在位置移除挂载目录才能卸载成功
[root@MiWiFi-R1CM-srv lys]# cd ..
[root@MiWiFi-R1CM-srv /]# umount /lys/
[root@MiWiFi-R1CM-srv /]# df -h 
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda5        21G  3.9G   16G  20% /
tmpfs           491M   80K  491M   1% /dev/shm
/dev/sda1       2.0G   36M  1.8G   2% /boot
/dev/sda2       4.8G   10M  4.6G   1% /data_for_quan
[root@MiWiFi-R1CM-srv /]# 

 

为了方便配置防火墙,需要固定nfs服务端口

NFS启动时会随机启动多个端口并向RPC注册,这样如果使用iptables对NFS端口进行限制就会有点麻烦,可以更改配置文件固定NFS服务相关端口。

[root@#quan#Better quan]$vim /etc/sysconfig/nfs

RQUOTAD_PORT=30001
LOCKD_TCPPORT=30002
LOCKD_UDPPORT=30002
MOUNTD_PORT=30003
STATD_PORT=30004 重启服务即可,这样随机端口就改为固定端口了

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!