zoukankan      html  css  js  c++  java
  • Linux系统NFS网络文件系统

    Linux系统NFS网络文件系统

    NFS(network file system)网络文件系统,就是通过网络让不同的主机系统之间可以共享文件或目录,此种方法NFS客户端使用挂载的方式让共享文件或目录到本地系统可挂载的目录下

    NFS实现是通过RPC服务来实现的

    实现过程:

    1、NFS RPC主要的功能是记录每个NFS功能所对应的端口号,并将信息传递给请求数据的NFS客户端,从而实现数据的传输

    2、NFS服务启动时会随机取用数个端口,并主动向RPC服务注册取用的相关端口信息,RPC服务就知道每个端口对应的NFS功能了,然后RPC会用固定的端口(111)来监听NFS客户端的请求,将正确的NFS端口传给NFS的客户端

    服务端启动顺序:

    事先RPC服务,后启动NFS服务,否则NFS服务无法向RPC服务进行注册,Centos 5.x 系统下RPC服务为portmap,Centos 6.x系统为rpcbind

    NFS软件:nfs-utils 是NFS的主程序

    NFS配置文件格式如下:

    NFS共享目录   NFS客户端地址(参数1.参数2)

    1、NFS共享目录的实际目录必须是绝对路径

    2、NFS客户端为服务端授权可以访问共享目录的NFS客户端地址,可以是IP、域名、主机名、整个网段,或者用*来匹配所有主机

    3、参数是对授权访问NFS共享目录的权限(客户端的访问权限集合)

    安装布署过程

     

    服务器端配置如下

    1、查看操作系统版本与内核

    [root@Centos ~]# cat /etc/redhat-release 

    CentOS release 6.5 (Final)

    [root@Centos ~]# uname -r

    2.6.32-431.el6.x86_64

    2、检查是否安装NFS RPC服务

    [root@Centos ~]# rpm -aq nfs-utils rpcbind

    rpcbind-0.2.0-12.el6.x86_64

    nfs-utils-1.2.3-39.el6.x86_64

    3、如果没安装相关服务请使用如下命令进行安装

    yum install nfs-utils -y

    yum install rpcbind  -y

    启动

    按启动的先后顺序来启动服务

    [root@Centos ~]# /etc/init.d/rpcbind status

    rpcbind is stopped

    [root@Centos ~]# /etc/init.d/rpcbind start

    Starting rpcbind:                                          [  OK  ]

    [root@Centos ~]# /etc/init.d/nfs status        

    rpc.svcgssd is stopped

    rpc.mountd is stopped

    nfsd is stopped

    rpc.rquotad is stopped

    [root@Centos ~]# 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

    [root@Centos ~]# /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  ]

    相关服务启动后,我们再来查看NFS向RPC的注册情况

    如果想让NFS、RPC服务开机自启动,实际生产环境中一般都会将配置放在/etc/rc.local下面

    [root@Centos ~]# cat /etc/rc.local

    ####start up by 2016-08-21

    /etc/init.d/rpcbind start

    /etc/init.d/nfs start

    检查配置情况

    [root@Centos /]# tail -2 /etc/rc.local 

    /etc/init.d/rpcbind start

    /etc/init.d/nfs start

    5、配置服务端NFS配置文件/etc/exports

    [root@Centos ~]# vi /etc/exports 

    #####config for nfs-server 2016-8-21

    /data/bbs 192.168.1.3(rw,sync)

    检查配置情况

    [root@Centos /]# tail -2 /etc/exports 

    #####config for nfs-server 2016-8-21

    /data/bbs 192.168.1.3(rw,sync)

    6、配置完成后重启NFS服务

    /etc/init.d/nfs reload  (/usr/bin/exprots -r) 两者功能相同

    [root@Centos /]# /etc/init.d/nfs reload

    客户端配置如下

    1、查看服务器操作系统版本与内核

    [root@localhost ~]# cat /etc/redhat-release 

    CentOS release 6.5 (Final)

    [root@localhost ~]# uname -r

    2.6.32-431.el6.x86_64

    2、启动RPC服务

    [root@localhost ~]# /etc/init.d/rpcbind status

    rpcbind is stopped

    [root@localhost ~]# /etc/init.d/rpcbind start 

    Starting rpcbind:                                          [  OK  ]

    [root@localhost ~]# /etc/init.d/rpcbind status

    rpcbind (pid  25679) is running...

    3、查看挂载情况

    [root@localhost ~]# showmount -e 192.168.1.2    

    clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

    [root@localhost ~]# ping 192.168.1.2

    PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.

    64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.745 ms

    64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.532 ms

    64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.470 ms

    64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=0.522 ms

    ^C

    --- 192.168.1.2 ping statistics ---

    4 packets transmitted, 4 received, 0% packet loss, time 3060ms

    rtt min/avg/max/mdev = 0.470/0.567/0.745/0.106 ms

    [root@localhost ~]# iptables -F           关闭服务与客户端防火墙

    [root@localhost ~]# showmount -e 192.168.1.2        

    Export list for 192.168.1.2:

    /data/bbs 192.168.1.3

    4、客户端进行NFS挂载

    [root@localhost ~]# mount -t nfs 192.168.1.2:/data/bbs /mnt

    [root@localhost ~]# df -h

    Filesystem                    Size  Used Avail Use% Mounted on

    /dev/mapper/VolGroup-lv_root   18G  3.3G   14G  20% /

    tmpfs                         491M   72K  491M   1% /dev/shm

    /dev/sda1                     485M   35M  426M   8% /boot

    192.168.1.2:/data/bbs          50G  3.5G   44G   8% /mnt

    5、测试共享

    服务端查看具体目录权限

    [root@Centos /]# ls -ld data/bbs/ 

    drwxr-xr-x. 2 root root 4096 Aug 21 10:07 data/bbs/

    表明其它用户具有读与执行权限

    [root@Centos /]# cd data/bbs/

    [root@Centos bbs]# touch text.txt

    [root@Centos bbs]# mkdir textdir

    [root@Centos bbs]# ls

    textdir  text.txt

    客户端测试

    [root@localhost ~]# cd /mnt

    [root@localhost mnt]# ls -ll

    total 4

    drwxr-xr-x. 2 root root 4096 Aug 21 05:39 textdir

    -rw-r--r--. 1 root root    0 Aug 21 05:39 text.txt

    [root@localhost mnt]# touch 123.txt

    touch: cannot touch `123.txt': Permission denied ----> 权限不足

    所以需要在服务端改变其它用户的访问权限才可以

    [root@Centos /]# chmod 777 data/bbs/

    [root@Centos /]# ls -ld data/bbs/

    drwxrwxrwx. 3 root root 4096 Aug 21 10:39 /data/bbs/

    然后在客户端测试

    [root@localhost mnt]# pwd

    /mnt

    [root@localhost mnt]# touch 123.txt

    [root@localhost mnt]# ls -ll

    total 4

    -rw-r--r--. 1 nfsnobody nfsnobody    0 Aug 21 05:44 123.txt

    drwxr-xr-x. 2 root      root      4096 Aug 21 05:39 textdir

    -rw-r--r--. 1 root      root         0 Aug 21 05:39 text.txt

    [root@localhost mnt]# echo "123">>123.txt 

    [root@localhost mnt]# cat 123.txt 

    123

     

    经过测试表明,客户端也可以正常访问共享目录与文件,同时也具有读写权限,那么问题又来了,客户端是通过什么用户名来访问服务器端的呢,刚刚修改的权限是征对其它用户(除了文件、目录的所有者与所属组外的用户),如果需要限制访问共享目录,其实这个权限是具有很大安全隐患的

    那么我们来服务端看看到底是授权什么用户来访问共享目录呢???

    [root@Centos /]# cat /var/lib/nfs/etab 

    /data/bbs       192.168.1.3(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534)

    这里我们查看这个uid/gid都为65534的用户是哪个???

    [root@Centos /]# grep 65534 /etc/passwd

    nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

    所以服务端我们将权限重新修改如下

    [root@Centos /]# chown -R nfsnobody:nfsnobody data/bbs/

    [root@Centos /]# ls -ld data/bbs/

    drwxrwxrwx. 3 nfsnobody nfsnobody 4096 Aug 21 10:44 data/bbs/

    客户端查看配置生效情况如下

    [root@localhost mnt]# ls -ll

    total 8

    -rw-r--r--. 1 nfsnobody nfsnobody    4 Aug 21 05:44 123.txt

    drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Aug 21 05:39 textdir

    -rw-r--r--. 1 nfsnobody nfsnobody    0 Aug 21 05:39 text.txt

    但是细心的朋友们肯定也发现了,这样还是不安全,因为所有相同的版本的系统都会默认存在这一个用户nfsnobody,那么对限制访问还是没有做到万无一失,因此这里就出现了更改默认用户的做法,将默认用户更改成其它用户

    其服务端配置如下

    首先得添加用户并禁止登陆指定它的UID

    [root@Centos /]# useradd -s /sbin/nologin -M -u 3000 nfsuser

    [root@Centos /]# tail -1 /etc/passwd

    nfsuser:x:3000:3000::/home/nfsuser:/sbin/nologin

    修改NFS配置

    [root@Centos /]# echo "#####config for nfs-server 2016-8-21">/etc/exports 

    清空配置并加上一行注释信息

    [root@Centos /]# echo "/data/bbs       192.168.1.3(rw,sync,all_squash,anonuid=3000,anongid=3000)">>/etc/exports         添加相关配置

    [root@Centos /]# tail -2 /etc/exports            检查配置情况

    #####config for nfs-server 2016-8-21

    /data/bbs       192.168.1.3(rw,sync,all_squash,anonuid=3000,anongid=3000)

    因此客户端同样也需要添加用户

    [root@localhost mnt]# useradd -s /sbin/nologin -M -u 3000 nfsuser

    [root@localhost mnt]# ls -ll

    total 8

    -rw-r--r--. 1 nfsnobody nfsnobody    4 Aug 21 05:44 123.txt

    drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Aug 21 05:39 textdir

    -rw-r--r--. 1 nfsnobody nfsnobody    0 Aug 21 05:39 text.txt

    测试发现还是原来默认的用户,这是因为服务端虽然改了NFS的配置,但是共享目录与文件的相关配置仍然是默认的所以上服务端查看下目录权限如下

    [root@Centos bbs]# ls -ll

    total 8

    -rw-r--r--. 1 nfsnobody nfsnobody    4 Aug 21 10:44 123.txt

    drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Aug 21 10:39 textdir

    -rw-r--r--. 1 nfsnobody nfsnobody    0 Aug 21 10:39 text.txt

    因此需要对/data/bbs/这个共享目录进行更改

    [root@Centos bbs]# chown -R nfsuser.nfsuser /data/bbs/

    [root@Centos bbs]# ls -ll

    total 8

    -rw-r--r--. 1 nfsuser nfsuser    4 Aug 21 10:44 123.txt

    drwxr-xr-x. 2 nfsuser nfsuser 4096 Aug 21 10:39 textdir

    -rw-r--r--. 1 nfsuser nfsuser    0 Aug 21 10:39 text.txt

     

    最后登陆到客户查看配置生效情况如下

    [root@localhost mnt]# ls -ll

    total 8

    -rw-r--r--. 1 nfsuser nfsuser    4 Aug 21 05:44 123.txt

    drwxr-xr-x. 2 nfsuser nfsuser 4096 Aug 21 05:39 textdir

    -rw-r--r--. 1 nfsuser nfsuser    0 Aug 21 05:39 text.txt

    [root@localhost mnt]# touch 234.txt

    [root@localhost mnt]# ls -ll

    total 8

    -rw-r--r--. 1 nfsuser nfsuser    4 Aug 21 05:44 123.txt

    -rw-r--r--. 1 nfsuser nfsuser    0 Aug 21 06:27 234.txt

    -rw-r--r--. 1 nfsuser nfsuser    0 Aug 21 06:17 23.txt

    drwxr-xr-x. 2 nfsuser nfsuser 4096 Aug 21 05:39 textdir

    -rw-r--r--. 1 nfsuser nfsuser    0 Aug 21 05:39 text.txt

    表明配置正确,且达到需求,此时访问共享目录的权限就更改为nfsuser,其它主机如果没有添加此用户是无法正常访问NFS的共享目录与文件的,安全性比较高。至此整个安装与布署过和结束





  • 相关阅读:
    python——集合
    python——字典
    python——用递归的方法求x的y次幂
    python——全局变量&局部变量
    python——组织列表
    Linux显示网络相关信息
    Linux里的发消息
    Linux中各种压缩文件
    Linux中的man
    Linux的find命令
  • 原文地址:https://www.cnblogs.com/youkanyouxiao/p/5861773.html
Copyright © 2011-2022 走看看