Centos6.5安装使用NFS

来源:互联网 发布:罗马帝国艳情史 知乎 编辑:程序博客网 时间:2024/06/08 05:20

NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。

一、服务端要启动rpcbind和nfs服务(NFS4不用于NFS3,不需要安装portmap)

yum install nfs-utils

安装nfs套件

启动服务:

service nfs start

service rpcbind start

如果不放心是否真的启动了,可以查询一下

[root@centos-6 ~]# rpcinfo
   program version netid     address                service    owner
    100000    4    tcp6      ::.0.111               portmapper superuser
    100000    3    tcp6      ::.0.111               portmapper superuser
    100000    4    udp6      ::.0.111               portmapper superuser
    100000    3    udp6      ::.0.111               portmapper superuser
    100000    4    tcp       0.0.0.0.0.111          portmapper superuser
    100000    3    tcp       0.0.0.0.0.111          portmapper superuser
    100000    2    tcp       0.0.0.0.0.111          portmapper superuser
    100000    4    udp       0.0.0.0.0.111          portmapper superuser
    100000    3    udp       0.0.0.0.0.111          portmapper superuser
    100000    2    udp       0.0.0.0.0.111          portmapper superuser
    100000    4    local     /var/run/rpcbind.sock  portmapper superuser
    100000    3    local     /var/run/rpcbind.sock  portmapper superuser
    100024    1    udp       0.0.0.0.188.178        status     29
    100024    1    tcp       0.0.0.0.171.140        status     29
    100024    1    udp6      ::.154.131             status     29
    100024    1    tcp6      ::.186.25              status     29
    100011    1    udp       0.0.0.0.3.107          rquotad    superuser
    100011    2    udp       0.0.0.0.3.107          rquotad    superuser
    100011    1    tcp       0.0.0.0.3.107          rquotad    superuser
    100011    2    tcp       0.0.0.0.3.107          rquotad    superuser
    100005    1    udp       0.0.0.0.200.138        mountd     superuser
    100005    1    tcp       0.0.0.0.163.215        mountd     superuser
    100005    1    udp6      ::.152.1               mountd     superuser
    100005    1    tcp6      ::.194.153             mountd     superuser
    100005    2    udp       0.0.0.0.186.175        mountd     superuser
    100005    2    tcp       0.0.0.0.160.135        mountd     superuser
    100005    2    udp6      ::.160.152             mountd     superuser
    100005    2    tcp6      ::.157.245             mountd     superuser
    100005    3    udp       0.0.0.0.181.118        mountd     superuser
    100005    3    tcp       0.0.0.0.149.97         mountd     superuser
    100005    3    udp6      ::.158.57              mountd     superuser
    100005    3    tcp6      ::.180.81              mountd     superuser
    100003    2    tcp       0.0.0.0.8.1            nfs        superuser
    100003    3    tcp       0.0.0.0.8.1            nfs        superuser
    100003    4    tcp       0.0.0.0.8.1            nfs        superuser
    100227    2    tcp       0.0.0.0.8.1            nfs_acl    superuser
    100227    3    tcp       0.0.0.0.8.1            nfs_acl    superuser
    100003    2    udp       0.0.0.0.8.1            nfs        superuser
    100003    3    udp       0.0.0.0.8.1            nfs        superuser
    100003    4    udp       0.0.0.0.8.1            nfs        superuser
    100227    2    udp       0.0.0.0.8.1            nfs_acl    superuser
    100227    3    udp       0.0.0.0.8.1            nfs_acl    superuser
    100003    2    tcp6      ::.8.1                 nfs        superuser
    100003    3    tcp6      ::.8.1                 nfs        superuser
    100003    4    tcp6      ::.8.1                 nfs        superuser
    100227    2    tcp6      ::.8.1                 nfs_acl    superuser
    100227    3    tcp6      ::.8.1                 nfs_acl    superuser
    100003    2    udp6      ::.8.1                 nfs        superuser
    100003    3    udp6      ::.8.1                 nfs        superuser
    100003    4    udp6      ::.8.1                 nfs        superuser
    100227    2    udp6      ::.8.1                 nfs_acl    superuser
    100227    3    udp6      ::.8.1                 nfs_acl    superuser
    100021    1    udp       0.0.0.0.226.77         nlockmgr   superuser
    100021    3    udp       0.0.0.0.226.77         nlockmgr   superuser
    100021    4    udp       0.0.0.0.226.77         nlockmgr   superuser
    100021    1    tcp       0.0.0.0.217.237        nlockmgr   superuser
    100021    3    tcp       0.0.0.0.217.237        nlockmgr   superuser
    100021    4    tcp       0.0.0.0.217.237        nlockmgr   superuser
    100021    1    udp6      ::.128.242             nlockmgr   superuser
    100021    3    udp6      ::.128.242             nlockmgr   superuser
    100021    4    udp6      ::.128.242             nlockmgr   superuser
    100021    1    tcp6      ::.141.81              nlockmgr   superuser
    100021    3    tcp6      ::.141.81              nlockmgr   superuser
    100021    4    tcp6      ::.141.81              nlockmgr   superuser

