NFS(Network File System)即網絡文件系統,是FreeBSD支持的文件系統中的一種,它容許網絡中的計算機之間經過TCP/IP網絡共享資源。在NFS的應用中,本地NFS的客戶端應用能夠透明地讀寫位於遠端NFS服務器上的文件,就像訪問本地文件同樣
NFS的優勢:
1. 節省本地存儲空間,將經常使用的數據存放在一臺NFS服務器上且能夠經過網絡訪問,那麼本地終端將能夠減小自身存儲空間的使用。
2. 用戶不須要在網絡中的每一個機器上都建有Home目錄,Home目錄能夠放在NFS服務器上且能夠在網絡上被訪問使用。
3. 一些存儲設備如軟驅、CDROM和Zip(一種高儲存密度的磁盤驅動器與磁盤)等均可以在網絡上被別的機器使用。這能夠減小整個網絡上可移動介質設備的數量。
php
下面開始NFS的安裝:html
1、安裝準備linux
關閉iptables service iptables stop chkconfig iptables off 設置selinux sed -i's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
1.安裝nfs服務 [root@allentuns~]# yum -y install nfs-utils rpcbind
2.啓動nfs服務 [root@allentuns~]# /etc/init.d/rpcbind start Starting rpcbind: [ OK ] [root@allentuns~]# /etc/init.d/nfs start Starting NFS services: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] Starting RPC idmapd: [ OK ]
3.建立共享目錄 [root@allentuns~]# mkdir -pv /web/htdocs mkdir: created directory `/web' mkdir: created directory `/web/htdocs' [root@allentuns~]# ll -d /web/htdocs/ drwxr-xr-x. 2 root root 4096 May 3 18:50 /web/htdocs/
4.添加共享目錄其它用戶有寫權限 [root@allentuns~]# chmod o+w /web/htdocs [root@allentuns~]# ll -d /web/htdocs/ drwxr-xrwx. 2 root root 4096 May 3 18:50 /web/htdocs/
5.修改配置文件,設置共享目錄或者分區 [root@allentuns~]# vim /etc/exports #增長一行 /web/htdocs 192.168.1.5(rw)
6.重讀exports文件,使馬上生效 [root@allentuns~]# exportfs -r
7.查看服務器共享的文件。前提須要域名解析,不然報錯 [root@allentuns~]# uname -n allentuns [root@allentuns~]# echo "127.0.0.1 allentuns" >> /etc/hosts [root@allentuns~]# showmount -e Export list for allentuns: /web/htdocs 192.168.1.5
1.安裝showmount [root@allentuns ~]# yum -y install showmount
2.查看服務器共享 [root@allentuns ~]# showmount -e 192.168.1.4 Export list for 192.168.1.4: /web/htdocs 192.168.1.5
3.客戶端掛載到本地 [root@allentuns ~]# ll /mnt/ total 0 [root@allentuns ~]# mount -t nfs 192.168.1.4:/web/htdocs /mnt/
服務器端----->>客戶端 1.服務器端 [root@allentuns~]# ifconfig |grep "Bcast" inetaddr:192.168.1.4 Bcast:192.168.1.255 Mask:255.255.255.0 [root@allentuns~]# cd /web/htdocs/ [root@allentunshtdocs]# ls [root@allentunshtdocs]# cat /etc/issue >> index.html [root@allentunshtdocs]# ll total4 -rw-r--r--. 1 rootroot 47 May 3 19:06 index.html
2.查看是否同步到客戶端 [root@allentuns~]# ifconfig |grep "Bcast" inet addr:192.168.1.5 Bcast:192.168.1.255 Mask:255.255.255.0 [root@allentuns~]# cd /mnt/ [root@allentuns mnt]# ll total 4 -rw-r--r--. 1 root root 47 May 3 19:06 index.html
客戶端----->>服務器端 1.客戶端建立文件 [root@allentuns mnt]# ifconfig |grep "Bcast" inet addr:192.168.1.5 Bcast:192.168.1.255 Mask:255.255.255.0 [root@allentuns mnt]# touch test.php [root@allentuns mnt]# ll total 4 -rw-r--r--. 1 root root 47 May 3 19:06 index.html -rw-r--r--. 1 nfsnobody nfsnobody 0 May 3 19:10 test.php
2.服務器端看是否已經同步 [root@allentuns htdocs]# ifconfig |grep "Bcast" inet addr:192.168.1.4 Bcast:192.168.1.255 Mask:255.255.255.0 [root@allentuns htdocs]# ll total 4 -rw-r--r--. 1 root root 47 May 3 19:06 index.html -rw-r--r--. 1 nfsnobody nfsnobody 0 May 3 19:10 test.php 以上兩點證實:nfs服務器端到客戶端是雙向同步的!!!
如下實驗你們用主機名來區分服務器端和客戶端,
服務器端爲NFS_Server ip-192.168.1.4;
客戶端爲 NFS_Client ip-192.168.1.5;web
實例一: 將/tmp分享給你們使用,讓全部的人均可以存取,讓root寫入檔案仍是具備root的權限 [NFS Server] [root@NFS_Server~]# ll -d /tmp/ #目錄tmp文件系統自己具備讀寫權限 drwxrwxrwt. 3 root root 4096 May 3 21:39 /tmp/ [root@NFS_Server~]# vim /etc/exports /tmp 192.168.1.5(rw,root_squash) [root@NFS_Server~]# exportfs -r #從新載入配置文件 [root@NFS_Server~]# ll /tmp total 0 -rw-------. 1 root root 0 Apr 23 15:05 yum.log [NFS Client] [root@NFS_Client~]# mkdir /share1 #建立掛載目錄 [root@NFS_Client~]# ll /share1/ total 0 [root@NFS_Client~]# mount -t nfs 192.168.1.4:/tmp/ /share1/ #掛載 [root@NFS_Client share1]# ll #已同步到客戶端的/share1目錄 total 0 -rw-------. 1 root root 0 Apr 23 15:05 yum.log [root@NFS_Client share1]# whoami #查看當前用戶 root [root@NFS_Client share1]# touch a.sh #以root用戶建立一個測試文件 [root@NFS_Client share1]# ll #查看a.sh的宿主宿組都是root total 0 -rw-r--r--. 1 root root 0 May 3 2014 a.sh -rw-------. 1 root root 0 Apr 23 15:05 yum.log [NFS Server] [root@NFS_Server tmp]# ll #查看a.sh是否同步,查看a.sh的宿主宿組是不是root total 0 -rw-r--r--. 1 root root 0 May 3 22:15 a.sh -rw-------. 1 root root 0 Apr 23 15:05 yum.log ---End---
實驗一總結:主要的就是共享權限no_root_squash 要想明白no_root_squash就要先明白root_squash的意思 root_squash:若是客戶端是以root身份進行對nfs分區操做,那麼會被root_squash壓縮成nfsnobody。 no_root_squash:與root_squash意思相反,就是不壓縮,仍然是以root用戶進行進行操做nfs分區,安全性沒有保證 例題2、 同一目錄針對不一樣範圍開放不一樣的權限 咱們將一個公共的目錄/www/onair 公開出去,可是咱們只限定個人局域網絡192.168.1.0/24這個網且加入Allentunsgroup的用戶纔可以讀寫,其它來源則只能讀取 [ON Server] [root@NFS_Server ~]# mkdir /www/onair -pv mkdir: created directory `/www' mkdir: created directory `/www/onair' [root@NFS_Server~]# touch /www/onair/a.sh [root@NFS_Server~]# groupadd -g 500 Allentunsgroup [root@NFS_Server~]# useradd -g Allentunsgroup -r 500 Allentuns [root@NFS_Server~]# setfacl -m g:Allentunsgroup:rwx /www/onair #設置Allentunsgroup組對/www/onair文件的讀寫執行權限 [root@NFS_Server~]# ll -d /www/onair/ drwxrwxr-x+ 2 root root 4096 May 4 01:45 /www/onair/ [root@NFS_Server~]# cat /etc/group |grep Allentunsgroup #查看對應的gid後面會用到 Allentunsgroup:x:500: [root@NFS_Server~]# vim /etc/exports /www/onair 192.168.1.0/24(rw) *(ro) [root@NFS_Server~]# exportfs -r [ON Client] [root@NFS_Client~]# mkdir /usr/local/onair [root@NFS_Client~]# showmount -e 192.168.1.4 Export list for 192.168.1.4: /www/onair (everyone) [root@NFS_Client~]# mount -t nfs 192.168.1.4:/www/onair /usr/local/onair/ [root@NFS_Client~]# cd /usr/local/onair/ [root@NFS_Client onair]# ll total 0 -rw-r--r--. 1 root root 0 May 4 01:45 a.sh [root@NFS_Client onair]# whoami root [root@NFS_Clien onair]# touch b.sh #拒絕 touch: cannot touch `b.sh': Permission denied root用戶拒絕寫入 而後建立一個新帳號,並加入到Allentunsgroup組中測試是否有寫入權限 [root@NFS_Client~]# groupadd -g 500 Allentunsgroup [root@NFS_Client~]# cat /etc/group |grep Allentunsgroup Allentunsgroup:x:500: [root@NFS_Client~]# useradd -g Allentunsgroup scott [root@NFS_Client~]# id scott uid=500(scott) gid=500(Allentunsgroup) groups=500(Allentunsgroup) [root@NFS_Client~]# passwd scott Changing password for user scott. New password: BAD PASSWORD: it is too short BAD PASSWORD: is too simple Retype new password: passwd: all authentication tokens updated successfully. [root@NFS_Client onair]# su scott [scott@NFS_Client onair]$ whoami scott [scott@NFS_Client onair]$ ll total 0 -rw-r--r--. 1 root root 0 May 4 01:45 a.sh [scott@NFS_Client onair]$ touch b.sh #寫入成功 [scott@NFS_Client onair]$ ll total 0 -rw-r--r--. 1 root root 0 May 4 01:45 a.sh -rw-r--r--. 1 nobody Allentunsgroup 0 May 4 2014 b.sh [ON Server] [root@NFS_Server ~]# ll /www/onair/ total 0 -rw-r--r--. 1 root root 0 May 4 01:45 a.sh -rw-r--r--. 1 Allentuns Allentunsgroup 0 May 4 02:11 b.sh 已經同步到Server端,而且同步到server端的用戶是Allentuns,用戶組爲Allentunsgroup setfacl的用法 setfacl命令能夠用來細分linux下的文件權限。 chmod命令能夠把文件權限分爲u,g,o三個組, 而setfacl能夠對每個文件或目錄設置更精確的文件權限。 比較經常使用的用法以下: setfacl –m u:apache:rwx file 設置apache用戶對file文件的rwx權限 setfacl –m g:market:rwx file 設置market用戶組對file文件的rwx權限 setfacl –x g:market file 刪除market組對file文件的全部權限 getfacl file 查看file文件的權限 Error1 [root@NFS_Server onair]# service rpcbind stop shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory chdir: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory Stopping rpcbind: 解決辦法 退出當前目錄,從新執行此命令 ---End---