Sun公司開發NFS (Network File System)之初就是爲了在不一樣linux/Unix系統之間共享文件或者文件夾。能夠在本地經過網絡掛載遠程主機的共享文件,和遠程主機交互。NFS共享存儲對初學者來講不太好理解,我看到過一個很好的例子,假若有三臺機器A、B、C,它們須要訪問同一個目錄,目錄中都是圖片,傳統的作法是把這些圖片分別放到A、B、C。可是使用NFS只須要放到A上,而後A共享給B和C便可。訪問的時候,B和C是經過網絡的方式去訪問A上的那個目錄的。node
Linux環境配置NFS服務至少須要2臺linux機子,而且保證能ping通。linux
NFS Server IP :10.1.101.188ubuntu
NFS Client IP : 10.1.101.189安全
在NFS Server和NFS Client兩臺機子都須要裝NFS包。(Red Hat Linux 用「yum」)Debian 和Ubuntu環境用"apt-get"。【以server端爲例】bash
# 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)。app
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文件中客戶端主機地址ssh
"客戶端主機地址"字段可使用多種形式表示主機地址
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