注:可以考虑做成开机启动


1、首先对NFS服务作简单的配置,修改主配置文件,

[root@localhost ~]# vim /etc/exports

/tmp    *(rw,no_root_squash,sync)
/public 192.168.0.0/24(rw,sync)    192.168.0.0/24(rw,sync)   *(ro,sync)
/test   192.168.0.100(rw,sync)
/linux  *.feifan.com(rw,all_squash,anonuid=40,anongid=40,sync)

 

2、不要忘了创建目录哦,并做权限修改

[root@localhost ~]# mkdir /public
[root@localhost ~]# mkdir /test
[root@localhost ~]# mkdir /linux

[root@localhost ~]# chmod 757 /public/
[root@localhost ~]# chmod 757 /test/
[root@localhost ~]# chmod 757 /linux/

重新挂载设定内容

[root@localhost ~]# exportfs -arv

或重启nfs服务  service nfs restart

[root@localhost ~]# showmount -e localhost
[root@localhost ~]# tail /var/lib/nfs/etab


为了安全,不要忘记设定防火墙哦

[root@localhost ~]# iptables -A INPUT -i eth0 -p tcp -s 192.168.0.0/24 --dport 111 -j ACCEPT

[root@localhost ~]# iptables -A INPUT -i eth0 -p udp -s 192.168.0.0/24 --dport 111 -j ACCEPT

也可以使用TCP Wrappers来限制

[root@localhost ~]# vim /etc/hosts.allow

mountd: 192.168.0.0/255.255.255.0

mountd: 192.168.1.0/255.255.255.0

[root@localhost ~]# vim /etc/hosts.deny

mountd: ALL

 

二、Client的使用

动rpcbind 服务: service rpcbind start

查看NFS服务器共享的目录

[root@localhost ~]# showmount -e 192.168.0.2 (NFS ip)

[root@localhost ~]# mount -t nfs 192.168.0.2:/public/ /public

开机自动挂载方式

[root@localhost ~]# vim /etc/fstab
192.168.0.2:/public  /public  nfs nosuid,noexec,nodev,rw,bg,soft,rsize=32768,wsize=32768  0 0

主动挂载方式

这里我要说的是,还有一种更好的挂载方法,可以让Client要使用NFS共享的目录资料时,才去挂载它,当隔了5分钟没有使用的时候,就主动的卸载它, 要注意的是,那些 /home/nfs/public 是不需要事先建立的。

[root@localhost ~]# vim /etc/auto.master

/home/nfs       /etc/auto.nfs --timeout=60

[root@localhost ~]# mkdir /home/nfs

[root@localhost ~]# vim /etc/auto.nfs

public  -rw,bg,soft,rsize=32768,wrise=32768     192.168.0.2:/public
testing -rw,bg,soft,rsize=32768,wrise=32768     192.168.0.2:/test
temp    -rw,bg,soft,rsize=32768,wrise=32768     192.168.0.2:/tmp

[root@localhost ~]# /etc/init.d/autofs restart

[root@localhost ~]# cd /home/nfs/public

