NFS服务器

环境:

       VMware workstation 10

       centos6.4操作系统

       两台或两台以上虚拟系统


RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易


运行时,一次客户机对服务器的RPC调用,其内部操作大致有如下十步:

1.调用客户端句柄;执行传送参数

2.调用本地系统内核发送网络消息

3.消息传送到远程主机

4.服务器句柄得到消息并取得参数

5.执行远程过程

6.执行的过程将结果返回服务器句柄

7.服务器句柄返回结果,调用远程系统内核

8.消息传回本地主机

9.客户句柄由内核接收消息

10.客户接收句柄返回的数据



在Linux与windows之间用rpc也是一种选择,但不是最佳

主要用于实现网络存储


在nfs客户端,会用rpc.mountd来连接对方的portmapper(发出连接请求.portmapper接收到请求之后会同rpc.mountd来协商通信端口,之后rpc.mountd会利用端口进行挂载到本地)


nfs工作时是将远方的磁盘挂载到本地

rpciod 是连接到对方的nfs上进行io操作的服务进程

nlockmgr是保证在众多的客户连接在进行io操作时数据的一致性,即对正在写操作的文件进行加锁保护

status主要是靠发送封包的形式维持客户与服务器的连接状态


[root@localhost ~]# rpcinfo

-bash: rpcinfo: command not found

[root@localhost etc]# mount /dev/cdrom /media/cdrom

mount: block device /dev/sr0 is write-protected, mounting read-only

[root@localhost etc]# cd /media/cdrom/Packages/

[root@localhost Packages]# ll rpc*

-r--r--r--. 3 root root 52096 Feb 23  2013 rpcbind-0.2.0-11.el6.i686.rpm

[root@localhost Packages]# rpm -qa |grep rpc

xmlrpc-c-client-1.16.24-1209.1840.el6.i686

xmlrpc-c-1.16.24-1209.1840.el6.i686

[root@localhost Packages]# yum --disablerepo=\* --enablerepo=c6-media install rpcbind



[root@localhost Packages]# rpcinfo

rpcinfo: can't contact rpcbind: RPC: Remote system error - No such file or directory


[root@localhost ~]# service rpcbind start

Starting rpcbind:                                          [  OK  ]

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

还可以用 -p 参数来指定主机端口


[root@localhost ~]# rpcinfo -p 127.0.0.1

  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@localhost ~]# chkconfig rpcbind on

[root@localhost ~]# mkdir /share1

编辑访问共享清单列表

关于这个文件的编写格式及含义,大家可以参阅man手册(可能要先安装)


在这其中有几个重要的参数如,no_root_squash,在写操做时映射为匿名账户


清单如下,如允许192.168.2.188进行读写操作,并同步操作,如图

wKioL1MG-kmxlLFMAAAaNOmQr_g276.jpg


网络权限为读写,那么本地的相关权限也要打开

[root@localhost ~]# ll -d /share1/

drwxr-xr-x. 2 root root 4096 Feb 20 00:44 /share1/

[root@localhost ~]# chmod o+wt /share1/


开启nfs服务,并将共享清单释放出去

[root@localhost ~]# service nfs start

Starting NFS services:                                     [  OK  ]

Starting NFS quotas:                                       [  OK  ]

Starting NFS mountd:                                       [  OK  ]

Starting NFS daemon:                                       [  OK  ]

[root@localhost ~]# chkconfig nfs on

[root@localhost ~]# exportfs -rv

exporting 192.168.2.188:/share1


关闭防火墙功能,或修改selinux配置文件(开机自动关闭)

[root@localhost ~]# vim /etc/sysconfig/selinux

wKioL1MG-mSxIG54AAAW3EO6idg672.jpg


[root@localhost ~]# service iptables stop

iptables: Flushing firewall rules:                         [  OK  ]

iptables: Setting chains to policy ACCEPT: filter          [  OK  ]

iptables: Unloading modules:                               [  OK  ]



客户机的也关闭

[root@localhost ~]# service iptables stop

iptables: Flushing firewall rules:                         [  OK  ]

iptables: Setting chains to policy ACCEPT: filter          [  OK  ]

iptables: Unloading modules:                               [  OK  ]

[root@localhost ~]# chkconfig iptables off

[root@localhost ~]# setenforce 0

[root@localhost ~]# mount /dev/sr0 /media/cdrom

mount: block device /dev/sr0 is write-protected, mounting read-only

[root@localhost ~]#

[root@localhost ~]# yum --disablerepo=\* --enablerepo=c6-media install rpcbind


[root@localhost ~]# service rpcbind start

Starting rpcbind:                                          [  OK  ]

wKiom1MG-o2zYcXyAAEWNFv1pVo342.jpg

[root@localhost ~]# chkconfig rpcbind on

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

[root@localhost ~]# showmount

-bash: showmount: command not found

[root@localhost ~]# yum --disablerepo=\* --enablerepo=c6-media install nfs-utils


但是在执行showmount指令时没有,所以要安装和它有关的软件包,在不知道具体软件的情况下可以在

在服务机上进行下列工作可以查出所需命令的软件包


