sean@sean:~$ sudo apt-get install nfs-kernel-server[sudo] password for sean: Reading package lists... DoneBuilding dependency treeReading state information... DoneThe following extra packages will be installed: keyutils libgssglue1 libnfsidmap2 libtirpc1 nfs-common rpcbindSuggested packages: open-iscsi watchdogThe following NEW packages will be installed: keyutils libgssglue1 libnfsidmap2 libtirpc1 nfs-common nfs-kernel-server rpcbind0 upgraded, 7 newly installed, 0 to remove and 159 not upgraded.Need to get 460 kB of archives.After this operation, 2,049 kB of additional disk space will be used.Do you want to continue? [Y/n] YGet:1 http://cn.archive.ubuntu.com/ubuntu/ trusty/main libgssglue1 amd64 0.4-2ubuntu1 [19.7 kB]Get:2 http://cn.archive.ubuntu.com/ubuntu/ trusty/main libnfsidmap2 amd64 0.25-5 [32.2 kB]Get:3 http://cn.archive.ubuntu.com/ubuntu/ trusty/main libtirpc1 amd64 0.2.2-5ubuntu2 [71.3 kB]Get:4 http://cn.archive.ubuntu.com/ubuntu/ trusty/main keyutils amd64 1.5.6-1 [33.6 kB]Get:5 http://cn.archive.ubuntu.com/ubuntu/ trusty-updates/main rpcbind amd64 0.2.1-2ubuntu2.2 [37.1 kB]Get:6 http://cn.archive.ubuntu.com/ubuntu/ trusty-updates/main nfs-common amd64 1:1.2.8-6ubuntu1.2 [181 kB]Get:7 http://cn.archive.ubuntu.com/ubuntu/ trusty-updates/main nfs-kernel-server amd64 1:1.2.8-6ubuntu1.2 [85.8 kB]Fetched 460 kB in 16s (27.2 kB/s) Selecting previously unselected package libgssglue1:amd64.(Reading database ... 168701 files and directories currently installed.)Preparing to unpack .../libgssglue1_0.4-2ubuntu1_amd64.deb ...Unpacking libgssglue1:amd64 (0.4-2ubuntu1) ...Selecting previously unselected package libnfsidmap2:amd64.Preparing to unpack .../libnfsidmap2_0.25-5_amd64.deb ...Unpacking libnfsidmap2:amd64 (0.25-5) ...Selecting previously unselected package libtirpc1:amd64.Preparing to unpack .../libtirpc1_0.2.2-5ubuntu2_amd64.deb ...Unpacking libtirpc1:amd64 (0.2.2-5ubuntu2) ...Selecting previously unselected package keyutils.Preparing to unpack .../keyutils_1.5.6-1_amd64.deb ...Unpacking keyutils (1.5.6-1) ...Selecting previously unselected package rpcbind.Preparing to unpack .../rpcbind_0.2.1-2ubuntu2.2_amd64.deb ...Unpacking rpcbind (0.2.1-2ubuntu2.2) ...Selecting previously unselected package nfs-common.Preparing to unpack .../nfs-common_1%3a1.2.8-6ubuntu1.2_amd64.deb ...Unpacking nfs-common (1:1.2.8-6ubuntu1.2) ...Selecting previously unselected package nfs-kernel-server.Preparing to unpack .../nfs-kernel-server_1%3a1.2.8-6ubuntu1.2_amd64.deb ...Unpacking nfs-kernel-server (1:1.2.8-6ubuntu1.2) ...Processing triggers for man-db (2.6.7.1-1ubuntu1) ...Processing triggers for ureadahead (0.100.0-16) ...ureadahead will be reprofiled on next rebootSetting up libgssglue1:amd64 (0.4-2ubuntu1) ...Setting up libnfsidmap2:amd64 (0.25-5) ...Setting up libtirpc1:amd64 (0.2.2-5ubuntu2) ...Setting up keyutils (1.5.6-1) ...Setting up rpcbind (0.2.1-2ubuntu2.2) ... Removing any system startup links for /etc/init.d/rpcbind ...rpcbind start/running, process 12074Processing triggers for ureadahead (0.100.0-16) ...Setting up nfs-common (1:1.2.8-6ubuntu1.2) ...Creating config file /etc/idmapd.conf with new versionCreating config file /etc/default/nfs-common with new versionAdding system user `statd" (UID 118) ...Adding new user `statd" (UID 118) with group `nogroup" ...Not creating home directory `/var/lib/nfs".statd start/running, process 12307gssd stop/pre-start, process 12341idmapd start/running, process 12399Processing triggers for ureadahead (0.100.0-16) ...Setting up nfs-kernel-server (1:1.2.8-6ubuntu1.2) ...Creating config file /etc/exports with new versionCreating config file /etc/default/nfs-kernel-server with new version * Not starting NFS kernel daemon: no exports.Processing triggers for libc-bin (2.19-0ubuntu6.7) ...Processing triggers for ureadahead (0.100.0-16) ...通过安装日志可以发现总共会安装keyutils,libgssglue1,libnfsidmap2,libtirpc1,nfs-common,nfs-kernel-server,rpcbind这7个包
从安装日志中我们可以发现NFS服务的配置文件为/etc/exports,并且这个文件在安装过程中已经生成好了,我们所要做的就是将NFS配置信息添加到这个文件中
sean@sean:~$ sudo vi /etc/exports# /etc/exports: the access control list for filesystems which may be exported#to NFS clients. See exports(5).## Example for NFSv2 and NFSv3:# /srv/homeshostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)## Example for NFSv4:# /srv/nfs4gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)/home/sean/shareDir 192.168.137.129(rw,no_root_squash,async)其中中的最后一行为新添加的NFS配置,NFS配置信息格式如下:
sean@sean:~$ sudo service nfs-kernel-server start * 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". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.x * Starting NFS kernel daemonsean@sean:~$ sudo service nfs-kernel-server statusnfsd runningsean@sean:~$ ps -ef|grep nfsdroot 10536 2 0 23:40 ?00:00:00 [nfsd4]root 10537 2 0 23:40 ?00:00:00 [nfsd4_callbacks]root 10539 2 0 23:40 ?00:00:00 [nfsd]root 10540 2 0 23:40 ?00:00:00 [nfsd]root 10541 2 0 23:40 ?00:00:00 [nfsd]root 10542 2 0 23:40 ?00:00:00 [nfsd]root 10543 2 0 23:40 ?00:00:00 [nfsd]root 10544 2 0 23:40 ?00:00:00 [nfsd]root 10545 2 0 23:40 ?00:00:00 [nfsd]root 10546 2 0 23:40 ?00:00:00 [nfsd]sean 105669528 0 23:41 pts/600:00:00 grep --color=auto nfsd通过命令可以看到文件夹已经被共享
sean@ubuntu:~$ sudo mount 192.168.137.128:/home/sean/shareDir /home/sean/sharedmount: wrong fs type, bad option, bad superblock on 192.168.137.128:/home/sean/shareDir,missing codepage or helper program, or other error(for several filesystems (e.g. nfs, cifs) you mightneed a /sbin/mount.<type> helper program)In some cases useful info is found in syslog - trydmesg | tail or so上网查看了一下错误原因,是因为客户端没有安装nfs-common包
sean@ubuntu:~$ dfFilesystem 1K-blocksUsed Available Use% Mounted onudev48880044887961% /devtmpfs999041456 984482% /run/dev/sda119478204 3745568 14720156 21% /none40 40% /sys/fs/cgroupnone 51200 51200% /run/locknone499512 1524993601% /run/shmnone102400 401023601% /run/user/dev/sr0 1044480 1044480 0 100% /media/sean/Ubuntu 14.04.4 LTS amd64192.168.137.128:/home/sean/shareDir 19478272 4288896 14176896 24% /home/sean/shared此时如果我们在128的/home/sean/shareDir目录下创建一个文件,在129的/home/sean/shared目录下可以看到一个名称相同的文件,并且文件的内容也相同