對硬鏈結的認識

2021-09-24 21:08:19 字數 3414 閱讀 1902

[root@localhost ~]# ls -al anaconda-ks.cfg 

-rw-------. 1 root root 1614 jan 20 17:32 anaconda-ks.cfg

drwxr-xr-x. 2 root root 6 jan 20 17:55 desktop

許可權位共11位

第1位 -表示檔案 d表示目錄

2-10位 三個一組 分別表示屬主、屬組、其他使用者的許可權

第11位 . 表示的是selinux安全標籤,如果檔案在selinux是enforcing的狀態下建立就會有

如果在selinux是disabled或者permissive的情況下建立檔案則不會有

許可權位後邊的數字是當前檔案或者目錄的硬鏈結數量

後邊分別表示屬主 屬組 檔案最後修改時間

下邊通過命令來說明一下selinux安全標籤

[root@localhost ~]# mkdir test

[root@localhost ~]# cd test

[root@localhost test]# touch a.txt

[root@localhost test]# ll a.txt

-rw-r--r--. 1 root root 0 jun 30 22:41 a.txt

[root@localhost test]# getenforce

enforcing

[root@localhost test]# setenforce 0

[root@localhost test]# getenforce

permissive

[root@localhost test]# touch b.txt

[root@localhost test]# ll b.txt

-rw-r--r--. 1 root root 0 jun 30 22:42 b.txt

[root@localhost test]# 發現臨時調整不可以,其實需要通過配置檔案永久調整才行

[root@localhost test]# vim /etc/selinux/config

selinux=disabled

[root@localhost test]# reboot

root@localhost ~]# cd test/

[root@localhost test]# getenforce

disabled

[root@localhost test]# touch c.txt

[root@localhost test]# ll

total 0

-rw-r--r--. 1 root root 0 jun 30 22:41 a.txt

-rw-r--r--. 1 root root 0 jun 30 22:42 b.txt

-rw-r--r-- 1 root root 0 jun 30 22:47 c.txt

可以看到新建立的檔案沒有selinux安全標籤

軟鏈結檔案有原檔案的inode節點不同

硬鏈結檔案與原檔案的inode節點相同

[root@localhost test]# cd

[root@localhost ~]# mkdir testdir

[root@localhost ~]# cd testdir/

[root@localhost testdir]# touch test.txt

[root@localhost testdir]# ln -s test.txt /etc/t1.txt

[root@localhost testdir]# ln test.txt /etc/t2.txt

[root@localhost testdir]# ls -i test.txt

68653591 test.txt

[root@localhost testdir]# ls -i /etc/t1.txt

68653596 /etc/t1.txt

[root@localhost testdir]# ls -i /etc/t2.txt

68653591 /etc/t2.txt

[root@localhost test]# pwd

/root/test

[root@localhost test]# ls

a.txt b.txt c.txt

[root@localhost test]# mkdir a

[root@localhost test]# ls -al

total 4

drwxr-xr-x. 3 root root 54 jun 30 22:49 .

dr-xr-x---. 17 root root 4096 jun 30 22:46 ..

drwxr-xr-x 2 root root 6 jun 30 22:49 a

-rw-r--r--. 1 root root 0 jun 30 22:41 a.txt

-rw-r--r--. 1 root root 0 jun 30 22:42 b.txt

-rw-r--r-- 1 root root 0 jun 30 22:47 c.txt

對於當前目錄下 的三個檔案來說 硬鏈結只有1個 不難理解 這個硬鏈結就是自己 也就是說 自己是對自己的乙個硬鏈結

這樣說,沒有什麼毛病 因為inode節點相同 而且檔案也一樣

新建立的a目錄有2個硬鏈結,乙個是自己,也就是名字是a的這個目錄,另外乙個是a目錄裡邊的.目錄

如果在a目錄下建立b目錄,a目錄的硬鏈結數會+1,因為b目錄裡的..目錄跟a目錄裡的.目錄是乙個硬鏈結,

也是因為這個原因,在b目錄cd .. 會切換到上層目錄a

[root@localhost test]# mkdir a/b

[root@localhost test]# ls -al

total 4

drwxr-xr-x. 3 root root 54 jun 30 22:49 .

dr-xr-x---. 17 root root 4096 jun 30 22:46 ..

drwxr-xr-x 3 root root 15 jun 30 22:53 a

-rw-r--r--. 1 root root 0 jun 30 22:41 a.txt

-rw-r--r--. 1 root root 0 jun 30 22:42 b.txt

-rw-r--r-- 1 root root 0 jun 30 22:47 c.txt

從輸出結果來看 ..目錄的硬鏈結是17個 而上層目錄是root目錄,排除root目錄自己的名字和root目錄下的.目錄,

可以知道root目錄下有15個目錄

認識Linux系統中的inode,硬鏈結和軟鏈結

1.inode 在linux系統中,每個檔案都有乙個inode 索引節點 inode儲存了檔案的元資訊,主要有 檔案的位元組數 檔案的使用者id 檔案的組id 檔案的read,write,execute許可權 檔案的時間戳 最近的產生時間,最近一次訪問時間,最近一次修改時間 鏈結數 inode所使用...

軟鏈結 硬鏈結

硬鏈結與軟鏈結的聯絡與區別 我們知道檔案都有檔名與資料,這在 linux 上被分成兩個部分 元資料 metadata 是檔案的附加屬性,如檔案大小 建立時間 所有者等資訊。使用者資料 user data 即檔案資料塊 data block 資料塊是記錄檔案真實內容的地方 元資料則在 linux 中,...

軟鏈結 硬鏈結

硬鏈結就是同一塊資料但有兩個不同的名字,讀寫的時候本質上都是修改的同一塊資料。硬鏈結在刪除的時候只是刪除了乙個名字。只有一塊資料的所有名字都刪除了的時候,資料才會被刪除。刪除的時候相當於把牆的這一面糊上,但蘋果本身不動,只有兩面都糊上了,你才會看不到 符號鏈結就是 我聲稱我有乙個蘋果,但是當你找我要...