目錄
一裁眯、NFS介紹
二、NFS服務(wù)端安裝配置
三讳癌、NFS配置選項(xiàng)
一穿稳、NFS介紹
NFS:Network File System的縮寫,是一個(gè)基于網(wǎng)絡(luò)的文件系統(tǒng)晌坤,使用廣泛逢艘。 NFS最早由Sun公司開發(fā)旦袋,分2,3,4三個(gè)版本,2和3由Sun起草開發(fā)埋虹,4.0開始Netapp公司參與并主導(dǎo)開發(fā)猜憎,最新為4.1版本。NFS數(shù)據(jù)傳輸基于RPC協(xié)議搔课,RPC為Remote Procedure Call的簡寫胰柑。NFS的服務(wù)端和客戶端本身無法直接通信,需要借助RPC來完成爬泥。
NFS應(yīng)用場景是:A,B,C三臺機(jī)器上需要保證被訪問到的文件是一樣的柬讨,A共享數(shù)據(jù)出來,B和C分別去掛載A共享的數(shù)據(jù)目錄袍啡,從而B和C訪問到的數(shù)據(jù)和A上的一致踩官。可以通過NFS實(shí)現(xiàn)服務(wù)器間實(shí)時(shí)的數(shù)據(jù)共享境输。
-
NFS原理圖
NFS服務(wù)端啟動(dòng)NFS服務(wù)蔗牡,NFS服務(wù)作為服務(wù)在系統(tǒng)中啟動(dòng),但不監(jiān)聽任何端口嗅剖,監(jiān)聽端口完成TCP/IP通信的工作由RPCbind服務(wù)產(chǎn)生的RPC協(xié)議實(shí)現(xiàn)辩越。服務(wù)端借助RPC協(xié)議為客戶端提供服務(wù),該協(xié)議由RPC服務(wù)實(shí)現(xiàn)信粮。RPC服務(wù)在Centos5及之前被稱為portmap黔攒,6及以后的版本被稱為RPCbind。
RPC服務(wù)監(jiān)聽111端口强缘,當(dāng)通信時(shí)督惰,
- 先由客戶端和服務(wù)端的RPC協(xié)議相互通信,
- NFS服務(wù)端會(huì)在RPC協(xié)議中注冊一個(gè)端口旅掂,
- 服務(wù)端的NFS服務(wù)告訴本地RPC自己通信的端口赏胚,
- 服務(wù)端RPC服務(wù)告訴客戶端的RPC服務(wù)該通信端口
- 最后NFS客戶端主機(jī)與服務(wù)端NFS的端口通信實(shí)現(xiàn)數(shù)據(jù)傳輸。
二商虐、NFS服務(wù)端安裝配置
- NFS服務(wù)需要安裝兩個(gè)包(nfs-utils和rpcbind)
//在安裝nfs-utills時(shí)會(huì)自一并安裝rpcbind
[root@minglinux-01 ~] yum install -y nfs-utils rpcbind
- 客戶端也需要安裝nfs-utills
[root@minglinux-02 ~] yum install -y nfs-utils
- 先修改配置文件(默認(rèn)該文件為空)
[root@minglinux-01 ~] ls /etc/exports
/etc/exports
[root@minglinux-01 ~] cat !$
cat /etc/exports
[root@minglinux-01 ~] vim !$
vim /etc/exports
//寫入以下內(nèi)容
1 /home/nfstestdir 192.168.162.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
這個(gè)配置文件就一行觉阅,共分為三部分。第一部分是本地要共享出去的目錄称龙,第二部分是允許訪問的主機(jī)(可以是一個(gè)IP留拾,也可以是一個(gè)IP段),第三部分就是小括號里面的一些權(quán)限選項(xiàng)鲫尊。此處配置的含義為:共享的目
錄為/home/nfstestdir痴柔,信任的主機(jī)為192.168.162.0/24這個(gè)網(wǎng)段,權(quán)限為讀/寫疫向,同步模式咳蔚,限定所有使用者豪嚎,并且限定的uid和gid都為1000。
- 創(chuàng)建共享目錄并修改其權(quán)限
[root@minglinux-01 ~] mkdir /home/nfstestdir
[root@minglinux-01 ~] chmod 777 /home/nfstestdir
//為了避免由于權(quán)限導(dǎo)致的報(bào)錯(cuò)谈火,將共享目錄改為777權(quán)限
- 啟動(dòng)rpcbind
[root@minglinux-01 ~] systemctl start rpcbind.service
[root@minglinux-01 ~] netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 758/sshd
tcp6 0 0 :::3306 :::* LISTEN 1235/mysqld
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::*
[root@minglinux-01 ~] ps aux |grep rpcbind
rpc 2132 0.0 0.0 69220 1204 ? Ss 22:53 0:00 /sbin/rpcbind -w
root 2136 0.0 0.0 112720 984 pts/1 S+ 22:55 0:00 grep --color=auto rpcbind
- 啟動(dòng)NFS服務(wù)
[root@minglinux-01 ~] systemctl start nfs
[root@minglinux-01 ~] ps aux| grep nfs
root 1518 0.0 0.0 0 0 ? S< 23:07 0:00 nfsd4_callbacks]
root 1524 0.0 0.0 0 0 ? S 23:07 0:00 nfsd]
root 1525 0.0 0.0 0 0 ? S 23:07 0:00 nfsd]
root 1526 0.0 0.0 0 0 ? S 23:07 0:00 nfsd]
root 1527 0.0 0.0 0 0 ? S 23:07 0:00 nfsd]
root 1528 0.0 0.0 0 0 ? S 23:07 0:00 nfsd]
root 1529 0.0 0.0 0 0 ? S 23:07 0:00 nfsd]
root 1530 0.0 0.0 0 0 ? S 23:07 0:00 nfsd]
root 1531 0.0 0.0 0 0 ? S 23:07 0:00 nfsd]
root 1547 0.0 0.0 112720 984 pts/0 S+ 23:07 0:00 grep --color=auto nfs
[root@minglinux-01 ~] ps aux| grep rpc //啟動(dòng)nfs服務(wù)時(shí)會(huì)自動(dòng)啟動(dòng)rpc相關(guān)的服務(wù)侈询,若rpc相關(guān)服務(wù)未啟動(dòng)則無法正常使用nfs
root 551 0.0 0.0 0 0 ? S< 23:03 0:00 rpciod]
rpc 585 0.0 0.0 69220 1528 ? Ss 23:03 0:00 /sbin/rpcbind -w
rpcuser 1504 0.0 0.0 42420 1756 ? Ss 23:07 0:00 /usr/sbin/rpc.statd
root 1512 0.0 0.0 42608 944 ? Ss 23:07 0:00 /usr/sbin/rpc.mountd
root 1513 0.0 0.0 45924 548 ? Ss 23:07 0:00 /usr/sbin/rpc.idmapd
root 1550 0.0 0.0 112724 980 pts/0 S+ 23:08 0:00 grep --color=auto rpc
[root@minglinux-01 ~] systemctl enable nfs //設(shè)置nfs服務(wù)開機(jī)啟動(dòng)
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
客戶端與服務(wù)端均需要啟動(dòng)rpcbind,但僅服務(wù)端需要啟動(dòng)nfs
- 客戶端啟動(dòng)rpcbind
[root@minglinux-02 ~] ps aux |grep rpcbind
root 1871 0.0 0.0 112720 984 pts/0 S+ 07:13 0:00 grep --color=auto rpcbind
[root@minglinux-02 ~] systemctl start rpcbind
[root@minglinux-02 ~] ps aux |grep rpcbind
rpc 1879 1.5 0.0 69220 1208 ? Ss 07:14 0:00 /sbin/rpcbind -w
root 1881 0.0 0.0 112720 984 pts/0 S+ 07:14 0:00 grep --color=auto rpcbind
[root@minglinux-02 ~] netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1392/master
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 887/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1392/master
tcp6 0 0 :::3306 :::* LISTEN 1400/mysqld
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 887/sshd
三糯耍、NFS配置選項(xiàng)
/home/nfstestdir 192.168.162.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
上節(jié)提到的配置文件中小括號里面的內(nèi)容是權(quán)限選項(xiàng)扔字,下面簡單介紹NFS的一些相關(guān)權(quán)限選項(xiàng):
rw:表示讀/寫。
ro:表示只讀温技。
sync:同步模式革为,表示內(nèi)存中的數(shù)據(jù)實(shí)時(shí)寫入磁盤。
async:非同步模式舵鳞,表示把內(nèi)存中的數(shù)據(jù)定期寫入磁盤震檩。
no_root_squash:加上這個(gè)選項(xiàng)后,root用戶就會(huì)對共享的目錄擁有至高的權(quán)限控制蜓堕,就像是對本機(jī)的目錄操作一樣抛虏。但這樣安全性降低。
root_squash:與no_root_squash選項(xiàng)對應(yīng)套才,表示root用戶對共享目錄的權(quán)限不高迂猴,只有普通用戶的權(quán)限,即限制了root霜旧。
all_squash:表示不管使用NFS的用戶是誰错忱,其身份都會(huì)被限定為一個(gè)指定的普通用戶身份儡率。
anonuid/anongid:要和root_squash以及all_squash選項(xiàng)一同使用挂据,用于指定使用NFS的用戶被限定后的uid和gid,但前提是本機(jī)的/etc/passwd中存在相應(yīng)的uid和gid儿普。
- 客戶端查看NFS的共享情況
[root@minglinux-02 ~] showmount -e 192.168.162.130 //192.168.162.130為服務(wù)端虛擬機(jī)IP
Export list for 192.168.162.130:
/home/nfstestdir 192.168.162.0/24
//在nfs服務(wù)工作時(shí)崎逃,rpcbind通過111端口通信,但nfs服務(wù)使用的是隨機(jī)端口眉孩。
//防火墻和selinux可能限制端口通信个绍,所以最好關(guān)閉selinux和防火墻(服務(wù)端和客戶端均需要關(guān)閉)
上例我們可以看到192.168.162.130的共享目錄為/home/nfstestdir,
信任主機(jī)為192.168.168.0/24這個(gè)網(wǎng)段浪汪。
- 客戶端掛載NFS共享目錄
[root@minglinux-02 ~] mount -t nfs 192.168.162.130:/home/nfstestdir /mnt/
[root@minglinux-02 ~] df -h
文件系統(tǒng) 容量 已用 可用 已用% 掛載點(diǎn)
/dev/sda3 28G 3.1G 25G 11% /
devtmpfs 901M 0 901M 0% /dev
tmpfs 911M 0 911M 0% /dev/shm
tmpfs 911M 9.6M 902M 2% /run
tmpfs 911M 0 911M 0% /sys/fs/cgroup
/dev/sda1 197M 140M 58M 71% /boot
tmpfs 183M 0 183M 0% /run/user/0
192.168.162.130:/home/nfstestdir 28G 6.6G 22G 24% /mnt
- 客戶端在共享目錄下創(chuàng)建測試文件
[root@minglinux-02 ~] cd /mnt/
[root@minglinux-02 /mnt] touch ming.txt
[root@minglinux-02 /mnt] ll
總用量 0
-rw-r--r--. 1 mysql mysql 0 12月 8 23:43 ming.txt
[root@minglinux-02 /mnt] id mysql
uid=1000(mysql) gid=1000(mysql) 組=1000(mysql)
- 服務(wù)端查看共享目錄
[root@minglinux-01 ~] ll /home/nfstestdir
總用量 0
-rw-r--r-- 1 1000 1000 0 12月 8 23:43 ming.txt //可以看到文件的屬主和屬組都為1000