這裏服務器環境爲Centos6.8 64位,並關閉了防火牆。
NFS(Network File System)即網絡文件系統,是FreeBSD支持的文件系統中的一種,它允許網絡中的計算機之間通過TCP/IP網絡共享資源。在NFS的應用中,本地NFS的客戶端應用可以透明地讀寫位於遠端NFS服務器上的文件,就像訪問本地文件一樣。
【1】安裝nfs和rpcbind
① 檢測是否已經安裝
[root@localhost softinstall]# rpm -aq | grep nfs
[root@localhost softinstall]# rpm -aq | grep rpcbind
② 安裝nfs和rpcbind
yum -y install nfs-utils rpcbind
安裝後檢測如下:
[root@localhost softinstall]# rpm -aq | grep rpcbind
rpcbind-0.2.0-16.el6.x86_64
[root@localhost softinstall]# rpm -aq | grep nfs
nfs-utils-lib-1.1.5-13.el6.x86_64
nfs-utils-1.2.3-78.el6_10.1.x86_64
【2】配置nfs服務機
① 創建需要共享的目錄
這裏使用/mnt/app目錄:
mkdir -p /mnt/app
② 配置nfs的配置文件
命令:
vim /etc/exports
在這個文件中添加需要輸出的目錄,如:
/mnt/app 192.168.73.130(rw)
/mnt/app 192.168.73.131(rw,sync,no_root_squash,no_subtree_check)
#/mnt/app:表示的是nfs服務器需要共享給其他客戶端服務器的文件夾
#192.168.73.130: 表示可以掛載服務器目錄的客戶端ip
#(rw):表示該客戶端對共享的文件具有讀寫權限
關於其他權限說明:
- rw:可讀寫的權限;
- ro:只讀的權限;
- no_root_squash:登入到NFS主機的用戶如果是root,該用戶即擁有root權限;(不添加此選項ROOT只有RO權限)
- root_squash:登入NFS主機的用戶如果是root,該用戶權限將被限定爲匿名使用者nobody;
- all_squash:不管登陸NFS主機的用戶是何權限都會被重新設定爲匿名使用者nobody。
- anonuid:將登入NFS主機的用戶都設定成指定的user id,此ID必須存在於/etc/passwd中。
- anongid:同anonuid,但是變成group ID就是了!
- sync:資料同步寫入存儲器中。
- async:資料會先暫時存放在內存中,不會直接寫入硬盤。
- insecure:允許從這臺機器過來的非授權訪問。
刷新配置立即生效
exportfs -r
③ 啓動服務檢測服務狀態
設置開機啓動
chkconfig nfs on
chkconfig rpcbind on
啓動rpcbind 服務
service rpcbind start
啓動nsf服務
service nfs start
查看 RPC 服務的註冊狀況
[root@localhost mnt]# 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
100011 1 udp 875 rquotad
100011 2 udp 875 rquotad
100011 1 tcp 875 rquotad
100011 2 tcp 875 rquotad
100005 1 udp 52791 mountd
100005 1 tcp 42294 mountd
100005 2 udp 55511 mountd
100005 2 tcp 54774 mountd
100005 3 udp 50872 mountd
100005 3 tcp 36770 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 46920 nlockmgr
100021 3 udp 46920 nlockmgr
100021 4 udp 46920 nlockmgr
100021 1 tcp 39362 nlockmgr
100021 3 tcp 39362 nlockmgr
100021 4 tcp 39362 nlockmgr
檢測服務狀態
service nfs status
service rpcbind status
查看服務開機啓動狀態
[root@localhost mnt]# chkconfig --list |grep nfs
nfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off
nfs-rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
nfslock 0:off 1:off 2:off 3:on 4:on 5:on 6:off
[root@localhost mnt]# chkconfig --list |grep rpcbind
rpcbind 0:off 1:off 2:on 3:on 4:on 5:on 6:off
可以手動設置服務運行級別,如下實例:
[root@localhost mnt]# chkconfig --level 2345 nfs on
[root@localhost mnt]# chkconfig --list |grep nfs
nfs 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nfs-rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
nfslock 0:off 1:off 2:off 3:on 4:on 5:on 6:off
④ 檢測配置文件
如下所示,打印配置文件內容:
[root@localhost mnt]# showmount -e
Export list for localhost.localdomain:
/mnt/app 192.168.73.130
注意:在執行這個命令的時候如果出現錯誤,說明DNS不能解析當前的服務器,那就是hosts文件沒有配置。
【3】客戶端掛載NFS中共享的目錄
① 創建本地路徑(也就是掛載點)
mkdir /mnt/app
② 安裝並啓動nfs與rpcbind服務
yum -y install nfs-utils rpcbind
chkconfig nfs on
chkconfig rpcbind on
service nfs start
service rpcbind start
③ 查看服務器拋出的共享目錄信息
[root@localhost ~]# showmount -e 192.168.73.129
Export list for 192.168.73.129:
/mnt/app 192.168.73.130
④ 掛載目錄
爲了提高NFS的穩定性,使用TCP協議掛載,NFS默認用UDP協議
mount -t nfs -o vers=3 192.168.73.129:/mnt/app /mnt/app -o proto=tcp -o nolock
#服務器IP:服務器共享路徑 本機掛載點
#192.168.73.129:/mount/app /mount/app
⑤ 測試驗證
服務器(192.168.73.129)創建文件:
cd /mnt/app
touch test.txt
客戶機驗證:
cd /mnt/app
ll
⑥ 取消掛載
客戶機執行命令
umount /mnt/app
這裏可以聯想服務器掛載磁盤,設置開機掛載、取消掛載同樣操作。
⑦ 查看掛載的狀態
命令:
mount | grep nfs
實例:
[root@localhost app]# mount | grep nfs
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
192.168.73.129:/mnt/app on /mnt/app type nfs (rw,vers=3,proto=tcp,nolock,addr=192.168.73.129)
【4】固定nfs服務端口
NFS啓動時會隨機啓動多個端口並向RPC註冊,這樣如果使用防火牆對NFS端口進行限制就會有點麻煩,可以更改配置文件固定NFS服務相關端口。
分配端口,編輯配置文件:
[root@localhost app]# vim /etc/sysconfig/nfs
添加如下(或者可以直接從配置文件裏面修改):
RQUOTAD_PORT=30001
LOCKD_TCPPORT=30002
LOCKD_UDPPORT=30002
MOUNTD_PORT=30003
STATD_PORT=30004
重啓nfs服務
[root@localhost app]# service nfs restart
Shutting down NFS daemon: [ OK ]
Shutting down NFS mountd: [ OK ]
Shutting down NFS quotas: [ OK ]
Shutting down NFS services: [ OK ]
Shutting down RPC idmapd: [ OK ]
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]
查看服務註冊狀態
[root@localhost app]# 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
100011 1 udp 30001 rquotad
100011 2 udp 30001 rquotad
100011 1 tcp 30001 rquotad
100011 2 tcp 30001 rquotad
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
【5】客戶機設置系統開機掛載磁盤
通過修改/etc/fstab實現掛載,添加完成後執行mount –a 即刻生效。
vim /etc/fstab
#添加配置如下
192.168.73.129:/mnt/app /mnt/app nfs defaults 0 0