[root@localhost ~]#  which  showmount

/usr/sbin/showmount

[root@localhost ~]# rpm -qf 'which showmount'    #` `是反引号,不是单引号,正则表达式规则

error: file /root/which showmount: No such file or directory

[root@localhost ~]# rpm -qf `which showmount`

nfs-utils-1.2.3-36.el6.i686

然后在客户机上安装

[root@localhost ~]# yum --disablerepo=\* --enablerepo=c6-media install nfs-utils


[root@localhost ~]# showmount -e 192.168.2.199

Export list for 192.168.2.199:

/share1 192.168.2.188


[root@localhost ~]# mount 192.168.2.199:/share1 /mnt/nfs

[root@localhost ~]# mount

/dev/sda2 on / type ext4 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")

/dev/sda1 on /boot type ext4 (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)

/dev/sr0 on /media/cdrom type iso9660 (ro)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

192.168.2.199:/share1 on /mnt/nfs type nfs (rw,vers=4,addr=192.168.2.199,clientaddr=192.168.2.188)


[root@localhost ~]# cd /mnt/nfs

[root@localhost nfs]# ll

total 0

[root@localhost nfs]# touch f1

[root@localhost nfs]# ll

total 0

-rw-r--r--. 1 nfsnobody nfsnobody 0 Feb 20 01:57 f1 #权限正常


测试其他ip的主机能否操作

先卸载本地挂载点

[root@localhost nfs]# cd

[root@localhost ~]# fuser -km /mnt/nfs

[root@localhost ~]# umount /mnt/nfs

[root@localhost ~]# mount

/dev/sda2 on / type ext4 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")

/dev/sda1 on /boot type ext4 (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)

/dev/sr0 on /media/cdrom type iso9660 (ro)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

修改客户端的ip(临时ip)

wKioL1MG-mnC5z5JAAA6mrsSgjE289.jpg

权限拒绝


测试针对所有人的挂载操作

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

wKiom1MG-o6gViMGAAAlyxqUi6k387.jpg


[root@localhost ~]# exportfs -rv

exporting 192.168.2.188:/share1

exporting *:/share1

mount查看结果,先测试192.168.2.188

wKioL1MG-mzS9lZ6AAEB1YSh7T4743.jpg

并进如目录,(129.168.2.177身份)实际测试

wKiom1MG-pXTjzxfAABSGli2GKo443.jpg




自动挂载测试

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

wKioL1MG-nDgvZ8FAAAqDuH8eJE228.jpg


测试,(先确保以前的已卸载)

wKioL1MG-nHzaWUfAACaxfFAprs515.jpg


wKiom1MG-pmSYfbHAAFHrwekkpk262.jpg



解决rpc中status状态占用带宽的问题,借助于自动文件系统,可能要先安装自动文件系统的相关包

[root@localhost ~]# rpm -qa |grep auto

avahi-autoipd-0.6.25-12.el6.i686

[root@localhost ~]# yum --disablerepo=\* --enablerepo=c6-media install autofs


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

wKiom1MG-pqDFOrxAAAU8JwTtts977.jpg

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

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

wKioL1MG-nWisPwXAAAcCCos-CM238.jpg

注意上图中的文件格式是不正确的,因为文件参数默认以空格为分隔符,正确为

wKioL1MG-nXhdqwuAAAo7xGUfxg988.jpg


修改行的参数意义为:去监控/mnt目录,当60之后该目录没有任何io操作时,将去执行/etc/auto.nfs中的内容


而在/etc/auto.nfs中我们的文件参数含义为,挂载192.168.2.199:/share1到/nfs1(/mnt/)目录中去,注意此时我们监控的是在/mnt目录下。所以会自动在该目录下创建子目录,不需要事先存在

,且该目录会在超时后自动消失



[root@localhost mnt]# service autofs restart

Stopping automount:                                        [  OK  ]

Starting automount:                                        [  OK  ]

[root@localhost mnt]# cd nfs1/

[root@localhost nfs1]# mount

/dev/sda2 on / type ext4 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")

/dev/sda1 on /boot type ext4 (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)

/dev/sr0 on /media/cdrom type iso9660 (ro)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

192.168.2.199:/share1 on /mnt/nfs1 type nfs (rw,soft,intr,sloppy,vers=4,addr=192.168.2.199,clientaddr=192.168.2.188)


监视mount

[root@localhost nfs1]# cd

[root@localhost ~]# watch -n  1 'mount'

wKiom1MG-pyCTHAjAAC3l7U35no848.jpg

没有挂载,然后进入,(会自动挂载)


wKiom1MG-p_SfFscAAAlmksn_og864.jpg

wKiom1MHJI2CqdHpAADrbio3bwM264.jpg回到主目录,等候60秒,让其自动卸载

[root@localhost ~]# cd

[root@localhost ~]# pwd

/root

wKioL1MG-nzQj_tvAAC_-xAlEug660.jpg

我们的实验正常结果应该是解决在进入指定目录后,会自动挂载,并在超时时间内自动卸载,

结果正常,测试结束.