第七單元
通路網絡檔案共享服務
一 挂載網絡檔案系統
網絡檔案系統是由網絡附加存儲伺服器通過網絡向多個主機提供的一種檔案系統 , 而不是由塊裝置 ( 例如硬碟驅動器 ) 提供的。用戶端通過特殊的檔案系統協定和格式通路遠端存儲
Linux 中有兩種主要協定可用通路網絡檔案系統 : NFS 和CIFS
1 )CIFS: 通用網絡檔案系統
CIFS 是針對 Microsoft Windows 作業系統的本地網絡檔案系統Linux 系統可以挂載和通路 CIFS 檔案共享 , 如同常見的網絡檔案系統一樣。
samba-client RPM 軟體包所包含的 smbclient 實用程式可用來識别由 Windows 或Samba 檔案伺服器提供的 CIFS 共享
用戶端:
1 通過samba-client方式共享
yum install samba-client -y ###安裝samba-client###
smbclient -L //172.25.254.250 ###登入遠端伺服器172.25.254.250###
smbclient //172.25.254.250/westos1 ###進入westos1###
put anaconda-ks.cfg ###往westos1上傳檔案###
rm anaconda-ks.cfg ###在westo1上删除檔案###
過程如下:
[root@localhost ~]# smbclient -L //172.25.254.250 ###登入172.25.254.250
Enter root's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
Sharename Type Comment
--------- ---- -------
westos1 Disk test share
westos2 Disk test share
westos3 Disk test share
westos4 Disk test share
westos5 Disk test share
westos6 Disk test share
westos7 Disk test share
westos8 Disk test share
westos9 Disk test share
westos10 Disk test share
IPC$ IPC IPC Service (Samba Server Version 4.1.1)
Server Comment
--------- -------
Workgroup Master
[root@localhost ~]# smbclient //172.25.254.250/westos1 ###進入westos1###
Enter root's password:
smb: \> ls ###檢視遠端伺服器的檔案###
. D 0 Sun Apr 30 01:36:27 2017
.. D 0 Wed Dec 2 20:43:01 2015
file A 52 Sun Apr 30 01:35:24 2017
userfile A 19 Sun Apr 30 01:35:25 2017
anaconda-ks.cfg A 8619 Sun Apr 30 01:36:01 2017
file2 N 0 Sun Apr 30 01:36:32 2017
file3 N 0 Sun Apr 30 01:36:32 2017
file4 N 0 Sun Apr 30 01:36:32 2017
file5 N 0 Sun Apr 30 01:36:32 2017
file6 N 0 Sun Apr 30 01:36:32 2017
file7 N 0 Sun Apr 30 01:36:32 2017
file8 N 0 Sun Apr 30 01:36:32 2017
file9 N 0 Sun Apr 30 01:36:32 2017
file10 N 0 Sun Apr 30 01:36:32 2017
fileeeeee N 0 Sun Apr 30 01:36:17 2017
100221120 blocks of size 1024. 21816820 blocks available
smb: \> !ls ###檢視本地檔案###
anaconda-ks.cfg etc file.rej Public
boot file kernel-3.10.0-123.el7.x86_64.rpm Templates
Desktop file1 lib Videos
Documents file3 Music
Downloads file.path Pictures
smb: \> put anaconda-ks.cfg
putting file anaconda-ks.cfg as \anaconda-ks.cfg (27.5 kb/s) (average 27.5 kb/s)
smb: \> rm anaconda-ks.cfg
NT_STATUS_NO_SUCH_FILE listing \anaconda-ks.cfg
smb: \> quit
2 通過挂載的方式共享
mount //172.25.254.250/westos1 /mnt/ -o username=guest ###以匿名使用者身份将westos1挂載到本地的/mnt下###
touch /mnt/file3
過程如下:
[root@localhost ~]# mount //172.25.254.250/westos1
mount: can't find //172.25.254.250/westos1 in /etc/fstab
[root@localhost ~]# mount //172.25.254.250/westos1 -o username=guest
[root@localhost ~]# mount //172.25.254.250/westos1 /mnt/ -o username=guest
[root@localhost ~]# touch /mnt/file3
[root@localhost ~]# touch /mnt/filehello
3 開機自動挂載
vim /etc/fstab
mount -a ###立即生效####
/etc/fstab下寫的内容:
//172.25.254.250/westos1 /mnt cifs defaults,username=guest 0 0
[root@localhost ~]# umount /mnt/
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# mount -a ###立即生效####
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 4064104 6409796 39% /
devtmpfs 922268 0 922268 0% /dev
tmpfs 942660 84 942576 1% /dev/shm
tmpfs 942660 17040 925620 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/sr0 3654720 3654720 0 100% /run/media/root/RHEL-7.0 Server.x86_64
//172.25.254.250/westos1 100221120 78407756 21813364 79% /mnt
2)nfs:網絡檔案系統
NFS ( Network File System ) 是由 Linux 、 UNIX 及其他類似作業系統使用的網際網路标準協定 , 可作為它們的本地網絡檔案系統。
它是一種活動擴充之下的開放标準 , 可支援本地 Linux 權限和檔案系統功能
有兩種方法用來确定由伺服器提供的 NFS 導出 , 或叫做檔案共享。如果伺服器支援 NFSv3 或 NFSv2 , 您可以使用 showmount -e server 指令來擷取導出項清單。如果伺服器支援 NFSv4 , 則可以将 / 導出項挂載到一個空目錄并浏覽所有導出檔案系統的内容
1 通路 NFS 導出的指令
yum install nfs-utils -y ###安裝nfs####
showmount -e ###登入###
mount ###挂載###
[root@localhost ~]# showmount -e 172.25.254.250 ###-e指連接配接到指定遠端主機###
Export list for 172.25.254.250:
/nfsshare/nfs5 *
/nfsshare/nfs4 *
/nfsshare/nfs3 *
/nfsshare/nfs2 *
/nfsshare/nfs1 *
/content 172.25.0.0/255.255.0.0
[root@localhost ~]# mount 172.25.254.250:/nfsshare/nfs1 /mnt/ ###挂載,不用寫用戶端#####
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# touch haha{1..10}
[root@localhost mnt]# ls
file1 haha10 haha3 haha5 haha7 haha9
haha1 haha2 haha4 haha6 haha8
[root@localhost mnt]# rm -fr *
2 開機自動挂載
mount -a
/etc/fstab 下寫的内容:
172.25.254.250:/nfsshare/nfs1 /mnt defaults 0 0
[root@localhost mnt]# cd
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# mount -a
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 4065080 6408820 39% /
devtmpfs 922268 0 922268 0% /dev
tmpfs 942660 84 942576 1% /dev/shm
tmpfs 942660 17044 925616 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/sr0 3654720 3654720 0 100% /run/media/root/RHEL-7.0 Server.x86_64
172.25.254.250:/nfsshare/nfs1 100221952 78411776 21810176 79% /mnt
[root@localhost ~]# touch file
[root@localhost ~]# touch file1
[root@localhost ~]# rm -fr file file1
二 自動挂載網絡存儲
使用 mount 指令 , 需要具有 root 使用者權限以連接配接到網絡共享。或者 , 我們可以向/etc/fstab 添加條目 , 但是随後與網絡伺服器的連接配接将始終處于活動狀态。當某一程式試圖通路網絡共享中的檔案時 , 可以将 automounter ( 或 autofs )服務配置為 “ 按需 ” 挂載網絡共享。當共享不再使用并處于不活動狀态一定時間以後 ,automounter 會對共享解除挂載
不活動狀态的時間間隔預設為五分鐘 , 但該時間間隔可在/etc/sysconfig/autofs 中進行全局更改
1 使用/net映射
/net,自挂載挂載點 , 然後手動配置間接自動挂載映射
預設情況下 , autofs 服務運作時 , 存在一個名為 /net 的特殊目錄 , 但是該目錄将顯示為空,通路不存在的目錄 /net/instructor.example.com 會使自動挂載器建立該子目錄 , 并顯示該 NFS 伺服器上的所有NFS 導出項。這有時稱為 “ 浏覽 ” 共享
/net/instructor.example.com 之下的所有檔案和目錄停止使用且逾時期滿後 ,autofs 将共享解除挂載并删除空的 /net/instructor.example.com 子目錄
yum install autofs.x86_64 -y ###安裝autofs.x86_64###
systemctl start autofs ###開啟服務###
cd /net ###進入該目錄下###
ls ###檢視發現為空###
cd 172.25.254.250 ###通路不存在的目錄172.25.254.250
ls ##檢視發現有内容###
cd nfsshare/nfs1
[root@localhost ~]# systemctl start autofs ###開啟服務###
[root@localhost ~]# cd /net/
[root@localhost net]# cd 172.25.254.250
[root@localhost 172.25.254.250]# ls
content nfsshare
[root@localhost 172.25.254.250]# cd nfsshare/nfs1
[root@localhost nfs1]# ls
[root@localhost nfs1]# touch file1
file1 file2
[root@localhost nfs1]# df ###檢視是否挂載###
/dev/vda1 10473900 4070616 6403284 39% /
tmpfs 942660 17052 925608 2% /run
172.25.254.250:/nfsshare/nfs1 100221952 78411776 21810176 79% /net/172.25.254.250/nfsshare/nfs1
[root@localhost nfs1]# cd
[root@localhost ~]# vim /etc/sysconfig/autofs ###編輯挂載時間###
/dev/vda1 10473900 4070636 6403264 39% /
172.25.254.250:/nfsshare/nfs1 100221952 78415872 21806080 79% /net/172.25.254.250/nfsshare/nfs1
2 間接映射
除了使用 /net 映射 , 系統管理者也可以手動配置任意目錄, 以便通路時在其子目錄上 “ 按需 ” 挂載特定共享
vim /etc/auto.master
/etc/auto.master下寫的内容:
最終挂載點的上一級目錄 有最終挂載點的一個目錄(例 :/etc/auto.westos)
vim /etc/auto.westos(你在/etc/auto.master下編輯的最終挂載點目錄)
編輯内容:
nfs1 172.25.254.250:/nfsshare/nfs1 ###将遠端伺服器的共享目錄挂載到nfs1,這樣隻能挂載nfs2###
* 172.25.254.250:/nfsshare/& ###可以挂載任意目錄###
systemctl restart autofs.service ###重新開機服務###
[root@localhost ~]# mkdir /westos
[root@localhost ~]# vim /etc/auto.master
[root@localhost ~]# vim /etc/auto.westos
#####此時/etc/auto.westos下的内容為:nfs1 172.25.254.250:/nfsshare/nfs1####
[root@localhost ~]# systemctl restart autofs.service
[root@localhost ~]# cd /westos/
[root@localhost westos]# ls ###/westos下沒有nfs1目錄####
[root@localhost westos]# cd nfs1
[root@localhost nfs1]# pwd
/westos/nfs1
[root@localhost nfs1]# df
/dev/vda1 10473900 4070672 6403228 39% /
172.25.254.250:/nfsshare/nfs1 100221952 78409728 21812224 79% /westos/nfs1
[root@localhost nfs1]# cd nfs2
-bash: cd: nfs2: No such file or directory
nfs1 172.25.254.250://nfsshare/nfs1
[root@localhost nfs1]# vim /etc/auto.westos
#####此時/etc/auto.westos下的内容為:* 172.25.254.250:/nfsshare/&#####
[root@localhost nfs1]# systemctl restart autofs.service
[root@localhost ~]# cd /westos/nfs2
[root@localhost nfs2]# pwd
/westos/nfs2
[root@localhost nfs3]# df
172.25.254.250:/nfsshare/nfs2 100221952 78409728 21812224 79% /westos/nfs2
本文轉自blueclo51CTO部落格,原文連結:http://blog.51cto.com/12774272/1921148 ,如需轉載請自行聯系原作者