Linux NFS搭建與錯誤提示解決
服務端設置:
[root@server ~]# cat /etc/redhat-release 查看操作系統版本信息
CentOS release 5.5 (Final)
[root@server ~]# uname -r 查看當前系統內核版本號
2.6.18-194.el5
[root@server ~]# uname -a
Linux server 2.6.18-194.el5 #1SMP Fri Apr 2 14:58:35 EDT 2010 i686 i686 i386 GNU/Linux
[root@server ~]# rpm -aqnfs-utils portmap
nfs-utils-1.0.9-44.el5
portmap-4.0-65.2.2.1
[root@server ~]# rpm -aq |egrep "nfs|portmap" 查看軟件包
nfs-utils-lib-1.0.8-7.6.el5
nfs-utils-1.0.9-44.el5
portmap-4.0-65.2.2.1
[root@server ~]#/etc/init.d/portmap start 啓動portmap服務
Starting portmap: [ OK ]
[root@server ~]# netstat -lnt|grep 111 查看端口,確認portmap服務啓動
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
[root@server ~]#/etc/init.d/nfs start 啓動 nfs服務
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS daemon: [ OK ]
Starting NFS mountd: [ OK ]
[root@server ~]#/etc/init.d/nfs status
rpc.mountd (pid 12083) isrunning...
nfsd (pid 12080 12079 1207812077 12076 12075 12074 12073) is running...
rpc.rquotad (pid 12068) isrunning...
[root@server ~]# ps -ef |grep rpc
root 2603 7 0 May13 ? 00:00:00 [rpciod/0]
root 2609 1 0 May13 ? 00:00:00 rpc.statd
root 2641 1 0 May13 ? 00:00:00 rpc.idmapd
rpc 12042 1 0 01:00 ? 00:00:00 portmap
root 12068 1 0 01:01 ? 00:00:00 rpc.rquotad
root 12083 1 0 01:01 ? 00:00:00 rpc.mountd
root 12120 11924 0 01:02 pts/0 00:00:00 grep rpc
[root@server /]# mkdir /data 創建共享目錄
[root@server /]# cd /data
[root@server data]#touch yubing.txt
[root@server data]# ll
total 4
-rw-r--r-- 1 root root 0 May 1401:11 yubing.txt
[root@server data]# echo"my name is yubing">yubing.txt
[root@server data]# catyubing.txt
my name is yubing
[root@server ~]# vi /etc/exports
/data 10.0.0.0/24(rw,sync)
格式爲:
共享目錄+指定共享IP地址或地址段(rw,sync)
[root@server ~]#/etc/init.d/nfs reload 重新加載
[root@server ~]#cat /var/lib/nfs/etab
/data 10.0.0.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,mapping=identity,anonuid=65534,anongid=65534)
[root@server ~]# showmount -e 127.0.0.1
Export list for 127.0.0.1:
/data1 10.0.0.0/24
客戶端設置:
[root@client ~]#/etc/init.d/portmap start
啓動 portmap:[確定]
[root@client ~]# mkdir /yubing
[root@client ~]# cd /yubing
[root@client yubing]# ll
總計 0
[root@client ~]# mount -t nfs10.0.0.152:/data /yubing 掛載共享目錄
[root@client ~]# cd /yubing
[root@client yubing]# ll
總計 4
-rw-r--r-- 1 root root 0 05-1401:11 yubing.txt
共享目錄權限設置:
[root@client yubing]# ll
總計 8
-rw-r--r-- 1 root root 18 05-1401:21 yubing.txt
[root@client yubing]# cat yubing.txt
my name is yubing
[root@client yubing]# echo"aaaaa">yubing.txt
-bash: yubing.txt: 權限不夠
[root@client yubing]# touch yubing1.txt 無法新建文件
touch: 無法觸碰“yubing1.txt”: 權限不夠
[root@client yubing]# ls -ld/yubing
drwxr-xr-x 2 root root 409605-14 01:11 /yubing
[root@client yubing]# rm yubing.txt
rm:是否刪除有寫保護的一般文件“yubing.txt”? y 無法刪除
rm: 無法刪除“yubing.txt”: 權限不夠
來到服務端進行權限更改:
[root@server data]# chown nfsnobody.nfsnobody /data
[root@server data]# ls -ld/data
drwxr-xr-x 2 nfsnobody nfsnobody 4096 May 14 01:11 /data
回到客戶端查看權限:
[root@client yubing]# ls -ld/yubing
drwxr-xr-x 2 nfsnobody nfsnobody 4096 05-14 01:11 /yubing
[root@client yubing]# ll
總計 8
-rw-r--r-- 1 root root 18 05-1401:21 yubing.txt
[root@client yubing]# touchyubing1.txt 新建成功
[root@client yubing]# ll
總計 12
-rw-r--r-- 1 nfsnobody nfsnobody 0 05-14 01:29 yubing1.txt
-rw-r--r-- 1 root root 18 05-14 01:21 yubing.txt
[root@client yubing]# echo"aaaaa">yubing.txt 此文件用戶爲root,無寫入權限
-bash: yubing.txt: 權限不夠
[root@client yubing]# echo"aaaaa">yubing1.txt
[root@client yubing]# cat yubing1.txt
aaaaa
[root@client yubing]# rm yubing.txt
rm:是否刪除有寫保護的一般文件“yubing.txt”? y
[root@client yubing]# ll
總計 8
-rw-r--r-- 1 nfsnobody nfsnobody 6 05-14 01:30 yubing1.txt
[root@client yubing]#
爲什麼chown nfsnobody.nfsnobody /data後客戶端就有權限了呢?
在服務端我們可以看到:
[root@server ~]# cat /etc/exports
/data 10.0.0.0/24(rw,sync)
[root@server ~]# cat /var/lib/nfs/etab
/data 10.0.0.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,mapping=identity,anonuid=65534,anongid=65534)
[root@server ~]# grep nfsnobody /etc/passwd
nfsnobody:x:65534:65534:AnonymousNFS User:/var/lib/nfs:/sbin/nologin
(1)rw:可讀寫權限。
(2)ro:只讀權限。
(3)sync:同步寫入資料到內存與硬盤中。
(4)async:資料會先暫存於內存中,而非直接寫入硬盤。
(5)no_root_squash:當登錄NFS主機使用共享目錄的使用者是root時,其權限將被轉換成爲匿名使
用者,通常它的UID與GID都會變成nobody身份。
(6)root_squash;如果登錄NFS主機使用共享目錄的使用者是root,那麼對於這個共享的目錄來說,
它具有root的權限。
(7)all_squash:忽略登錄NFS使用者的身份,其身份都會被轉換爲匿名使用者,通常即nobody。
(8)anonuid:通常爲nobody,也可以自行設定這個UID的值,UID必須存在於/etc/passwd中。
(9)anongid:同anonuid,但是變爲Group ID。
再來看客戶端:
[root@client yubing]# grep nfsnobody /etc/passwd
nfsnobody:x:65534:65534:AnonymousNFS User:/var/lib/nfs:/sbin/nologin
所以,我們可以 vi /etc/exports
/data 10.0.0.0/24(rw,sync,all_squash,anonuid=888,anongid=999)
來指定用戶的UID和GID,當然服務端需存在UID、GID爲888的用戶,客戶端只要存在UID、GID爲888的用戶,就可以擁有服務端對共享目錄設置權限後的各種權限了。
錯誤提示解決:
錯誤提示1:
[root@client yubing]# mount -tnfs 10.0.0.152:/data /yubing
mount: mountto NFS server '10.0.0.152' failed: System Error: Connection refused.
掛載失敗,查看服務端服務是否正常運行
[root@server ~]#/etc/init.d/portmap status
portmap is stopped
[root@server ~]#/etc/init.d/portmap start
Starting portmap: [ OK ]
[root@server ~]#/etc/init.d/nfs status
rpc.mountd is stopped
nfsd is stopped
rpc.rquotad is stopped
[root@server ~]#/etc/init.d/nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS daemon: [ OK ]
Starting NFS mountd: [ OK ]
[root@client yubing]# mount -tnfs 10.0.0.152:/data /yubing
[root@client yubing]# df -h
文件系統容量已用可用已用% 掛載點
/dev/sda3 19G 1.6G 16G 9% /
/dev/sda1 122M 12M 104M 10% /boot
tmpfs 506M 0 506M 0% /dev/shm
10.0.0.152:/data 7.7G 1.6G 5.7G 22% /yubing 掛載成功
錯誤提示2:
[root@server ~]#/etc/init.d/nfs start 啓動nfs失敗
Starting NFSservices: [ OK ]
Starting NFSquotas: Cannot register service: RPC: Unable to receive; errno = Connectionrefused
rpc.rquotad:unable to register (RQUOTAPROG, RQUOTAVERS, udp).
[FAILED]
Starting NFSdaemon: [FAILED]
[root@server ~]#/etc/init.d/portmap status
portmap is stopped
[root@server ~]#/etc/init.d/portmap start
Starting portmap: [ OK ]
[root@server ~]#/etc/init.d/nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS daemon: [ OK ]
Starting NFS mountd: [ OK ]
必須先啓動portmap服務後再啓動nfs服務,因爲NFS及其輔助程序都是基於RPC(Remote Procedure Call)協議的(使用的端口爲111),所以首先要確保系統中運行了portmap服務。
錯誤提示3:
[root@server ~]#/etc/init.d/nfs reload
exportfs: Nooptions for /data10.0.0.0/24(rw,sync) : suggest (sync) to avoid warning
[root@server ~]#/etc/init.d/nfs reload
exportfs: No options for/data10.0.0.0/24(rw,sync) : suggest (sync) to avoid warning
[root@server ~]# vi /etc/exports
/data10.0.0.0/24(rw,sync) 中間沒空格
改爲:
/data 10.0.0.0/24(rw,sync)
[root@server ~]#/etc/init.d/nfs reload 重啓服務成功
[root@server ~]#
錯誤提示4:
[root@client yubing]# df -h
文件系統容量已用可用已用% 掛載點
/dev/sda3 19G 1.6G 16G 9% /
/dev/sda1 122M 12M 104M 10% /boot
tmpfs 506M 0 506M 0% /dev/shm
(卡死,不能做任何操作)
[root@client ~]# cd /yubing
(依然卡死)
客戶端掛載了服務端的共享目錄,服務端如果關閉,就會出現卡死
只能重新連接後取消掛載
[root@client ~]# umount -lf/yubing
[root@client ~]# df -h
文件系統容量已用可用已用% 掛載點
/dev/sda3 19G 1.6G 16G 9% /
/dev/sda1 122M 12M 104M 10% /boot
tmpfs 506M 0 506M 0% /dev/shm
[root@client ~]#
錯誤提示5
[root@syt ~]# mount -t nfs10.0.0.242:/data/r_shared /data/b_r
mount:10.0.0.242:/data/r_shared failed, reason given by server: Permission denied
NFS服務端/data/r_shared目錄不存在。檢查共享目錄是/date/r_shared
如果大家有出現其他問題,希望能隨時給我留言~謝謝啦~