Ubuntu配置NFS

来源:互联网 发布:nginx 隐藏ip 编辑:程序博客网 时间:2024/06/12 21:21

NFS 是Network File System的缩写,即网络文件系统,一种使用于分散式文件系统的协定

NFS通过网络让不同的机器、不同的操作系统能够彼此分享个别的数据,例如将/home设置为共享目录,然后共享给网络中的其它服务器,这样每台机器就不必单独建立自己的/home路径了,NFS独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享,是在类Unix系统间实现磁盘文件共享的一种方法

NFS本身没有提供信息传输的协议和功能,而是使用RPC服务完成这部分功能。可以说NFS本身就是使用RPC服务的一个程序,或者说NFS也是一个RPC SERVER。所以只要用到NFS的地方都要启动RPC服务,不论是NFS server还是NFS client,可以这么理解RPC和NFS之间的关系:NFS是一个文件系统,而RPC负责信息的传输

一,系统环境

Linux Distribution:Ubuntu 14


二,安装Linux NFS

[plain] view plain copy
  1. sean@sean:~$ sudo apt-get install nfs-kernel-server  
  2. [sudo] password for sean:   
  3. Reading package lists... Done  
  4. Building dependency tree         
  5. Reading state information... Done  
  6. The following extra packages will be installed:  
  7.   keyutils libgssglue1 libnfsidmap2 libtirpc1 nfs-common rpcbind  
  8. Suggested packages:  
  9.   open-iscsi watchdog  
  10. The following NEW packages will be installed:  
  11.   keyutils libgssglue1 libnfsidmap2 libtirpc1 nfs-common nfs-kernel-server  
  12.   rpcbind  
  13. 0 upgraded, 7 newly installed, 0 to remove and 159 not upgraded.  
  14. Need to get 460 kB of archives.  
  15. After this operation, 2,049 kB of additional disk space will be used.  
  16. Do you want to continue? [Y/n] Y  
  17. Get:1 http://cn.archive.ubuntu.com/ubuntu/ trusty/main libgssglue1 amd64 0.4-2ubuntu1 [19.7 kB]  
  18. Get:2 http://cn.archive.ubuntu.com/ubuntu/ trusty/main libnfsidmap2 amd64 0.25-5 [32.2 kB]  
  19. Get:3 http://cn.archive.ubuntu.com/ubuntu/ trusty/main libtirpc1 amd64 0.2.2-5ubuntu2 [71.3 kB]  
  20. Get:4 http://cn.archive.ubuntu.com/ubuntu/ trusty/main keyutils amd64 1.5.6-1 [33.6 kB]  
  21. Get:5 http://cn.archive.ubuntu.com/ubuntu/ trusty-updates/main rpcbind amd64 0.2.1-2ubuntu2.2 [37.1 kB]  
  22. Get:6 http://cn.archive.ubuntu.com/ubuntu/ trusty-updates/main nfs-common amd64 1:1.2.8-6ubuntu1.2 [181 kB]  
  23. Get:7 http://cn.archive.ubuntu.com/ubuntu/ trusty-updates/main nfs-kernel-server amd64 1:1.2.8-6ubuntu1.2 [85.8 kB]  
  24. Fetched 460 kB in 16s (27.2 kB/s)                                                
  25. Selecting previously unselected package libgssglue1:amd64.  
  26. (Reading database ... 168701 files and directories currently installed.)  
  27. Preparing to unpack .../libgssglue1_0.4-2ubuntu1_amd64.deb ...  
  28. Unpacking libgssglue1:amd64 (0.4-2ubuntu1) ...  
  29. Selecting previously unselected package libnfsidmap2:amd64.  
  30. Preparing to unpack .../libnfsidmap2_0.25-5_amd64.deb ...  
  31. Unpacking libnfsidmap2:amd64 (0.25-5) ...  
  32. Selecting previously unselected package libtirpc1:amd64.  
  33. Preparing to unpack .../libtirpc1_0.2.2-5ubuntu2_amd64.deb ...  
  34. Unpacking libtirpc1:amd64 (0.2.2-5ubuntu2) ...  
  35. Selecting previously unselected package keyutils.  
  36. Preparing to unpack .../keyutils_1.5.6-1_amd64.deb ...  
  37. Unpacking keyutils (1.5.6-1) ...  
  38. Selecting previously unselected package rpcbind.  
  39. Preparing to unpack .../rpcbind_0.2.1-2ubuntu2.2_amd64.deb ...  
  40. Unpacking rpcbind (0.2.1-2ubuntu2.2) ...  
  41. Selecting previously unselected package nfs-common.  
  42. Preparing to unpack .../nfs-common_1%3a1.2.8-6ubuntu1.2_amd64.deb ...  
  43. Unpacking nfs-common (1:1.2.8-6ubuntu1.2) ...  
  44. Selecting previously unselected package nfs-kernel-server.  
  45. Preparing to unpack .../nfs-kernel-server_1%3a1.2.8-6ubuntu1.2_amd64.deb ...  
  46. Unpacking nfs-kernel-server (1:1.2.8-6ubuntu1.2) ...  
  47. Processing triggers for man-db (2.6.7.1-1ubuntu1) ...  
  48. Processing triggers for ureadahead (0.100.0-16) ...  
  49. ureadahead will be reprofiled on next reboot  
  50. Setting up libgssglue1:amd64 (0.4-2ubuntu1) ...  
  51. Setting up libnfsidmap2:amd64 (0.25-5) ...  
  52. Setting up libtirpc1:amd64 (0.2.2-5ubuntu2) ...  
  53. Setting up keyutils (1.5.6-1) ...  
  54. Setting up rpcbind (0.2.1-2ubuntu2.2) ...  
  55.  Removing any system startup links for /etc/init.d/rpcbind ...  
  56. rpcbind start/running, process 12074  
  57. Processing triggers for ureadahead (0.100.0-16) ...  
  58. Setting up nfs-common (1:1.2.8-6ubuntu1.2) ...  
  59.   
  60. Creating config file /etc/idmapd.conf with new version  
  61.   
  62. Creating config file /etc/default/nfs-common with new version  
  63. Adding system user `statd' (UID 118) ...  
  64. Adding new user `statd' (UID 118) with group `nogroup' ...  
  65. Not creating home directory `/var/lib/nfs'.  
  66. statd start/running, process 12307  
  67. gssd stop/pre-start, process 12341  
  68. idmapd start/running, process 12399  
  69. Processing triggers for ureadahead (0.100.0-16) ...  
  70. Setting up nfs-kernel-server (1:1.2.8-6ubuntu1.2) ...  
  71.   
  72. Creating config file /etc/exports with new version  
  73.   
  74. Creating config file /etc/default/nfs-kernel-server with new version  
  75.  * Not starting NFS kernel daemon: no exports.  
  76. Processing triggers for libc-bin (2.19-0ubuntu6.7) ...  
  77. Processing triggers for ureadahead (0.100.0-16) ...  

通过安装日志可以发现总共会安装keyutils,libgssglue1,libnfsidmap2,libtirpc1,nfs-common,nfs-kernel-server,rpcbind这7个包

很多文档中安装的包为portmap,但是这个包已经被rpcbind替代

[plain] view plain copy
  1. sean@sean:~$ sudo apt-get install portmap  
  2. Reading package lists... Done  
  3. Building dependency tree         
  4. Reading state information... Done  
  5. Note, selecting 'rpcbind' instead of 'portmap'  
  6. rpcbind is already the newest version.  
  7. 0 upgraded, 0 newly installed, 0 to remove and 164 not upgraded.  

rpcbind包安装完成后会自动启动rpcbind服务

[plain] view plain copy
  1. sean@sean:~$ ps -ef|grep rpcbind  
  2. root        807      1  0 22:27 ?        00:00:00 rpcbind  
  3. sean      10215   9528  0 22:48 pts/6    00:00:00 grep --color=auto rpcbind  

但是由于目前NFS的配置文件为空,NFS服务并没有启动


三,配置NFS服务

从安装日志中我们可以发现NFS服务的配置文件为/etc/exports,并且这个文件在安装过程中已经生成好了,我们所要做的就是将NFS配置信息添加到这个文件中

[plain] view plain copy
  1. sean@sean:~$ sudo vi /etc/exports  
[plain] view plain copy
  1. # /etc/exports: the access control list for filesystems which may be exported  
  2. #               to NFS clients.  See exports(5).  
  3. #  
  4. # Example for NFSv2 and NFSv3:  
  5. # /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)  
  6. #  
  7. # Example for NFSv4:  
  8. # /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)  
  9. # /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)  
  10. /home/sean/shareDir 192.168.137.129(rw,no_root_squash,async)  

其中中的最后一行为新添加的NFS配置,NFS配置信息格式如下:

[plain] view plain copy
  1. <共享目录> [客户端1 选项(访问权限,用户映射,其他)] [客户端2 选项(访问权限,用户映射,其他)]  

1,共享目录:

共享目录是指NFS系统中需要共享给客户机使用的目录


2,客户端:

客户端是指网络中可以访问NFS共享目录的计算机

客户端常用的指定方式:

(1)指定ip地址的主机:192.168.0.1
(2)指定子网中的所有主机:192.168.0.0/255.255.255.0
(3)指定域名的主机:www.sean.com
(4)指定域中的所有主机:*.sean.com
(5)所有主机:*


3,选项:

选项用来设置输出目录的访问权限、用户映射等,NFS主要有3类选项:

访问权限选项:

(1)设置输出目录只读:ro
(2)设置输出目录读写:rw

用户映射选项:

(1)all_squash:将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody)
(2)no_all_squash:与all_squash取反(默认设置)
(3)root_squash:将root用户及所属组都映射为匿名用户或用户组(默认设置)
(4)no_root_squash:与rootsquash取反
(5)anonuid=xxx:将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx)
(6)anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx)

其它选项:

(1)secure:限制客户端只能从小于1024的tcp/ip端口连接nfs服务器(默认设置)
(2)insecure:允许客户端从大于1024的tcp/ip端口连接服务器
(3)sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性
(4)async:将数据先保存在内存缓冲区中,必要时才写入磁盘
(5)wdelay:检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率(默认设置)
(6)no_wdelay:若有写操作则立即执行,应与sync配合使用
(7)subtree:若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置)
(8)no_subtree:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率


四,启动NFS服务

[plain] view plain copy
  1. sean@sean:~$ sudo service nfs-kernel-server start  
  2.  * Exporting directories for NFS kernel daemon...                                                                                                                                exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' specified for export "192.168.137.129:/home/sean/shareDir".  
  3.   Assuming default behaviour ('no_subtree_check').  
  4.   NOTE: this default has changed since nfs-utils version 1.0.x  
  5.   
  6.                                                                                                                                                                           [ OK ]  
  7.  * Starting NFS kernel daemon     
  8. sean@sean:~$ sudo service nfs-kernel-server status  
  9. nfsd running  
  10. sean@sean:~$ ps -ef|grep nfsd  
  11. root      10536      2  0 23:40 ?        00:00:00 [nfsd4]  
  12. root      10537      2  0 23:40 ?        00:00:00 [nfsd4_callbacks]  
  13. root      10539      2  0 23:40 ?        00:00:00 [nfsd]  
  14. root      10540      2  0 23:40 ?        00:00:00 [nfsd]  
  15. root      10541      2  0 23:40 ?        00:00:00 [nfsd]  
  16. root      10542      2  0 23:40 ?        00:00:00 [nfsd]  
  17. root      10543      2  0 23:40 ?        00:00:00 [nfsd]  
  18. root      10544      2  0 23:40 ?        00:00:00 [nfsd]  
  19. root      10545      2  0 23:40 ?        00:00:00 [nfsd]  
  20. root      10546      2  0 23:40 ?        00:00:00 [nfsd]  
  21. sean      10566   9528  0 23:41 pts/6    00:00:00 grep --color=auto nfsd  

通过命令可以看到文件夹已经被共享

[plain] view plain copy
  1. sean@sean:~$ showmount -e  
  2. Export list for sean:  
  3. /home/sean/shareDir 192.168.137.129  


五,客户端挂载NFS共享目录

[plain] view plain copy
  1. sean@ubuntu:~$ sudo mount 192.168.137.128:/home/sean/shareDir /home/sean/shared  
  2. mount: wrong fs type, bad option, bad superblock on 192.168.137.128:/home/sean/shareDir,  
  3.        missing codepage or helper program, or other error  
  4.        (for several filesystems (e.g. nfs, cifs) you might  
  5.        need a /sbin/mount.<type> helper program)  
  6.        In some cases useful info is found in syslog - try  
  7.        dmesg | tail  or so  

上网查看了一下错误原因,是因为客户端没有安装nfs-common包

[plain] view plain copy
  1. sean@ubuntu:~$ sudo apt-get install nfs-common  

之后再次使用挂载命令即可,此时如果我们再次查看客户端挂载的磁盘,就可以看到NFS共享目录已经被挂载上了

[plain] view plain copy
  1. sean@ubuntu:~$ df  
  2. Filesystem                          1K-blocks    Used Available Use% Mounted on  
  3. udev                                   488800       4    488796   1% /dev  
  4. tmpfs                                   99904    1456     98448   2% /run  
  5. /dev/sda1                            19478204 3745568  14720156  21% /  
  6. none                                        4       0         4   0% /sys/fs/cgroup  
  7. none                                     5120       0      5120   0% /run/lock  
  8. none                                   499512     152    499360   1% /run/shm  
  9. none                                   102400      40    102360   1% /run/user  
  10. /dev/sr0                              1044480 1044480         0 100% /media/sean/Ubuntu 14.04.4 LTS amd64  
  11. 192.168.137.128:/home/sean/shareDir  19478272 4288896  14176896  24% /home/sean/shared  

此时如果我们在128的/home/sean/shareDir目录下创建一个文件,在129的/home/sean/shared目录下可以看到一个名称相同的文件,并且文件的内容也相同


六,开机启动NFS服务

安装完成之后查看系统服务

[plain] view plain copy
  1. sean@sean:~$ runlevel  
  2. N 2  
  3. sean@sean:~$ ll /etc/rc2.d  
  4. total 20  
  5. drwxr-xr-x   2 root root  4096  6月 20 23:25 ./  
  6. drwxr-xr-x 132 root root 12288  6月 26 20:16 ../  
  7. -rw-r--r--   1 root root   677  2月 17 12:59 README  
  8. lrwxrwxrwx   1 root root    20  2月 22 06:33 S20kerneloops -> ../init.d/kerneloops*  
  9. lrwxrwxrwx   1 root root    27  6月 20 23:25 S20nfs-kernel-server -> ../init.d/nfs-kernel-server*  
  10. lrwxrwxrwx   1 root root    15  2月 22 06:33 S20rsync -> ../init.d/rsync*  
  11. lrwxrwxrwx   1 root root    27  2月 22 06:33 S20speech-dispatcher -> ../init.d/speech-dispatcher*  
  12. lrwxrwxrwx   1 root root    15  2月 22 06:33 S50saned -> ../init.d/saned*  
  13. lrwxrwxrwx   1 root root    19  2月 22 06:33 S70dns-clean -> ../init.d/dns-clean*  
  14. lrwxrwxrwx   1 root root    18  2月 22 06:33 S70pppd-dns -> ../init.d/pppd-dns*  
  15. lrwxrwxrwx   1 root root    21  2月 22 06:33 S99grub-common -> ../init.d/grub-common*  
  16. lrwxrwxrwx   1 root root    18  2月 22 06:33 S99ondemand -> ../init.d/ondemand*  
  17. lrwxrwxrwx   1 root root    18  2月 22 06:33 S99rc.local -> ../init.d/rc.local*  

nfs-kernel-server是自动启动,就不需要再进行修改了,而rpcbind就比较麻烦了,找了半天才找到vi /etc/init/rpcbind-boot.conf

[plain] view plain copy
  1. # portmap-boot  
  2.   
  3. description "Upstart job to start rpcbind on boot only"  
  4. author "Clint Byrum"  
  5.   
  6. emits start-rpcbind  
  7.   
  8. start on virtual-filesystems and net-device-up IFACE=lo  
  9.   
  10. task  
  11.   
  12. exec initctl emit --no-wait start-rpcbind ON_BOOT=y  

可以见到这个服务设为开机启动了


七,开机自动挂载共享盘

这时候我们需要修改的文件是/etc/fstab,命令格式如下:

[plain] view plain copy
  1. <NFS服务端>:<共享目录> <本地目录> <文件系统类型> <选项> <dump> <pass>  

每个字段的具体含义这里就不详细解释了,vi /etc/fstab,在文件中添加下面这行配置

[plain] view plain copy
  1. 192.168.137.128:/home/sean/shareDir /home/sean/shared nfs defaults 0 0  

系统每次启动时即会自动挂载共享盘

0 0