zoukankan      html  css  js  c++  java
  • nfs 是Network File System 网络文件系统

    NFS的基本原刚是容许不同的客户端及服务通过一组PRC分享相同的文件系统,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享。NFS在文件传送过程中依赖于RPC协议。远程过程调用Remote Procedure Call是能使客户端执行其他系统中程序的一种机制。NFS本身是没有提供信息传输的协议和功能的,但NFS却能让我们通过网络时行资料分享,这是因为NFS使用了一些其它的传输协议。而这些传输协议用到这个RPC功能。可以说NFS本身就是便用RPC的一个程序。或者说NFS也是一个RPC SERVER。所以只要用到NFS的地方都要启动RPC服务,不论是NFS SERVER或者NFS CLIENT。这样SERVER和CLIENT才能通过RPC来实现 PROGRAM PORT的对应。可以这么理解RPC和NFS的关系:NFS是一个文件系统,而RPC是负责信息的传输。

    因为NFS支持的功能相当多,而不同的功能都会使用不同的程序来启动。每启动一个功能就会启用一些端口来传输数据,因此NFS的功能所对应的端口才没有固定,而是采用随机取用一些未被使用的小于724的端口来作为传输之用。

    RPC最主要的功能就是指定每个NFS功能所对应的端口号,并且回报给客户端,让客户端可以连接到正确的端口上。当服务器在启动NFS时会随机选用数个端 口,并主动地向RPC注册。因此RPC可以知道每个端口对应的NFS功能。然后RPC固定使用端口111来监听客户端的请求并回报客户端正确的端口,所以 可以让NFS的启动更为容易。注意,启动NFS之前,要先启动RPC;否则NFS会无法向RPC注册。另外,重新启动RPC时原本注册的数据会不见,因此 RPC重新启动后它管理的所有程序都需要重新启动以重新向RPC注册。

    当客户端有NFS文件要存取请求时,它如何向服务器端要求数据?
    (1)客户端会向服务器端的RPC(port 111)发出NFS文件存取功能的询问请求。
    (2)服务器端找到对应的已注册的NFS daemon端口后会回报给客户端。
    (3)客户端了解正确的端口后,就可以直接与NFS守护进程来联机。

    服务端:(默认已经安装了,只是没启动服务)

    [root@cs /]# chkconfig|grep nfs
    nfs             0:off   1:off   2:off   3:off   4:off   5:off   6:off
    nfs-rdma        0:off   1:off   2:off   3:off   4:off   5:off   6:off
    nfslock         0:off   1:off   2:off   3:on    4:on    5:on    6:off
    [root@cs /]# chkconfig --level 35 nfs on
    [root@cs /]# chkconfig |grep nfs
    nfs             0:off   1:off   2:off   3:on    4:off   5:on    6:off
    nfs-rdma        0:off   1:off   2:off   3:off   4:off   5:off   6:off
    nfslock         0:off   1:off   2:off   3:on    4:on    5:on    6:off
    yum install -y rpcbind nfs-utils
    # /etc/init.d/nfs start
    Starting NFS services:                                     [  OK  ]
    Starting NFS quotas:                                       [  OK  ]
    Starting NFS mountd:                                       [  OK  ]
    Starting NFS daemon:                                       [  OK  ]
    Starting RPC idmapd:                                       [  OK  ]
    # cat /etc/exports 
    /data *(rw)

    上面框内容是要分享的目录

    启动服务之前,一定要先启动rpcbind

    [root@db73 ~]# /etc/init.d/rpcbind status
    rpcbind is stopped
    [root@db73 ~]# /etc/init.d/rpcbind start
    Starting rpcbind: [  OK  ]
    [root@db73 ~]# netstat -lnutp
    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:22                  0.0.0.0:*                   LISTEN      909/sshd            
    tcp        0      0 0.0.0.0:445                 0.0.0.0:*                   LISTEN      1875/smbd           
    tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      4081/mysqld         
    tcp        0      0 0.0.0.0:139                 0.0.0.0:*                   LISTEN      1875/smbd           
    tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      14806/rpcbind       
    udp        0      0 0.0.0.0:985                 0.0.0.0:*                               14806/rpcbind       
    udp        0      0 0.0.0.0:111                 0.0.0.0:*                               14806/rpcbind       
    udp        0      0 10.100.0.255:137            0.0.0.0:*                               21620/nmbd          
    udp        0      0 10.100.0.250:137            0.0.0.0:*                               21620/nmbd          
    udp        0      0 0.0.0.0:137                 0.0.0.0:*                               21620/nmbd          
    udp        0      0 10.100.0.255:138            0.0.0.0:*                               21620/nmbd          
    udp        0      0 10.100.0.250:138            0.0.0.0:*                               21620/nmbd          
    udp        0      0 0.0.0.0:138                 0.0.0.0:*                               21620/nmbd 

    查看NFS服务状态:

    # /etc/init.d/nfs status
    rpc.svcgssd is stopped
    rpc.mountd (pid 29497) is running...
    nfsd (pid 29513 29512 29511 29510 29509 29508 29507 29506) is running...
    rpc.rquotad (pid 29492) is running...
    [root@NB Downloads]# /etc/init.d/rpcbind status
    rpcbind (pid  2608) is running...

    设置NFS服务在系统运行级别2和5自动启动 

    
    

    [root@db73 ~]# /etc/init.d/rpcbind status
    rpcbind is stopped
    [root@db73 ~]# /etc/init.d/rpcbind start
    Starting rpcbind: [ OK ]
    [root@db73 ~]# netstat -lnutp
    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:22 0.0.0.0:* LISTEN 909/sshd
    tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 1875/smbd
    tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4081/mysqld
    tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 1875/smbd
    tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 14806/rpcbind
    udp 0 0 0.0.0.0:985 0.0.0.0:* 14806/rpcbind
    udp 0 0 0.0.0.0:111 0.0.0.0:* 14806/rpcbind
    udp 0 0 10.100.0.255:137 0.0.0.0:* 21620/nmbd
    udp 0 0 10.100.0.250:137 0.0.0.0:* 21620/nmbd
    udp 0 0 0.0.0.0:137 0.0.0.0:* 21620/nmbd
    udp 0 0 10.100.0.255:138 0.0.0.0:* 21620/nmbd
    udp 0 0 10.100.0.250:138 0.0.0.0:* 21620/nmbd
    udp 0 0 0.0.0.0:138 0.0.0.0:* 21620/nmbd

    20/nmbd 
    [root@NB Downloads]# chkconfig --level 35 nfs on
    [root@NB Downloads]# chkconfig|grep nfs
    nfs                0:off    1:off    2:off    3:on    4:off    5:on    6:off
    nfs-rdma           0:off    1:off    2:off    3:off    4:off    5:off    6:off
    nfslock            0:off    1:off    2:off    3:on    4:on    5:on    6:off

    安全:指定ip可以挂载目录

    [root@NB Desktop]# cat /etc/exports 
    #/data *(rw,sync,all_squash,no_subtree_check)#*是所ip都可以这挂载这个目录
    /data 10.100.2.84(rw,sync,all_squash,no_subtree_check)#只允许10.100.2.84挂载

    如果改变/etc/exports一定要重启nfs服务

    服务器端使用showmount命令查询nfs的共享状态

    [root@NB Downloads]# showmount -e
    clnt_create: RPC: Unknown host
    [root@NB Downloads]# showmount -e localhost
    Export list for localhost:
    /data *

    默认查看自己共享的服务,前提是要DNS能解自己,不然会报错!!!如上面的内容

    [root@GitLab ~]# mount -t nfs 192.168.1.3:/data /data_nfs/ #/bin/mount -t nfs -o nosuid,noexec,nodev,noatime,intr,rsize=32768,wsize=32768 192.168.1.3:/data /data_nfs
    [root@GitLab ~]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                           38G  2.2G   34G   6% /
    tmpfs                 939M     0  939M   0% /dev/shm
    /dev/vda1             477M   38M  414M   9% /boot
    192.168.1.3:/data     459G  127G  310G  29% /data_nfs
    备注:noexec,nosuid,nodev,因为共享存放的是简单资料,不需要suid位不需要执行,没有设备文件;hard,intr,bg当NFS连接断了之后会一直去监测服务端的NFS服务直到恢复之后生新连接;
    rsize=32768 wsize=32768调优NFS传输的区块大小;其本参数rw读写权。

     客户端安装:

    yum install -y rpcbind nfs-utils
    #一般这个包是预装好的,监听的是111端口

    客户端使用showmount 命令查询NFS的共享状态:

    [root@GitLab ~]# showmount -e 192.168.1.3
    Export list for 192.168.1.3:
    /data *

    权限问题,客户端发生下面情况:

    [root@GitLab data_nfs]# touch k
    touch: cannot touch `k': Permission denied

    无权限!!!!

    是因为NFS 服务器端共享的目录本身的写权限没有开放给其他用户,在服务器端打开该权限。

    开放权限

    [root@NB /]# chmod -R 777 data/
    # 还不行
    [root@NB /]# chmod -R 755 /data/
    [root@NB /]# cat /etc/exports 
    /data *(rw,sync,all_squash,no_subtree_check)
    [root@NB /]# exportfs -rv
    exporting *:/data

    问题解决!!!(但这种方法,我感觉不是太好下面的方法会更好下些)

    [root@NB Desktop]# cat /etc/exports 
    /data *(rw,no_root_squash) #no_root_squash:root用户具有对根目录的完全管理访问权限
    [root@NB Desktop]# exportfs -av
    #同样问题解决!!!!
    -a 是export所有在/etc/exports文件内列出的文件系统
    -v 是在结果中列出每一个已经export出去的和没有export出去的文件系统

    /etc/exports 用到的一些参数:

     sync:资料同步写入内存和硬盘

     no_subtree_check:不检查父目录的权限。

     all_squash:不论登录nfs的使用者身份如何,他的身份都会被定为匿名便用者,通常也就是nobody

    nfs服务端的防火墙规则:

    -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -m multiport --dports 22,111,875,892,2049 -j ACCEPT
    -A INPUT -p udp -m multiport --dports 53,111,123,875,892,2049 -j ACCEPT
    #这两条是关键点
    # getenforce
    Disabled
    #SELINUX 必须是disabled

     nfs服务所需要的iptables设置:

    portmapp在nfs服务启动的时候给每一个NFS服务分配了一个动态的端口,如这些服务MOUNTD_PORT STATD_PORT LOCKD_TCPPORT LOCKD_UDPPORT

    如何才能让NFS client在使用iptables时可以正常使用NFS服务?办法就是指定将上述服务的运行端口.

    查看nfs的配值端口:
    
    # grep -v '^#' /etc/sysconfig/nfs
    RQUOTAD_PORT=875
    LOCKD_TCPPORT=32803
    LOCKD_UDPPORT=32769
    MOUNTD_PORT=892
    STATD_PORT=662
    [root@NB Desktop]# rpcinfo -p|grep 32769
        100021    1   udp  32769  nlockmgr
        100021    3   udp  32769  nlockmgr
        100021    4   udp  32769  nlockmgr
    [root@NB Desktop]# rpcinfo -p|grep 32803
        100021    1   tcp  32803  nlockmgr
        100021    3   tcp  32803  nlockmgr
        100021    4   tcp  32803  nlockmgr
    [root@NB Desktop]# rpcinfo -p|grep 875
        100011    1   udp    875  rquotad
        100011    2   udp    875  rquotad
        100011    1   tcp    875  rquotad
        100011    2   tcp    875  rquotad
    [root@NB Desktop]# rpcinfo -p|grep 892
        100005    1   udp    892  mountd
        100005    1   tcp    892  mountd
        100005    2   udp    892  mountd
        100005    2   tcp    892  mountd
        100005    3   udp    892  mountd
        100005    3   tcp    892  mountd
    [root@NB Desktop]# rpcinfo -p|grep 662
        100024    1   udp    662  status
        100024    1   tcp    662  status

    iptables 更新2:

    -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -m multiport --dports 22,111,875,892,662,32803,32769,2049 -j ACCEPT
    -A INPUT -p udp -m multiport --dports 53,111,123,875,892,662,32803,32769,2049 -j ACCEPT

     切记如果一要注意路由,如果路由却失也不能正功,这一点也是比效容易副略的.

    [root@NB movies]# netstat -rn
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
    192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0
    10.100.2.0      0.0.0.0         255.255.255.0   U         0 0          0 br0
    169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 br0
    0.0.0.0         10.100.2.1      0.0.0.0         UG        0 0          0 br0

     强制umount nfs文件系统

    sh-4.1# umount /data_nfs/
    umount.nfs: /data_nfs: device is busy
    umount.nfs: /data_nfs: device is busy
    不能umount,要先执行一下fuser -km
    sh-4.1# fuser -km /data_nfs/
    /data_nfs/:          20629c
    sh-4.1# umount /data_nfs/
    sh-4.1# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                           38G  2.2G   34G   6% /
    tmpfs                 939M     0  939M   0% /dev/shm
    /dev/vda1             477M   38M  414M   9% /boot
    sh-4.1# which fuser
    /sbin/fuser
    sh-4.1# rpm -qf /sbin/fuser 
    psmisc-22.6-19.el6_5.x86_64

     服务端的最终配置:

    [root@NB /]# grep 'nfsnobody' /etc/passwd
    nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    [root@NB /]# chown -R nfsnobody:root /data
    [root@NB /]# cat /etc/exports 
    /data *(rw,sync,all_squash,no_subtree_check)

    客户端:

    sh-4.1# /bin/mount -t nfs -o nosuid,noexec,nodev,noatime,intr,rsize=32768,wsize=32768 192.168.1.3:/data /data_nfs

    这样也解决了权限问题!

    指定可以挂载目录的服务器:cat /etc/exports

    /data/ok 10.1.20.11,10.17.47.18(rw,sync,all_squash,no_subtree_check)
    #多服务器用','隔开
    
  • 相关阅读:
    8.1 管道符| 使用技巧
    7.1 elementui的radio无法选中问题
    2.0 es6数组操作
    小练习-双数日期
    匿名函数、三元表达式、列表生成式
    sys.argv
    常用模块 os,sys,
    Python操作数据库
    Python time模块
    加密模块hashlib
  • 原文地址:https://www.cnblogs.com/bass6/p/6081987.html
Copyright © 2011-2022 走看看