NFS可以通過網絡,讓不同的機器、不同的操作系統可以共享彼此的文件,客戶端可以將網絡中NFS服務器共享的目錄掛載到本地端的文件系統中。客戶端的系統中來看,那個遠程主機的目錄就好像是自己的一個磁盤分區一樣,在使用上相當便利。
NFS服務的端口是2049,但由於文件系統非常複雜,NFS還有其他的程序去啓動額外隨機選擇小於1024的端口來傳輸數據,這些端口是通過遠程過程調用協議RPC來實現的,所以開啓NFS服務的同時,還需要開啓rpcbind服務。
1.安裝服務
nfs和rpcbind服務在rhel7中是默認安裝的,開啓服務並加入到開機啓動中。
[root@shijie Desktop]# yum install nfs-utils
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
rhel | 4.1 kB 00:00:00
Package 1:nfs-utils-1.3.0-0.el7.x86_64 already installed and latest version
Nothing to do
[root@shijie Desktop]# yum install rpcbind
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Package rpcbind-0.2.0-23.el7.x86_64 already installed and latest version
Nothing to do
[root@shijie Desktop]#
[root@shijie Desktop]# systemctl start rpcbind
[root@shijie Desktop]# systemctl start nfs-server
[root@shijie Desktop]# systemctl enable rpcbind
[root@shijie Desktop]# systemctl enable nfs-server
ln -s '/usr/lib/systemd/system/nfs-server.service' '/etc/systemd/system/nfs.target.wants/nfs-server.service'
2.關閉防火牆策略
[root@shijie ~]# iptables -F
[root@shijie ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
3.編輯配置文件
配置文件是/etc/exports,文件是空的,在文件中編輯共享目錄、共享的IP和用戶權限。格式:共享目錄的路徑 允許訪問的NFS客戶端(共享權限參數)。
權限參數 作用
ro 只讀
rw 讀寫
root_squash 當NFS客戶端以root管理員訪問時,映射爲NFS服務器的匿名用戶
no_root_squash 當NFS客戶端以root管理員訪問時,映射爲NFS服務器的root管理員
all_squash 無論NFS客戶端使用什麼賬戶訪問,均映射爲NFS服務器的匿名用戶
sync 同時將數據寫入到內存與硬盤中,保證不丟失數據
async 優先將數據保存到內存,然後再寫入硬盤;這樣效率更高,但可能會丟失數據
[root@shijie Desktop]# vim /etc/exports
/root/Desktop/nfsfile 192.168.10.*(rw,sync,no_root_squash)
4.重啓服務
重啓服務需要將nfs-server和rpcbind都需要重啓。
[root@shijie Desktop]# systemctl restart rpcbind
[root@shijie Desktop]# systemctl restart nfs-server
5.客戶端訪問
5.1linux客戶端
知道服務器IP可以使用showmount命令查詢服務器共享的文件夾和共享給客戶端的網段。
參數 作用
-e 顯示NFS服務器的共享列表
-a 顯示本機掛載的文件資源的情況NFS資源的情況
-v 顯示版本號
[root@shijie2 Desktop]# showmount -e 192.168.10.10
Export list for 192.168.10.10:
/root/Desktop/nfsfile 192.168.10.*
如果自己網段是屬於服務器共享的網段,就可以直接掛載使用了。掛載到桌面的file目錄。
[root@shijie2 Desktop]# mount.nfs 192.168.10.10:/root/Desktop/nfsfile /root/Desktop/file/
[root@shijie2 Desktop]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.2G 15G 18% /
devtmpfs 908M 0 908M 0% /dev
tmpfs 914M 140K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sr0 3.5G 3.5G 0 100% /media/cdrom
/dev/sda1 497M 96M 401M 20% /boot
192.168.10.10:/root/Desktop/nfsfile 18G 3.0G 15G 17% /root/Desktop/file
5.2windows訪問共享
先安裝NFS服務和客戶端。
映射網絡驅動器。
添加網絡路徑和本地盤符號。
這樣就可以在windows上使用linux的nfs服務器了。