我們接着玩Linux,O(∩_∩)O哈哈~
1.什么是nfs
NFS(Network File System)即網絡文件系統,是FreeBSD支持的文件系統中的一種,它允許網絡中的計算機之間通過TCP/IP網絡共享資源。在NFS的應用中,本地NFS的客戶端應用可以透明地讀寫位於遠端NFS服務器上的文件,就像訪問本地文件一樣。
2.搭建nfs
首先你得有一個Linux服務器哈,這里我還是用我的廉價騰訊雲~
2.1前期准備:
1.安裝nfs-utils和rpcbind
yum install nfs-utils rpcbind
2.設置開機啟動服務
chkconfig nfs on
chkconfig rpcbind on
3.啟動相關服務
service rpcbind start
service nfs start
2.2服務端配置:
1.創建共享目錄
mkdir /xjy/data/nfs/server
2.編輯/etc/exports文件添加如下內容
vim /etc/exports
/xjy/data/nfs/server *(rw,no_root_squash,no_all_squash,sync)
① /xjy/data/nfs/server —要共享的目錄
② * 處代表限制訪問的ip段
可以填192.168.0.* —允許訪問的網段,也可以是ip地址、主機名(能夠被服務器解析)
可以填192.168.0.123/24
*(所有人都能訪問)
③ 括號內的常見參數有:
參數值 內容說明
rw ro 該目錄分享的權限是可擦寫 (read-write) 或只讀 (read-only),但最終能不能讀寫,還是與文件系統的 rwx 及身份有關。
sync async sync 代表數據會同步寫入到內存與硬盤中,async 則代表數據會先暫存於內存當中,而非直接寫入硬盤!
no_root_squash root_squash 客戶端使用 NFS 文件系統的賬號若為 root 時,系統該如何判斷這個賬號的身份?預設的情況下,客戶端 root 的身份會由 root_squash 的設定壓縮成 nfsnobody, 如此對服務器的系統會較有保障。但如果你想要開放客戶端使用 root 身份來操作服務器的文件系統,那么這里就得要開 no_root_squash 才行!
all_squash 不論登入 NFS 的使用者身份為何, 他的身份都會被壓縮成為匿名用戶,通常也就是 nobody(nfsnobody) 啦!
no_subtree_check 關閉子樹檢查
anonuid anongid anon 意指 anonymous (匿名者) 前面關於 *_squash 提到的匿名用戶的 UID 設定值,通常為 nobody(nfsnobody),但是你可以自行設定這個 UID 的值!當然,這個 UID 必需要存在於你的 /etc/passwd 當中! anonuid 指的是 UID 而 anongid 則是群組的 GID 啰。
其他選項可以通過man exports查閱man文檔
3.刷新配置立即生效
exportfs -r
4.重啟nfs
service nfs restart
5.查看 RPC 服務的注冊狀況
[root@VM_15_8_centos server]# rpcinfo -p localhost program vers proto port service 4 tcp 111 portmapper 3 tcp 111 portmapper 2 tcp 111 portmapper 4 udp 111 portmapper 3 udp 111 portmapper 2 udp 111 portmapper 1 udp 49979 mountd 1 tcp 58393 mountd 2 udp 45516 mountd 2 tcp 37792 mountd 3 udp 32997 mountd 3 tcp 39937 mountd 2 tcp 2049 nfs 3 tcp 2049 nfs 4 tcp 2049 nfs 2 tcp 2049 nfs_acl 3 tcp 2049 nfs_acl 2 udp 2049 nfs 3 udp 2049 nfs 4 udp 2049 nfs 2 udp 2049 nfs_acl 3 udp 2049 nfs_acl 1 udp 51112 nlockmgr 3 udp 51112 nlockmgr 4 udp 51112 nlockmgr 1 tcp 43271 nlockmgr 3 tcp 43271 nlockmgr 4 tcp 43271 nlockmgr
選項與參數:
-p :針對某 IP (未寫則預設為本機) 顯示出所有的 port 與 porgram 的信息;
-t :針對某主機的某支程序檢查其 TCP 封包所在的軟件版本;
-u :針對某主機的某支程序檢查其 UDP 封包所在的軟件版本;
6.本機查詢nfs服務器
[root@VM_15_8_centos server]# showmount -e localhost
Export list for localhost:
/xjy/data/nfs/server *
選項與參數:
-a :顯示目前主機與客戶端的 NFS 聯機分享的狀態;
-e :顯示某部主機的 /etc/exports 所分享的目錄數據。
2.3客戶端配置:
1.創建掛載點
mkdir /xjy/data/nfs/client
2.查看服務器拋出的共享目錄信息
[root@VM_15_8_centos server]# showmount -e 192.168.0.123 Export list for 192.168.0.123: /xjy/data/nfs/server *
3.掛載目錄
為了提高NFS的穩定性,使用TCP協議掛載,NFS默認用UDP協議
mount -t nfs -o vers=3 192.168.0.123:/xjy/data/nfs/server /xjy/data/nfs/client -o proto=tcp -o nolock
4.查看掛載的目錄
[root@VM_15_8_centos server]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 18G 1.1G 16G 7% / tmpfs 112M 0 112M 0% /dev/shm /dev/sda1 477M 54M 398M 12% /boot 192.168.0.123:/data/lys 18G 1.1G 16G 7% /lys
3.測試:
服務端 [root@VM_15_8_centos xjy]# echo “test” > test.txt 客戶端 [root@VM_15_8_centos xjy]# cat /xjy/test.txt test [root@VM_15_8_centos xjy]# echo “204” >> /xjy/test.txt 服務端 [root@VM_15_8_centos xjy]# cat /data/xjy/test.txt test 204
4.取消掛載:
[root@VM_15_8_centos server]# umount /xjy/data/nfs/client [root@VM_15_8_centos server]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 18G 1.1G 16G 7% / tmpfs 112M 0 112M 0% /dev/shm /dev/sda1 477M 54M 398M 12% /boot 如果顯示:device is busy,可以使用-lf參數強行刪除 [root@VM_15_8_centos server]# umount –lf /xjy/data/nfs/client
5.重啟和關閉:
重啟:[root@VM_15_8_centos server]# service 服務名 restart 停止:[root@VM_15_8_centos server]# service 服務名 stop 開啟:[root@VM_15_8_centos server]# service 服務名 start
6.固定nfs服務端口
為了方便配置防火牆,需要固定nfs服務端口
NFS啟動時會隨機啟動多個端口並向RPC注冊,這樣如果使用iptables對NFS端口進行限制就會有點麻煩,可以更改配置文件固定NFS服務相關端口。
[root@VM_15_8_centos server]# rpcinfo -p localhost program vers proto port service 100000 4 tcp 111 portmapper 100000 3 tcp 111 portmapper 100000 2 tcp 111 portmapper 100000 4 udp 111 portmapper 100000 3 udp 111 portmapper 100000 2 udp 111 portmapper 100005 1 udp 49979 mountd 100005 1 tcp 58393 mountd 100005 2 udp 45516 mountd 100005 2 tcp 37792 mountd 100005 3 udp 32997 mountd 100005 3 tcp 39937 mountd 100003 2 tcp 2049 nfs 100003 3 tcp 2049 nfs 100003 4 tcp 2049 nfs 100227 2 tcp 2049 nfs_acl 100227 3 tcp 2049 nfs_acl 100003 2 udp 2049 nfs 100003 3 udp 2049 nfs 100003 4 udp 2049 nfs 100227 2 udp 2049 nfs_acl 100227 3 udp 2049 nfs_acl 100021 1 udp 51112 nlockmgr 100021 3 udp 51112 nlockmgr 100021 4 udp 51112 nlockmgr 100021 1 tcp 43271 nlockmgr 100021 3 tcp 43271 nlockmgr 100021 4 tcp 43271 nlockmgr
分配端口,編輯配置文件:
[root@VM_15_8_centos server]# vim /etc/sysconfig/nfs 添加: RQUOTAD_PORT=30001 LOCKD_TCPPORT=30002 LOCKD_UDPPORT=30002 MOUNTD_PORT=30003 STATD_PORT=30004
重啟
[root@VM_15_8_centos server]# service nfs restart
關閉 NFS 守護進程: [確定]
關閉 NFS mountd: [確定]
關閉 NFS 服務: [確定]
Shutting down RPC idmapd: [確定]
啟動 NFS 服務: [確定]
啟動 NFS mountd: [確定]
啟動 NFS 守護進程: [確定]
正在啟動 RPC idmapd: [確定]
查看結果
[root@VM_15_8_centos server]# rpcinfo -p localhost program vers proto port service 100000 4 tcp 111 portmapper 100000 3 tcp 111 portmapper 100000 2 tcp 111 portmapper 100000 4 udp 111 portmapper 100000 3 udp 111 portmapper 100000 2 udp 111 portmapper 100005 1 udp 30003 mountd 100005 1 tcp 30003 mountd 100005 2 udp 30003 mountd 100005 2 tcp 30003 mountd 100005 3 udp 30003 mountd 100005 3 tcp 30003 mountd 100003 2 tcp 2049 nfs 100003 3 tcp 2049 nfs 100003 4 tcp 2049 nfs 100227 2 tcp 2049 nfs_acl 100227 3 tcp 2049 nfs_acl 100003 2 udp 2049 nfs 100003 3 udp 2049 nfs 100003 4 udp 2049 nfs 100227 2 udp 2049 nfs_acl 100227 3 udp 2049 nfs_acl 100021 1 udp 30002 nlockmgr 100021 3 udp 30002 nlockmgr 100021 4 udp 30002 nlockmgr 100021 1 tcp 30002 nlockmgr 100021 3 tcp 30002 nlockmgr 100021 4 tcp 30002 nlockmgr
可以看到,隨機端口以固定
iptables策略問題完美解決!!!
7.設置系統開機就掛載磁盤
在/etc/fstab中加入開機掛載命令
[root@VM_15_8_centos server]# vim /etc/fstab 10.10.159.68:/s4_attachment /data/nfs/client nfs defaults 0 0
8.Windows作為客戶端掛載nfs
1.開啟nfs客戶端:打開控制面板》程序》啟用或關閉windos功能》選中nfs客戶端,點擊確定
.2.掛載:mount 192.168.0.123:/xjy/data/nfs X: (此處注意只能定位到nfs目錄,否則會報網絡錯誤)
會創建一個網絡盤X盤,里面掛載文件
3.取消掛載:右鍵X盤,點擊取消連接或者執行命令:umount X:
OVER!