网络文件系统-NFS


:Mr.zhou  阅读: 1,316 次

  NFS 可以让网络中的客户端挂载远程服务端的磁盘像挂载本地磁盘一样简单,从而使客户端可以通过挂载后的本地目录,来访问服务端的文件。NFS 基于RPC(Remote Procedure Call 远程过程调用),所以在启用NFS 服务之前必须先启动 RPC 服务。

nfs1  

 

nfs2  演示环境:CentOS release 6.6 (Final) kernel 2.6.32-504.el6.x86_64

[root@nfserver ~]# hostname
nfserver.z-dig.com
[root@nfserver ~]# grep "IPADDR" /etc/sysconfig/network-scripts/ifcfg-eth0 
IPADDR=192.168.1.251
[root@nfserver ~]# tail -2 /etc/hosts
192.168.1.30 client.z-dig.com
192.168.1.251 nfserver.z-dig.com
[root@nfserver ~]# 
[root@client ~]# hostname
client.z-dig.com
[root@client ~]# grep "IPADDR" /etc/sysconfig/network-scripts/ifcfg-eth0 
IPADDR=192.168.1.30
[root@client ~]# tail -2 /etc/hosts
192.168.1.251 nfserver.z-dig.com
192.168.1.30 client.z-dig.com
[root@client ~]# 

  关闭 SELinux 和 防火墙。

[root@nfserver ~]#/etc/init.d/iptables status
iptables: Firewall is not running.
[root@nfserver ~]#getsebool 
getsebool:  SELinux is disabled

[root@client ~]#/etc/init.d/iptables status
iptables: Firewall is not running.
[root@client ~]#getsebool 
getsebool:  SELinux is disabled

  服务器端
  1、安装 rpcbind 和 nfs-utils 两个软件。

[root@nfserver ~]# yum -y install rpcbind nfs-utils
...
Installed:
  nfs-utils.x86_64 1:1.2.3-54.el6                             
  rpcbind.x86_64 0:0.2.0-11.el6                               

Dependency Installed:
  keyutils.x86_64 0:1.4-5.el6                                 
  libevent.x86_64 0:1.4.13-4.el6                              
  libgssglue.x86_64 0:0.1-11.el6                              
  libtirpc.x86_64 0:0.2.1-10.el6                              
  nfs-utils-lib.x86_64 0:1.1.5-9.el6                          

Complete!
[root@nfserver ~]#

  2、创建要共享的目录,若要共享的目录已经存在则略过创建目录的步骤。但要将共享目录的属主(或属主加属组)改为 nfsnobody。

[root@nfserver ~]# mkdir /upload
[root@nfserver ~]# ll /|grep upload
drwxr-xr-x   2 root root  4096 Jun 17 17:47 upload
[root@nfserver ~]# chown -R nfsnobody.nfsnobody /upload
[root@nfserver ~]# ll /|grep upload
drwxr-xr-x   2 nfsnobody nfsnobody  4096 Jun 17 17:47 upload
[root@nfserver ~]#

  3、编辑 nfs 的配置文件 /etc/exports,将要挂载的目录、允许访问的客户端及访问选项写入。

[root@nfserver ~]# echo "/upload 192.168.1.0/24(rw,sync,all_squash)" >> /etc/exports
[root@nfserver ~]# cat /etc/exports
/upload 192.168.1.0/24(rw,sync,all_squash)
[root@nfserver ~]# 

  4、启动 rpcbind 服务。一定要注意服务的启动顺序,若先启动 nfs 服务再启动 rpcbind 服务,则 nfs 的监听端口不能在 rpc 中注册。

[root@nfserver ~]# /etc/init.d/rpcbind status
rpcbind is stopped
[root@nfserver ~]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@nfserver ~]# /etc/init.d/rpcbind status
rpcbind (pid  1510) is running...
[root@nfserver ~]#

  5、启动 nfs 服务。

[root@nfserver ~]# /etc/init.d/rpcbind status
rpcbind is stopped
[root@nfserver ~]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@nfserver ~]# /etc/init.d/rpcbind status
rpcbind (pid  1510) is running...
[root@nfserver ~]# clear
[root@nfserver ~]# /etc/init.d/nfs status
rpc.svcgssd is stopped
rpc.mountd is stopped
nfsd is stopped
rpc.rquotad is stopped
[root@nfserver ~]# /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  ]
[root@nfserver ~]# /etc/init.d/nfs status
rpc.svcgssd is stopped
rpc.mountd (pid 1599) is running...
nfsd (pid 1615 1614 1613 1612 1611 1610 1609 1608) is running...
rpc.rquotad (pid 1594) is running...
[root@nfserver ~]# 

  6、检查 nfs 服务是否在 rpc 中注册,使用 showmount 检查本机ip可挂载的目录列表。

[root@nfserver ~]# rpcinfo -p
   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
    100011    1   udp    875  rquotad
    100011    2   udp    875  rquotad
    100011    1   tcp    875  rquotad
    100011    2   tcp    875  rquotad
    100005    1   udp  50022  mountd
    100005    1   tcp  47784  mountd
    100005    2   udp  53391  mountd
    100005    2   tcp  60992  mountd
    100005    3   udp  45775  mountd
    100005    3   tcp  36582  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  52972  nlockmgr
    100021    3   udp  52972  nlockmgr
    100021    4   udp  52972  nlockmgr
    100021    1   tcp  59884  nlockmgr
    100021    3   tcp  59884  nlockmgr
    100021    4   tcp  59884  nlockmgr
