|
1. 前言 本文主要講解linux軟鏈接與硬鏈接的原理以及使用的方法。Linux的鏈接包括軟鏈接和硬鏈接,類似于Linux的快捷方式。 linux怎么建立軟鏈接和硬鏈接 一個(gè)文件由文件名和數(shù)據(jù)組成,在 Linux 系統(tǒng)上也是被分成兩個(gè)部分:用戶數(shù)據(jù) (user data) 與元數(shù)據(jù) (metadata)。用戶數(shù)據(jù),即文件數(shù)據(jù)塊 (data block),數(shù)據(jù)塊是記錄文件真實(shí)內(nèi)容的地方;而元數(shù)據(jù)則是文件的附加屬性,如文件大小、創(chuàng)建時(shí)間、所有者等信息。在 Linux系統(tǒng)中,元數(shù)據(jù)中的 inode 號(hào)(inode 是文件元數(shù)據(jù)的一部分但其并不包含文件名,inode 號(hào)即索引節(jié)點(diǎn)號(hào))才是文件的唯一標(biāo)識(shí)而非文件名。文件名僅是為了方便人們的記憶和使用,系統(tǒng)或程序通過 inode 號(hào)尋找正確的文件數(shù)據(jù)塊。下圖展示了程序通過文件名獲取文件內(nèi)容的過程。 通過文件名打開文件 2. 查看文件對應(yīng)的inode號(hào)碼 在 Linux 系統(tǒng)中查看 inode 號(hào)可使用命令 stat 或 ls -i。 [root@CentOS7 ~]# stat anaconda-ks.cfg 文件:'anaconda-ks.cfg' 大?。?408 塊:8 IO 塊:4096 普通文件 設(shè)備:fd00h/64768d Inode:67146830 硬鏈接:1 權(quán)限:(0600/-rw-------) Uid:( 0/ root) Gid:( 0/ root) 環(huán)境:system_u:object_r:admin_home_t:s0 最近訪問:2019-05-31 17:36:00.109184712 +0800 最近更改:2019-05-31 17:36:00.110184571 +0800 最近改動(dòng):2019-05-31 17:36:00.111184429 +0800 創(chuàng)建時(shí)間:- 查看文件anaconda-ks.cfg的inode號(hào) [root@zcwyou ~]# ls -i -F anaconda-ks.cfg 67146830 anaconda-ks.cfg 使用mv命令對文件anaconda-ks.cfg移動(dòng) [root@zcwyou ~]# mv anaconda-ks.cfg 123.cfg 再次查看文件的inode號(hào) [root@zcwyou ~]# ls -i -F 123.cfg 67146830 123.cfg 可以看出在Linux系統(tǒng)中使用命令 mv 移動(dòng)并重命名文件123.cfg,其結(jié)果不影響文件的用戶數(shù)據(jù)及 inode 號(hào),文件移動(dòng)前后 inode 號(hào)均為:67146830。 為解決文件的共享使用,Linux 系統(tǒng)引入了兩種鏈接:硬鏈接 (hard link) 與軟鏈接(又稱符號(hào)鏈接,即 soft link 或 symbolic link)。鏈接為 Linux 系統(tǒng)解決了文件的共享使用,還帶來了隱藏文件路徑、增加權(quán)限安全及節(jié)省存儲(chǔ)等好處。若一個(gè) inode 號(hào)對應(yīng)多個(gè)文件名,則稱這些文件為硬鏈接。換言之,硬鏈接就是同一個(gè)文件使用了多個(gè)別名。hard link 就是 file 的一個(gè)別名,他們有共同的 inode)。硬鏈接可由命令 link 或 ln 創(chuàng)建。如下是對文件 oldfile 創(chuàng)建硬鏈接。 執(zhí)行命令 link 舊文件名 新文件名 或者 ln 舊文件名 新文件名 由于硬鏈接是有著相同 inode 號(hào)僅文件名不同的文件,因此硬鏈接存在以下幾點(diǎn)特性:
3. Linux創(chuàng)建硬鏈接 [root@CentOS7 ~]# ls -li 總用量 4 67146830 -rw-------. 1 root root 1408 5月 31 17:36 123.cfg 只能對已存在的文件創(chuàng)建硬連接 命令格式 link old.file hard.link 操作案例: [root@zcwyou ~]# cp 123.cfg anaconda-ks.cfg[root@zcwyou ~]# link anaconda-ks.cfg hard-link.cfg | ls -li 總用量 8 67146830 -rw-------. 1 root root 1408 5月 31 17:36 123.cfg 67146843 -rw-------. 2 root root 1408 6月 2 09:36 anaconda-ks.cfg [root@CentOS7 ~]# ls -li 總用量 12 67146830 -rw-------. 1 root root 1408 5月 31 17:36 123.cfg 67146843 -rw-------. 2 root root 1408 6月 2 09:36 anaconda-ks.cfg 67146843 -rw-------. 2 root root 1408 6月 2 09:36 hard-link.cfg 文件 anaconda-ks.cfg與 hard-link.cfg 有著相同的 inode 號(hào):67146843及文件權(quán)限,inode 是隨著文件的存在而存在,因此只有當(dāng)文件存在時(shí)才可創(chuàng)建硬鏈接,即當(dāng) inode 存在且鏈接計(jì)數(shù)器(link count)不為 0 時(shí)。inode 號(hào)僅在各文件系統(tǒng)下是唯一的,當(dāng) Linux 掛載多個(gè)文件系統(tǒng)后將出現(xiàn) inode 號(hào)重復(fù)的現(xiàn)象,文件比如文件系統(tǒng)1(比如/dev)的1.txt、文件系統(tǒng)2(比如/root)的2.txt 并無關(guān)聯(lián),卻有著相同的 inode 號(hào)),因此硬鏈接創(chuàng)建時(shí)不可跨文件系統(tǒng)。設(shè)備文件目錄 /dev 使用的文件系統(tǒng)是 devtmpfs,而 /root(與根目錄 / 一致)使用的是磁盤文件系統(tǒng) ext4。 不能交叉文件系統(tǒng) [root@zcwyou ~]# ln /dev/input/event5 /root/bfile.txt ln: 無法創(chuàng)建硬鏈接'/root/bfile.txt' => '/dev/input/event5': 無效的跨設(shè)備連接 不能對目錄進(jìn)行創(chuàng)建硬連接 [root@CentOS7 ~]# mkdir -p test_dir/test[root@CentOS7 ~]# ln test_dir/ test2.dir ln: 'test_dir/': 不允許將硬鏈接指向目錄 3.1 查找有相同 inode 號(hào)的文件 查看文件系統(tǒng) [root@zcwyou ~]# df -i --print-type 文件系統(tǒng) 類型 Inode 已用(I) 可用(I) 已用(I)% 掛載點(diǎn) /dev/mapper/centos-root xfs 19394560 29924 19364636 1% / devtmpfs devtmpfs 229908 394 229514 1% /dev tmpfs tmpfs 232906 1 232905 1% /dev/shm tmpfs tmpfs 232906 738 232168 1% /run tmpfs tmpfs 232906 16 232890 1% /sys/fs/cgroup /dev/sda1 xfs 524288 334 523954 1% /boot tmpfs tmpfs 232906 1 232905 1% /run/user/0 [root@zcwyou ~]# find / -inum /root/anaconda-ks.cfg /root/hard-link.cfg 找到兩個(gè)文件具有相關(guān)的inode號(hào)67146843,實(shí)際上是同一文件 硬鏈接不能對目錄創(chuàng)建是受限于文件系統(tǒng)的設(shè)計(jì)。現(xiàn) Linux 文件系統(tǒng)中的目錄均隱藏了兩個(gè)特殊的目錄:當(dāng)前目錄(.)與父目錄(..)。查看這兩個(gè)特殊目錄的 inode 號(hào)可知其實(shí)這兩目錄就是兩個(gè)硬鏈接(注意目錄 /mnt/lost+found/ 的 inode 號(hào))。若系統(tǒng)允許對目錄創(chuàng)建硬鏈接,則會(huì)產(chǎn)生目錄環(huán)。 4. Linux創(chuàng)建軟鏈接 軟鏈接與硬鏈接不同,若文件用戶數(shù)據(jù)塊中存放的內(nèi)容是另一文件的路徑名的指向,則該文件就是軟連接。軟鏈接就是一個(gè)普通文件,只是數(shù)據(jù)塊內(nèi)容有點(diǎn)特殊。軟鏈接有著自己的 inode 號(hào)以及用戶數(shù)據(jù)塊。 Linux創(chuàng)建軟鏈接 因此軟鏈接的創(chuàng)建與使用沒有類似硬鏈接的諸多限制:
4.1 Linux軟鏈接命令 命令格式:ln -s 源文件 軟鏈接新文件 [root@zcwyou ~]# ln -s anaconda-ks.cfg soft-link.cfg 查看軟鏈接文件 [root@CentOS7 ~]# ll soft-link.cfg lrwxrwxrwx. 1 root root 15 6月 2 09:47 soft-link.cfg -> anaconda-ks.cfg 創(chuàng)建目錄軟鏈接命令格式 ln -s 源目錄路徑 軟鏈接路徑 [root@CentOS7 ~]# ln -s test_dir/ soft_link_dir 查看軟鏈接目錄結(jié)果: bash [root@CentOS7 ~]# ll soft_link_dir ¨G16G bash [root@CentOS7 ~]# ls -li 總用量 12 67146830 -rw-------. 1 root root 1408 5月 31 17:36 123.cfg 67146843 -rw-------. 2 root root 1408 6月 2 09:36 anaconda-ks.cfg 67146843 -rw-------. 2 root root 1408 6月 2 09:36 hard-link.cfg 67146844 lrwxrwxrwx. 1 root root 15 6月 2 09:47 soft-link.cfg -> anaconda-ks.cfg 67146845 lrwxrwxrwx. 1 root root 9 6月 2 09:49 soft_link_dir -> test_dir/ 100673615 drwxr-xr-x. 3 root root 34 6月 2 09:51 test_dir 5. 總結(jié) 硬鏈接指通過索引節(jié)點(diǎn)來進(jìn)行連接。在Linux的文件系統(tǒng)中,保存在磁盤分區(qū)中的文件不管是什么類型都給它分配一個(gè)編號(hào),稱為索引節(jié)點(diǎn)號(hào)(Inode Index)。在Linux中,多個(gè)文件名指向同一索引節(jié)點(diǎn)是存在的。一般這種連接就是硬連接。硬連接的作用是允許一個(gè)文件擁有多個(gè)有效路徑名,這樣用戶就可以建立硬連接到重要文件,以防止“誤刪”的功能。其原因如上所述,因?yàn)閷?yīng)該目錄的索引節(jié)點(diǎn)有一個(gè)以上的連接。只刪除一個(gè)連接并不影響索引節(jié)點(diǎn)本身和其它的連接,只有當(dāng)最后一個(gè)連接被刪除后,文件的數(shù)據(jù)塊及目錄的連接才會(huì)被釋放。也就是說,文件真正刪除的條件是與之相關(guān)的所有硬連接文件均被刪除。 另外一種連接稱之為符號(hào)連接(Symbolic Link),也叫軟連接。軟鏈接文件有類似于Windows的快捷方式。它實(shí)際上是一個(gè)特殊的文件。在符號(hào)連接中,文件實(shí)際上是一個(gè)文本文件,其中包含的有另一文件的位置信息。 本文已同步至博客站,尊重原創(chuàng),轉(zhuǎn)載時(shí)請?jiān)谡闹懈綆б韵骆溄樱?/p> https://www./rmxx/1023.html |
|
|