14.1 NFS介紹 14.2 NFS服務端安裝配置 14.3 NFS配置選項
14.1 NFS介紹
NFS是Network File System的縮寫
NFS最早由Sun公司開發,分2,3,4三個版本,2和3由Sun起草開發,4.0開始Netapp公司參與並主導開發,最新為4.1版本
NFS數據傳輸基於RPC協議,RPC為Remote Procedure Call的簡寫。
NFS應用場景是:A,B,C三臺機器上需要保證被訪問到的文件是一樣的,A共享數據出來,B和C分別去掛載A共享的數據目錄,從而B和C訪問到的數據和A上的一致
例如: 一個站點,上面傳輸了很多圖片,用戶訪問一個圖片時,需要從A機器調用,但A機器負載高,所以就弄多兩臺B機器C機器同時提供服務;正常的話,需要到A機器上才能拿到數據,但是B機器和C機器做了負載均衡,分擔了相同的服務器,那麽用戶也有可能到B機器或者C機器上;那麽用戶請求到B機器上的時候,如何才能獲取到A機器上的數據呢;要麽把A機器的數據傳輸到B機器上,但是這個不能時時更新,(用戶上傳的數據是存放在A機器上,但用戶請求的時候數據是請求到B機器上)這樣就會導致用戶請求獲取的數據訪問不到;那麽NFS服務就可以解決這個問題,將A機器的數據共享到B機器、C機器。有NFS服務以後,上傳到A機器上的數據,B機器或C機器上就能馬上看到和調用
NFS原理圖: 服務端需要啟動一個NFS服務,服務端要想給客戶端提供服務,需要借助RPC協議,RPC協議是由rpcbind服務實現;在centos 5或者之前的版本叫portmap服務;之後的版本叫rpcbind服務;NFS服務默認不會監聽任何端口;最終實現NFS服務,需要借助rpcbind服務產生的RPC協議,RPC協議默認監聽的端口是111; 整個流程為:服務端的NFS服務監聽一個端口通過RPC協議監聽的端口,告訴客戶端RPC協議,然後客戶端通過本機的RPC端口回傳數據信息到服務端NFS監聽的端口,最終實現通信
14.2 NFS服務端安裝配置
步驟如下
yum install -y nfs-utils rpcbind
vim /etc/exports //加入如下內容
/home/nfstestdir 192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
保存配置文件後,執行如下準備操作
mkdir /home/nfstestdir
chmod 777 /home/nfstestdir
systemctl start rpcbind
systemctl start nfs
systemctl enable rpcbind
systemctl enable nfs
首先要準備倆臺機器,一臺服務端,一臺客戶端
在客戶端 192.168.202.132 安裝 nfs-utils yum install -y nfs-utils
在服務端 192.168.202.131 上安裝 nfs-utils ,rpcbind yum install -y nfs-utils rpcbind
首先服務端上安裝nfs-utils ,rpcbind,客戶端安裝 nfs-utils
``` [root@localhost ~]# yum install -y nfs-utils rpcbind 已加載插件:fastestmirror 已安裝: nfs-utils.x86_64 1:1.3.0-0.48.el7_4 rpcbind.x86_64 0:0.2.0-42.el7 作為依賴被安裝: gssproxy.x86_64 0:0.7.0-4.el7 keyutils.x86_64 0:1.5.8-3.el7 libbasicobjects.x86_64 0:0.1.1-27.el7 libcollection.x86_64 0:0.6.2-27.el7 libevent.x86_64 0:2.0.21-4.el7 libini_config.x86_64 0:1.3.0-27.el7 libnfsidmap.x86_64 0:0.25-17.el7 libpath_utils.x86_64 0:0.2.1-27.el7 libref_array.x86_64 0:0.1.5-27.el7 libtirpc.x86_64 0:0.2.4-0.10.el7 libverto-libevent.x86_64 0:0.2.5-4.el7 quota.x86_64 1:4.01-14.el7 quota-nls.noarch 1:4.01-14.el7 tcp_wrappers.x86_64 0:7.6-77.el7 完畢! [root@localhost ~]# 剛剛改了下主機名 hostname [root@aming-01 ~]# yum install -y nfs-utils rpcbind 已加載插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.btte.net * epel: ftp.riken.jp * extras: mirrors.163.com * updates: mirrors.163.com 軟件包 1:nfs-utils-1.3.0-0.48.el7_4.x86_64 已安裝並且是最新版本 軟件包 rpcbind-0.2.0-42.el7.x86_64 已安裝並且是最新版本 無須任何處理 [root@aming-01 ~]# ``` - 客戶端安裝 ``` [root@aming-02 ~]# yum install -y nfs-utils 已安裝: nfs-utils.x86_64 1:1.3.0-0.48.el7_4 作為依賴被安裝: gssproxy.x86_64 0:0.7.0-4.el7 keyutils.x86_64 0:1.5.8-3.el7 libbasicobjects.x86_64 0:0.1.1-27.el7 libcollection.x86_64 0:0.6.2-27.el7 libevent.x86_64 0:2.0.21-4.el7 libini_config.x86_64 0:1.3.0-27.el7 libnfsidmap.x86_64 0:0.25-17.el7 libpath_utils.x86_64 0:0.2.1-27.el7 libref_array.x86_64 0:0.1.5-27.el7 libtirpc.x86_64 0:0.2.4-0.10.el7 libverto-libevent.x86_64 0:0.2.5-4.el7 quota.x86_64 1:4.01-14.el7 quota-nls.noarch 1:4.01-14.el7 rpcbind.x86_64 0:0.2.0-42.el7 tcp_wrappers.x86_64 0:7.6-77.el7 完畢! [root@aming-02 ~]# ``` - 安裝完了之後在服務端 編輯 vim /etc/exports - 創建一個/home/nfstestdir 目錄 把它的權限改為777 ``` [root@aming-01 ~]# vim /etc/exports /home/nfstestdir 192.168.202.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) ~ :wq [root@aming-01 ~]# vim /etc/exports [root@aming-01 ~]# mkdir /home/nfstestdir [root@aming-01 ~]# chmod 777 /home/nfstestdir [root@aming-01 ~]# ``` - 在啟動rpcbind之前 來看下 現在啟動的服務 - 發現rpnbind安裝完之後就自動幫你起來 ``` [root@aming-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:80 0.0.0.0:* LISTEN 954/nginx: master p tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 953/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1774/master tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 954/nginx: master p tcp6 0 0 :::3306 :::* LISTEN 1480/mysqld tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 953/sshd tcp6 0 0 ::1:25 :::* LISTEN 1774/master [root@aming-01 ~]# ``` - 看看客戶端的開啟的服務 ``` [root@aming-02 ~]# 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 832/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1385/master tcp6 0 0 :::3306 :::* LISTEN 1161/mysqld tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 832/sshd tcp6 0 0 ::1:25 :::* LISTEN 1385/master [root@aming-02 ~]# [root@aming-02 ~]# ps aux |grep rpc rpc 2426 0.0 0.1 64964 1052 ? Ss 22:16 0:00 /sbin/rpcbind -w root 2547 0.0 0.0 112680 980 pts/0 S+ 22:29 0:00 grep --color=auto rpc [root@aming-02 ~]# ``` - 既然已經啟動了rpcbind 就開始啟動nfs - 實際上在啟動nfs的時候 它會自動幫你啟動rpc ``` [root@aming-01 ~]# systemctl start nfs [root@aming-01 ~]# ps aux | grep nfs root 2457 0.0 0.0 0 0 ? S< 22:31 0:00 [nfsd4_callbacks] root 2463 0.0 0.0 0 0 ? S 22:31 0:00 [nfsd] root 2464 0.0 0.0 0 0 ? S 22:31 0:00 [nfsd] root 2465 0.0 0.0 0 0 ? S 22:31 0:00 [nfsd] root 2466 0.0 0.0 0 0 ? S 22:31 0:00 [nfsd] root 2467 0.0 0.0 0 0 ? S 22:31 0:00 [nfsd] root 2468 0.0 0.0 0 0 ? S 22:31 0:00 [nfsd] root 2469 0.0 0.0 0 0 ? S 22:31 0:00 [nfsd] root 2470 0.0 0.0 0 0 ? S 22:31 0:00 [nfsd] root 2474 0.0 0.0 112680 980 pts/0 R+ 22:31 0:00 grep --color=auto nfs [root@aming-01 ~]# [root@aming-01 ~]# ps aux | grep rpc root 376 0.0 0.0 0 0 ? S< 22:02 0:00 [rpciod] rpc 2431 0.0 0.1 64964 1432 ? Ss 22:30 0:00 /sbin/rpcbind -w rpcuser 2445 0.0 0.1 42380 1752 ? Ss 22:31 0:00 /usr/sbin/rpc.statd root 2446 0.0 0.0 19324 652 ? Ss 22:31 0:00 /usr/sbin/rpc.idmapd root 2447 0.0 0.0 42564 948 ? Ss 22:31 0:00 /usr/sbin/rpc.mountd root 2476 0.0 0.0 112680 980 pts/0 R+ 22:31 0:00 grep --color=auto rpc [root@aming-01 ~]# ``` - 如果想要開機就啟動,還需要執行這個命令systemctl enable nfs systemctl enable rpcbind ``` [root@aming-01 ~]# systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service. [root@aming-01 ~]# [root@aming-01 ~]# cat /etc/exports /home/nfstestdir 192.168.202.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) [root@aming-01 ~]# ```
14.3 NFS配置選項
rw 讀寫
ro 只讀
sync 同步模式,內存數據實時寫入磁盤
async 非同步模式
no_root_squash 客戶端掛載NFS共享目錄後,root用戶不受約束,權限很大
root_squash 與上面選項相對,客戶端上的root用戶收到約束,被限定成某個普通用戶
all_squash 客戶端上所有用戶在使用NFS共享目錄時都被限定為一個普通用戶
anonuid/anongid 和上面幾個選項搭配使用,定義被限定用戶的uid和gid
客戶端掛載
yum install -y nfs-utils
showmount -e 192.168.133.130 //該ip為NFS服務端ip
mount -t nfs 192.168.133.130:/home/nfstestdir /mnt
df -h
touch /mnt/aminglinux.txt
ls -l /mnt/aminglinux.txt //可以看到文件的屬主和屬組都為1000
先去客戶端,shwomount -e 192.168.202.131 該ip為NFS服務端ip
``` [root@aming-02 ~]# show showconsolefont showkey showmount [root@aming-02 ~]# showmount -e 192.168.202.131 clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host) [root@aming-02 ~]# ``` - 報錯了,看下防火墻是否開啟,報錯找不到指定的主機 之前已經確認過了111端口已經被監聽,但是還是訪問不了;那就可能是防火墻導致 關閉防火墻, - 先去服務端把防火墻關掉,還有selinux ``` [root@aming-01 ~]# systemctl stop firewalld [root@aming-01 ~]# setenforce 0 [root@aming-01 ~]# getenforce Permissive [root@aming-01 ~]# ``` - 在把客戶端防火墻,selinux 關掉 - 因為NFS服務比較特殊,雖然rpc可以通信,就算iptables把111端口放行但NFS也不一定能正常通信;因為他們用了一個不固定的端口 ``` [root@aming-02 ~]# systemctl stop firewalld [root@aming-02 ~]# getenforce Enforcing [root@aming-02 ~]# setenforce 0 [root@aming-02 ~]# getenforce Permissive [root@aming-02 ~]# showmount -e 192.168.202.131 Export list for 192.168.202.131: /home/nfstestdir 192.168.202.0/24 [root@aming-02 ~]# ``` - 現在就可以去掛載了 ``` [root@aming-02 ~]# mount -t nfs 192.168.202.131:/home/nfstestdir /mnt/ [root@aming-02 ~]# df -h 文件系統 容量 已用 可用 已用% 掛載點 /dev/sda3 28G 4.2G 24G 15% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.7M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 197M 109M 88M 56% /boot tmpfs 98M 0 98M 0% /run/user/0 192.168.202.131:/home/nfstestdir 28G 4.4G 24G 16% /mnt [root@aming-02 ~]# ``` - 這就是遠程服務端nfs共享的目錄 ``` [root@aming-02 ~]# cd /mnt/ [root@aming-02 mnt]# ls [root@aming-02 mnt]# touch aminglinux.111 [root@aming-02 mnt]# ls -l 總用量 0 -rw-r--r--. 1 mysql mysql 0 10月 31 23:16 aminglinux.111 [root@aming-02 mnt]# ```
先來看下服務端這邊有沒有更新
[root@aming-01 ~]# ls -l /home/nfstestdir/總用量 0 -rw-r--r--. 1 mysql mysql 0 10月 31 23:16 aminglinux.111 [root@aming-01 ~]#
原因是配置 /etc/export 文件時,配置了anonuid=1000,anongid=1000 客戶端顯示 1000 用戶 是因為客戶端機器上並沒有1000權限的用戶 同樣,服務端顯示mysql,是因為服務端上的機器mysql 就是1000權限的用戶
14.1 NFS介紹 14.2 NFS服務端安裝配置 14.3 NFS配置選項