[root@nfserver ~]# showmount -e 192.168.1.251
Export list for 192.168.1.251:
/upload 192.168.1.0/24
[root@nfserver ~]#

  到此,服务端的已配置完毕。

  7、将 rpcbind nfs 服务设为开机启动。修改配置文件、设置开机启动,最好写上相应的备注。方便他人,以后查看。

[root@nfserver ~]# echo -e "/etc/init.d/rpcbind start 2>/var/log/rpcbinderror.log\n/etc/init.d/nfs start 2>/var/log/nfserror.log">>/etc/rc.local
[root@nfserver ~]# tail -2 /etc/rc.local    
/etc/init.d/rpcbind start 2>/var/log/rpcbinderror.log
/etc/init.d/nfs start 2>/var/log/nfserror.log
[root@nfserver ~]# 

  客户端

  1、客户端也需安装 rpcbind 和 nfs-utils 两个软件。

[root@client ~]# yum -y install rpcbind nfs-utils
...
Installed:
  nfs-utils.x86_64 1:1.2.3-54.el6                rpcbind.x86_64 0:0.2.0-11.el6               

Dependency Installed:
  keyutils.x86_64 0:1.4-5.el6                     libevent.x86_64 0:1.4.13-4.el6             
  libgssglue.x86_64 0:0.1-11.el6                  libtirpc.x86_64 0:0.2.1-10.el6             
  nfs-utils-lib.x86_64 0:1.1.5-9.el6             

Complete!
[root@client ~]# 

  2、启动 rpcbind 服务。

[root@client ~]# /etc/init.d/rpcbind status
rpcbind is stopped
[root@client ~]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@client ~]# /etc/init.d/rpcbind status
rpcbind (pid  1435) is running...
[root@client ~]# 

  3、使用 showmount 查看可以挂载的目录。

  使用服务器的IP地址:

[root@client ~]# showmount -e 192.168.1.251
Export list for 192.168.1.251:
/upload 192.168.1.0/24
[root@client ~]# 

  使用服务器的域名:

[root@client ~]# showmount -e nfserver.z-dig.com
Export list for nfserver.z-dig.com:
/upload 192.168.1.0/24
[root@client ~]# 

  4、创建挂载点,进行挂载。

[root@client ~]# mkdir /upload

  使用IP地址挂载:

[root@client ~]# mount -t nfs 192.168.1.251:/upload /upload
[root@client ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             9.1G  1.7G  6.9G  20% /
tmpfs                 491M     0  491M   0% /dev/shm
/dev/sda1             190M   27M  153M  16% /boot
192.168.1.251:/upload
                      9.1G  1.7G  6.9G  20% /upload
[root@client ~]#

  使用域名挂载:

[root@client ~]# umount /upload
[root@client ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       9.1G  1.7G  6.9G  20% /
tmpfs           491M     0  491M   0% /dev/shm
/dev/sda1       190M   27M  153M  16% /boot
[root@client ~]# mount -t nfs nfserver.z-dig.com:/upload /upload
[root@client ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             9.1G  1.7G  6.9G  20% /
tmpfs                 491M     0  491M   0% /dev/shm
/dev/sda1             190M   27M  153M  16% /boot
nfserver.z-dig.com:/upload
                      9.1G  1.7G  6.9G  20% /upload
[root@client ~]# 

  5、设置开机自动挂载。更改配置文件、设置开机启动服务时,最好写上备注。

[root@client upload]# echo "mount -t nfs nfserver.z-dig.com:/upload /upload">>/etc/rc.local 
[root@client upload]# tail -1 /etc/rc.local 
mount -t nfs nfserver.z-dig.com:/upload /upload
[root@client upload]#

  若要使用 /etc/fstab 进行开机自动挂载,则需要让 netfs 服务开机自动启动。否则 nfs 将挂载失败,因为 nfs 是基于网络的文件系统,需要在网络启动完毕后才能进行挂载。netfs的作用就是在确保网络环境启动完毕后再挂载 nfs 。

[root@mrzhou ~]# chkconfig netfs on    
[root@mrzhou ~]# chkconfig --list netfs
netfs           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@mrzhou ~]#
[root@mrzhou ~]# tail -1 /etc/fstab
nfserver.z-dig.com:/upload      /upload    nfs     defaults        0       0
[root@mrzhou ~]#

 

  测试

[root@nfserver upload]# hostname
nfserver.z-dig.com
[root@nfserver upload]# pwd
/upload
[root@nfserver upload]# ls
[root@nfserver upload]# echo "Testing">test.txt
[root@nfserver upload]# ls
test.txt
[root@nfserver upload]# 

[root@client upload]# hostname
client.z-dig.com
[root@client upload]# pwd
/upload
[root@client upload]# ls
test.txt
[root@client upload]# cat test.txt 
Testing
[root@client upload]#
[root@nfserver upload]# touch client.txt
[root@nfserver upload]# ls
client.txt  test.txt
[root@nfserver upload]# 

  相关选项参数及优化稍后补充。


转载请注明原文链接:http://www.z-dig.com/network-file-system-nfs.html



正文部分到此结束