[root@localhost public]#

竟然进来了,是不是很奇怪啊呵呵

 

实例演示完了,下面详细分析档案权限(权限具体介绍,见下面一篇文章)

情况一:Client 端与 Server 端具有相同的 UID 与帐号
    假设我在 192.168.0.100 登入 NFS Server (IP 假设为 192.168.0.2) 主机,并且192.168.0.100 的帐号为 tom 这个身份,同时,在这部 NFS 上面也有 tom 这个帐号,并具有相同的 UID,那么,

    ①. 由于 192.168.0.2 这部 NFS 主机的 /tmp 权限为 -rwxrwxrwt ,所以 192.168.0.100 上面的用户在 NFS 的/tmp 底下具有存取的权限,并且写入的档案所有人为 tom ;
   ②. 在 /public 当中,由于我有读写的权限,所以如果在 /public 这个目录的权限对于 tom 有开放写入的话,那么我就可以读写,并且我写入的档案所有人是 tom,但是如果 /public 对于 tom 这个使用者并没有开放可以写入的权限时, 那么我还是没有办法写入档案,这点请特别注意!
    ③. 在 /test 当中,我的权限与 /public 相同,但是还需要 NFS 主机的 /test 对于tom 有读写限;
    ④. 在 /linux 当中,由于用户身份一定都会被变成 UID=40 这个帐号,所以,这个目录就必需要针对 UID = 40 的那个帐号名称,修改他的权限才行。

情况二:Client 端与 Server 端的帐号不同

    假如我在 192.168.0.100 的身份为 tom ,但是 192.168.0.2 这部 NFS 主机却没有 tom 这个帐号时,情况会变成怎样呢?

    ①. 我在 /tmp 底下还是可以写入,但是写入的档案所有人变成 nobody 了;
    ②. 我在 /public 里面是否可以写入,还需要视 /public 的权限而定,不过,我的身份被变成 nobody ;
    ③. /test 与 /public 相同;
    ④. /linux 底下,我的身份被变成 UID = 40 那个使用者。

情况三:Client 端的身份为 root
    假如我在 192.168.0.100 的身份为 root,权限变成怎样

    ①. 我在 /tmp 里面可以写入,并且由于 no_root_squash 的参数,改变了预设的 root_squash 设定值,所以在 /tmp 写入的档案所有者为 root ;
    ②. 我在 /public 底下的身份还是被压缩成为 nobody ,因为预设属性里面都具有 root_squash ,所以,如果 /public 有针对 nobody 开放写入权限时,那么我就可以写入,但是档案所有人变成 nobody ;
    ③. /test 与 /home/public 相同;
    ④. /linux 的情况中,root 的身份也被压缩成为 UID = 40 的那个使用者。

 

在我讲解实例以及权限之后,您应该了解了很多,如果这一关通过了,那么你的 NFS 服务掌握的非常好了,在这里还希望大家提出宝贵意见,给我留言,我的QQ: 303247761

 

有兴趣的朋友可以做这样一个实例:
假设环境∶

   1. 假设我的 Linux 主机为 192.168.0.100 这一部;
   2. 预计将 /tmp 以可读写,并且不限制使用者身份的方式分享给所有 192.168.0.0/24 这个网域中的所有 Linux 工作站;
   3. 预计开放 /home/nfs 这个目录,使用的属性为唯读,可提供除了网域内的工作站外,向外亦提供资料内容;
   4. 预计开放 /home/upload 做为 192.168.0.0/24 这个网域的资料上传目录,其中,这个 /home/upload 的使用者及所属群组为 nfs-upload 这个名字,他的 UID 与 GID 均为 210;
   5. 预计将 /home/andy 这个目录仅分享给 192.168.0.50 这部 Linux 主机,以提供该主机上面 andy 这个使用者来使用,也就是说, andy 在 192.168.0.50 及 192.168.0.100 均有帐号,且帐号均为 andy ,所以预计开放 /home/andy 给 andy 使用他的家目录啦!
http://blog.sina.com.cn/s/blog_5f7d4fe60100dt4k.html
0 0
原创粉丝点击