Sun公司开发NFS (Network File System)之初就是为了在不同linux/Unix系统之间共享文件或者文件夹。可以在本地通过网络挂载远程主机的共享文件,和远程主机交互。NFS共享存储对初学者来说不太好理解,我看到过一个很好的例子,假如有三台机器A、B、C,它们需要访问同一个目录,目录中都是图片,传统的做法是把这些图片分别放到A、B、C。但是使用NFS只需要放到A上,然后A共享给B和C即可。访问的时候,B和C是通过网络的方式去访问A上的那个目录的。
Linux环境配置NFS服务至少需要2台linux机子,并且保证能ping通。
NFS Server IP :10.1.101.188
NFS Client IP : 10.1.101.189
在NFS Server和NFS Client两台机子都需要装NFS包。(Red Hat Linux 用“yum”)Debian 和Ubuntu环境用"apt-get"。【以server端为例】
# apt-get install nfs-common nfs-kernel-server
NFS Server和NFS Client两台机子都启动nfs服务。
要启动portmap和nfs两个服务,并且portmap服务一定要先于nfs启动。【以server端为例】
root@nfsserver:~# /etc/init.d/portmap start Rather than invoking init scripts through /etc/init.d, use the service(8) utility, e.g. service portmap start Since the script you are attempting to invoke has been converted to an Upstart job, you may also use the start(8) utility, e.g. start portmap root@nfsserver:~# /etc/init.d/nfs-kernel-server start * Exporting directories for NFS kernel daemon... [ OK ] * Starting NFS kernel daemon
要共享一个目录,首先要在/etc/exports中加入。我们在根目录下新建一个目录/nfsshare,共享给客户端。
然后重启服务使配置生效(或者使用命令#exportfs -rv)。
root@nfsserver:~# mkdir /nfsshare
root@nfsserver:~# cat /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/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
/nfsshare 10.1.101.189(rw,sync,no_root_squash)
root@nfsserver:~# /etc/init.d/nfs-kernel-server restart
/nfsshare 10.1.101.189(rw,sync,no_root_squash)这句话意思是根分区下的/nfsshare目录被共享给IP“10.1.101.189”,并且有read和write(rw)权限,这里也可以用主机名(hostname)来代替IP。
exports文件中客户端主机地址
"客户端主机地址"字段可以使用多种形式表示主机地址
10.1.101.189:指定IP地址的主机
nfsclient.test.com:指定域名的主机
10.1.101.0/24:指定网段中的所有主机
*.test.com:指定域下的所有主机
*:所有主机
exports文件中配置选项
exports文件中的“配置选项”放在括号中,选项之间逗号分隔。
服务器端配置好后,在客户端挂载共享目录或分区。
第一步:查看NFS Server的共享文件
root@nfsclient:~# showmount -e 10.1.101.188 Export list for 10.1.101.188: /nfsshare 10.1.101.189
第二步:新建挂载点
root@nfsclient:~# mkdir -p /mnt/nfsshare
第三步:挂载,注意权限。
root@nfsclient:~# mount -t nfs 10.1.101.188:/nfsshare /mnt/nfsshare mount.nfs: access denied by server while mounting 10.1.101.188:/nfsshare
root@nfsserver:/# chmod 777 -R /nfsshare/
root@nfsserver:/# /etc/init.d/nfs-kernel-server restart * Stopping NFS kernel daemon [ OK ] * Unexporting directories for NFS kernel daemon... [ OK ] * Exporting directories for NFS kernel daemon... exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' specified for export "10.1.101.189:/nfsshare". Assuming default behaviour ('no_subtree_check'). NOTE: this default has changed since nfs-utils version 1.0.x [ OK ] * Starting NFS kernel daemon
root@nfsclient:~# mount -t nfs 10.1.101.188:/nfsshare /mnt/nfsshare
第四步,检查挂载是否成功:
root@nfsclient:~# mount |grep nfs 10.1.101.188:/nfsshare on /mnt/nfsshare type nfs (rw,vers=4,addr=10.1.101.188,clientaddr=10.1.101.189)
以上挂载只是暂时的,机子重启后就没有了。要永久挂载,可在"/etc/fsab"中配置。
即在/etc/fstab中加入一行:
10.1.101.188:/nfsshare /mnt/nfsshare nfs defaults 0 0
root@nfsclient:~# cat /etc/fstab # /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # # <file system> <mount point> <type> <options> <dump> <pass> proc /proc proc nodev,noexec,nosuid 0 0 # / was on /dev/xvda1 during installation UUID=0c681b37-97ed-4d10-bd79-8d5931c443f8 / ext4 errors=remount-ro 0 1 # swap was on /dev/xvda5 during installation UUID=9e2efc1b-ef13-4b7c-b616-34d2a62f04ea none swap sw 0 0 10.1.101.188:/nfsshare /mnt/nfsshare nfs defaults 0 0
然后执行命令:mount -a
root@nfsclient:~# mount -a
root@nfsclient:~# mount |grep nfs
10.1.101.188:/nfsshare on /mnt/nfsshare type nfs (rw,vers=4,addr=10.1.101.188,clientaddr=10.1.101.189)
在NFS Server端新建一个测试文件,检查在NFS Client端是否能获取到,反之亦然。
第一步,在NFS Server的共享目录中新建文件"nfsTestServer.txt"。
root@nfsserver:/nfsshare# cat nfsTestServer.txt This is a test file to test the working of NFS server setup. This file is created at nfs server end.
第二步,在NFS Client端无需刷新就可以看到“nfsTestServer.txt”文件。
root@nfsclient:/mnt/nfsshare# ls nfstest.txt
root@nfsclient:/mnt/nfsshare# cat nfsTestServer.txt
This is a test file to test the working of NFS server setup.
This file is created at nfs server end.
第三步,在NFS Client端,新建一个测试文件"nfsTestClient.txt"。
root@nfsclient:/mnt/nfsshare# cat nfsTestClient.txt This is a test file to test the working of NFS server setup. This file is created at nfs client end.
第四步,在NFSServer端无需刷新就可以看到“nfsTestClient.txt”文件。
root@nfsserver:/nfsshare# cat nfsTestClient.txt This is a test file to test the working of NFS server setup. This file is created at nfs client end.
文件共享完后执行umount命令卸载。
root@nfsclient:~# df -h -F nfs
Filesystem Size Used Avail Use% Mounted on
10.1.101.188:/nfsshare 19G 1.7G 17G 10% /mnt/nfsshare
root@nfsclient:~# umount /mnt/nfsshare
root@nfsclient:~# df -h -F nfs
df: no file systems processed
root@nfsclient:/mnt/nfsshare# umount /mnt/nfsshare umount.nfs: /mnt/nfsshare: device is busy
首先:注意不要在当前目录去执行umount,否则会报错。
root@nfsclient:~# umount /mnt/nfsshare umount.nfs: /mnt/nfsshare: device is busy
如果退出该目录还是不行,则判断是有一个进程在用该目录,找出。
root@nfsclient:~# fuser -m /mnt/nfsshare /mnt/nfsshare: 923c
找到使用该目录的进程:
root@nfsclient:~# ps aux |grep 923 root 923 0.0 0.3 21452 4036 pts/0 Ss+ 10:12 0:00 -bash root 1323 0.0 0.0 8104 924 pts/1 S+ 11:22 0:00 grep --color=auto 923
杀死进程
root@nfsclient:~# kill -9 923
然后就可以卸载了。
root@nfsclient:~# umount /mnt/nfsshare
showmount
exportfs管理工具可以对“exports”文件进行管理
root@nfsserver:~# exportfs -v /nfsshare 10.1.101.188(rw,wdelay,no_root_squash,no_subtree_check)
root@nfsserver:~# exportfs -a exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' specified for export "10.1.101.189:/nfsshare". Assuming default behaviour ('no_subtree_check'). NOTE: this default has changed since nfs-utils version 1.0.x
设置自动启动nfs服务
chkconfig --level 35 portmap on
chkconfig --level 35 nfs on
rpcinfo:查看rpc服务注册情况
-p:显示所有的端口和程序信息:
root@nfsserver:~# rpcinfo -p 10.1.101.188 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 100024 1 udp 44370 status 100024 1 tcp 35677 status 100003 2 tcp 2049 nfs 100003 3 tcp 2049 nfs 100003 4 tcp 2049 nfs 100227 2 tcp 2049 100227 3 tcp 2049 100003 2 udp 2049 nfs 100003 3 udp 2049 nfs 100003 4 udp 2049 nfs 100227 2 udp 2049 100227 3 udp 2049 100021 1 udp 40301 nlockmgr 100021 3 udp 40301 nlockmgr 100021 4 udp 40301 nlockmgr 100021 1 tcp 39133 nlockmgr 100021 3 tcp 39133 nlockmgr 100021 4 tcp 39133 nlockmgr 100005 1 udp 46249 mountd 100005 1 tcp 40795 mountd 100005 2 udp 42966 mountd 100005 2 tcp 41292 mountd 100005 3 udp 33508 mountd 100005 3 tcp 54892 mountd
推荐资源链接
https://help.ubuntu.com/community/SettingUpNFSHowTo