一、安裝並開啓nfs服務
服務端:
[root@server ~]# yum install nfs-utils -y
[root@server ~]# systemctl start nfs
客戶端:
[root@client ~]# showmount -e 172.25.71.1
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
原因:
在服務端沒有加入火牆策略
[root@server ~]# firewall-cmd --permanent --add-service=nfs
success
[root@server ~]# firewall-cmd --permanent --add-service=rpc-bind
success
[root@server ~]# firewall-cmd --permanent --add-service=mountd
success
[root@server ~]# firewall-cmd --reload
success
[root@server ~]# systemctl restart firewalld.service
測試:
[root@client ~]# showmount -e 172.25.71.1
Export list for 172.25.71.1: ##此時服務端還沒有共享的目錄
做出共享目錄,或者把服務端的某一目錄共享給外界使用
一、安裝並開啓nfs服務
服務端:
[root@server ~]# yum install nfs-utils -y
[root@server ~]# systemctl start nfs
客戶端:
[root@client ~]# showmount -e 172.25.71.1
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
原因:
在服務端沒有加入火牆策略
[root@server ~]# firewall-cmd --permanent --add-service=nfs
success
[root@server ~]# firewall-cmd --permanent --add-service=rpc-bind
success
[root@server ~]# firewall-cmd --permanent --add-service=mountd
success
[root@server ~]# firewall-cmd --reload
success
[root@server ~]# systemctl restart firewalld.service
測試:
[root@client ~]# showmount -e 172.25.71.1
Export list for 172.25.71.1: ##此時服務端還沒有共享的目錄
二、做出共享目錄,或者把服務端的某一目錄共享給外界使用
[root@server ~]# mkdir /zhaoyan/nfs -p
[root@server ~]# cat /etc/exports ##寫入共享的某一目錄
/zhaoyan/nfs *(sync) ##表示將次目錄進行共享,並且對所有人開放
[root@server ~]# exportfs -rv ##在此服務中,不能重啓此服務,否則會進行客戶端訪問的卡頓,
exporting *:/zhaoyan/nfs ##用此來進行策略的更新
測試:
[root@client ~]# showmount -e 172.25.71.1
Export list for 172.25.71.1:
/zhaoyan/nfs *
並進行掛載使用:
[root@client ~]# mount 172.25.71.1:/zhaoyan/nfs /mnt/
三、實現客戶端的自動掛載與自動卸載功能
此在客戶端進行實現:
在客戶端進行安裝此軟件
[root@client ~]# yum install autofs -y
開啓此軟件,會自動增加一個/net的目錄,
[root@client ~]# systemctl start autofs
[root@client ~]# cd /net/
[root@client net]# ls ##/net此目錄雖不顯示,但可以直接進去共享的服務器的ip
[root@client net]# cd 172.25.71.1
[root@client 172.25.71.1]# ls
zhaoyan
關閉此目錄時:/etc/sysconfig/autofs此文件會默認卸載時間時300秒,可以進行修改
[root@client ~]# systemctl stop autofs
[root@client ~]# cd /net/
-bash: cd: /net/: No such file or directory
##開啓此服務就可以進入掛載的目錄中,關閉此服務實現卸載,實現自動卸載與掛載功能
四、修改掛載的目錄
[root@client ~]# vim /etc/auto.master
5 # For details of the format look at autofs(5).
6 #
7 /misc /etc/auto.misc
8 /zhaoyan/linux /etc/auto.nfs ##此爲添加的策略,/zhaoyan/linux寫入的是,掛載點的上層目錄
9 # ##將寫入的要掛載的服務端寫入此文件,該文件沒有,需要自己建立
[root@client nfs]# cat /etc/auto.nfs
nfs -rw,noatime 172.25.71.1:/zhaoyan/nfs ##要掛載的點爲/zhaoyan/linux/nfs
測試:(在此服務開啓的情況下)
[root@client nfs]# pwd
/zhaoyan/linux/nfs
退出此目錄:
##此實驗中的/zhaoyan/linux/nfs是客戶端自己打開此服務建立的,與/net/目錄是一樣的,如果自己建立/zhaoyan/linux/nfs目錄的話,會與系統自己建立的目錄進行衝突。
五、客戶端自行進行建立與刪除文件
服務端:
因爲客戶端將此共享目錄掛載在自己的目錄上,若自行建立文件,實際是在服務端的該共享目錄上建立文件的,所以需要服務端分配所要的權限
[root@server ~]# chmod 777 /zhaoyan/nfs/ ##給共享的此目錄一個777的權限
[root@server ~]# ls -ld /zhaoyan/nfs/
drwxrwxrwx. 2 root root 6 Dec 10 06:53 /zhaoyan/nfs/
/zhaoyan/nfs *(sync,rw)
測試:
172.25.71.1:/zhaoyan/nfs 10473984 3149568 7324416 31% /zhaoyan/linux/nfs ##已經成功掛載
[root@client nfs]# touch file ##建立文件沒有報錯
[root@client nfs]# ls
file
六、對建立的人給相應的用戶
[root@client nfs]# ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 10 07:23 file ##默認將建立的用戶壓縮成爲nfsnobody
服務端:
[root@server ~]# useradd zhaoyan ##建立一個用戶
[root@server ~]# id zhaoyan
uid=1001(zhaoyan) gid=1001(zhaoyan) groups=1001(zhaoyan)
[root@server ~]# vim /etc/exports
[root@server ~]# exportfs -rv
exporting *:/zhaoyan/nfs
[root@server ~]# cat /etc/exports
/zhaoyan/nfs *(sync,rw,anonuid=1001,anongid=1001) ##讓客戶端建立文件的人使用的身份是1001
測試:
[root@client ~]# cd /zhaoyan/linux/nfs
[root@client nfs]# ls
file
[root@client nfs]# touch file1
[root@client nfs]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 10 07:23 file
-rw-r--r-- 1 1001 1001 0 Dec 10 07:32 file1
在客戶端顯示使用的是1001的身份,表示此客戶端1001的uid並沒有人使用
[root@client nfs]# cat /etc/passwd| grep 1001
[root@client nfs]#
[root@client nfs]# useradd haha
[root@client nfs]# cat /etc/passwd| grep 1001
haha:x:1001:1001::/home/haha:/bin/bash
此時在此進行測試:
[root@client nfs]# touch file2
[root@client nfs]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 10 07:23 file
-rw-r--r-- 1 haha haha 0 Dec 10 07:32 file1 ##發現建立文件的用用戶均成爲了haha用戶
-rw-r--r-- 1 haha haha 0 Dec 10 07:36 file2
讓172.25.71.2這臺主機在使用root用戶登陸時,不進行身份轉換
[root@server ~]# cat /etc/exports
/zhaoyan/nfs 172.25.71.2(sync,rw,no_root_squash) *(sync,ro)
[root@server ~]# exportfs -rv
exporting 172.25.71.2:/zhaoyan/nfs
exporting *:/zhaoyan/nfs
測試:
no_root_squash ##相當於該客戶端的root用戶對該共享目錄有極大的權限,極度不安全的一個參數
###在nfs時,經常在掛載時就出現這樣的問題,此問題出現的原因是服務端的系統版本與所下載的軟件的版本是不一致的,這樣就會出現掛載不上的問題
[root@client ~]# mount 172.25.71.1:/zhaoyan/nfs /mnt/
mount.nfs: an incorrect mount option was specified