跳至內容

LPI Linux 認證/LPIC1 考試 101/Linux 檔案系統

來自華夏公益教科書,開放的書籍,開放的世界
LPI logo

章節概述

[編輯 | 編輯原始碼]
  • 1.104.1 建立分割槽和檔案系統
  • 1.104.2 維護檔案系統的完整性
  • 1.104.3 控制掛載和解除安裝檔案系統
  • 1.104.4 管理磁碟配額
  • 1.104.5 使用檔案許可權控制對檔案的訪問
  • 1.104.6 管理檔案所有權
  • 1.104.7 建立和更改硬連結和符號連結
  • 1.104.8 查詢系統檔案並將檔案放置在正確的位置


  • Linux 檔案系統
  • 建立分割槽和檔案系統
  • 維護檔案系統的完整性
  • 控制掛載和解除安裝檔案系統
  • 管理磁碟配額
  • 使用檔案許可權控制對檔案的訪問
  • 管理檔案所有權
  • 建立和更改硬連結和符號連結

建立分割槽和檔案系統

[編輯 | 編輯原始碼]

描述: 候選人應能夠配置磁碟分割槽,然後在硬碟等介質上建立檔案系統。本目標包括使用各種 mkfs 命令來設定分割槽到各種檔案系統,包括 ext2、ext3、reiserfs、vfat 和 xfs。

關鍵檔案術語實用程式包括
fdisk
mkfs

分割槽

[編輯 | 編輯原始碼]

介質可以劃分為分割槽。分割槽通常在安裝時建立,但也可以使用 fdisk 程式或其他實用程式建立。這將把介質劃分為分割槽,並且可以構建不同的檔案系統,並且可以安裝不同的作業系統。

IDE 被識別如下

  • 主盤
/dev/hda : 完整磁碟
/dev/hda1: 第一個分割槽
/dev/hda2: 第二個分割槽
  • 從盤: /dev/hdb
  • 輔盤: /dev/hdc
  • 從盤: /dev/hdd

SCSI 被識別如下

  • ID1
/dev/sda: 完整磁碟
/dev/sda1: 第一個分割槽
  • ID2: /dev/sdb

USB 和 FireWire 磁碟被識別為 SCSI 磁碟。

一旦分割槽完成,就可以在每個分割槽上構建一個檔案系統。

檔案系統

[編輯 | 編輯原始碼]

檔案系統存在是為了讓你能夠在介質上儲存、檢索和操作資料。檔案系統維護一個內部資料結構(元資料),它使所有資料井井有條並易於訪問。此元資料的結構賦予了檔案系統的特徵。檔案系統透過組織的元資料結構由驅動程式訪問。當 Linux 啟動時,它會讀取 /etc/fstab 中所有需要掛載的檔案系統,並檢查它們是否處於可用狀態。

當電源故障發生時,Linux 將無法正確解除安裝檔案系統,並且快取中的一些資料將不會同步到介質上。元資料可能會損壞。

重新啟動系統後,它會檢測到這一點並對整個元資料結構執行 fsck 以進行一致性檢查。這可能需要很長時間。幾分鐘到幾小時,與介質大小成正比。對檔案系統進行日誌記錄是在磁碟上新增一個名為日誌的新資料結構。在驅動程式對元資料進行每次修改之前,它首先被寫入日誌。在每次元資料修改之前,日誌都會維護接下來操作的日誌。

現在,當電源故障發生時,只需要檢查日誌。日誌記錄的檔案系統恢復非常快。它只需要遍歷日誌並修復最新的操作。日誌記錄的檔案系統恢復只需要幾秒鐘。

在集群系統上,日誌記錄允許快速恢復已關閉節點的共享分割槽。

Linux 檔案系統

[編輯 | 編輯原始碼]
  • ext2: 舊的非常穩定的 Linux 檔案系統。對於大於 ~2-3K 的檔案來說效率很高。
  • ext3: ext2 的日誌記錄擴充套件。可以將檔案系統在 ext2 和 ext3 之間來回移動。
  • Reiserfs: 日誌記錄檔案系統。在操作小檔案時比 ext2 快 8-15 倍。
  • XFS: 一個強大的日誌記錄檔案系統,具有可能的配額和 ACL
  • Msdos: MS-Windows FAT 檔案系統型別。(主要用於軟盤)
  • Vfat: MS-Windows FAT 檔案系統型別。 (主要用於大型硬碟分割槽)
  • NTFS(只讀,但迴圈檔案):MS-Windows 日誌檔案系統
  • SMBFS:從 Linux 掛載 Windows 或 Samba 共享的檔案系統
  • NFS:網路檔案系統

...

檔案系統樹

[edit | edit source]

Linux 檔案系統有一個名為根 (/) 的頂級目錄,其中儲存了整個系統的所有子目錄。 子目錄可以是另一個分割槽、遠端目錄或透過 NFS 協議訪問的遠端分割槽。

建立檔案系統

[edit | edit source]

要在分割槽上建立檔案系統,請使用 mkfs。

mkfs [options] -t [fstype] device [blocksize]

常見選項

-t: fstype: 檔案系統型別。
-c : 在構建檔案系統之前檢查裝置是否有壞塊。

整個分割槽將被擦除並組織為請求的檔案系統型別。 沒有撤消命令。 可用的 fstype 為:msdos、ext2、ext3、reiserfs、minix、xfs

塊大小允許您自定義檔案系統的塊大小。

示例

mkfs -t msdos /dev/fd0
mkfs -t reiserfs /dev/hdd1  4096

建立擴充套件檔案系統

[edit | edit source]

要在分割槽上建立擴充套件 (ext2、ext3) 檔案系統,請使用 mke2fs。

mke2fs [options] device [blocksize]

常見選項

-b: 指定塊大小 fstype: 檔案系統型別。
-c : 在構建檔案系統之前檢查裝置是否有壞塊。
-j: 使用 ext3 日誌建立檔案系統。
-L: 為檔案系統設定卷標。

使用 mke2fs,可以將超級塊和日誌資訊儲存在另一個裝置上。 示例

mkefs -b 2048 /dev/fd0 -L floppy
mkfs -V
mke2fs 1.26 (3-Feb-2002) Using EXT2FS Library version 1.263

監控磁碟使用情況

[edit | edit source]

要列印磁碟使用情況,請使用 du。

du [options] [files...]

常見選項

-a: 所有檔案,不僅僅是目錄
-b: 以位元組為單位列印大小
-c: 總計
-h: 人類可讀格式。 (1K、20M、...)

示例

$ du -ch Documents
112k    Documents/Cours/LPI101
4.0k    Documents/Cours/LPI102
4.0k    Documents/Cours/LPI201
4.0k    Documents/Cours/LPI202
124k    total
du -sk ~ # Sums up your total disk usage in kilobytes
du -ak ~ | sort -n more # Display every file and its disk space in numerical order.

檔案系統磁碟空間

[edit | edit source]

檔案系統由元資料結構加上塊列表組成。 要列印檔案系統磁碟空間使用情況,請使用 df。

df [options] [files...]

常見選項

-a: 包含所有包含 0 個塊的檔案系統。
-t: 將列表限制為檔案系統型別。
-h: 人類可讀格式。 (1K、20M、...)

示例

$ df -t reiserfs -h
F           1k-blocks      Used Available Use% Mounted on
/dev/hda3             28771528   3121536  25649992  11% /
$ df -t ext2 -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda1              15M  3.8M   10M  27% /boot

練習

[edit | edit source]

維護檔案系統的完整性

[edit | edit source]

概述

[edit | edit source]

描述:候選人應該能夠驗證檔案系統的完整性、監控可用空間和 inode,並修復簡單的檔案系統問題。 此目標包括維護標準檔案系統所需的命令,以及與日誌檔案系統相關的額外資料。

關鍵檔案術語實用程式包括
du
df
fsck
e2fsck
mke2fs
debugfs
dumpe2fs
tune2fs

檢查檔案系統

[edit | edit source]

要檢查檔案系統一致性,請使用 fsck。

fsck [options] -t [fstype] device [fsck-options]

常見選項

-A: 遍歷 /etc/fstab 檔案並嘗試檢查所有檔案系統。 通常在引導時從指令碼中使用。
-t fslist: 指定要檢查的檔案系統型別。 使用 -A 時,只檢查與 fslist 匹配的檔案系統
-C: 顯示完成/進度條。

常見 fsck 選項

-a: 自動修復。
-r: 互動式修復。

示例

fsck -t msdos /dev/fd0 -a
fsck -t reiserfs /dev/hda2 -r

檢查擴充套件檔案系統

[edit | edit source]

要檢查擴充套件檔案系統一致性,請使用 e2fsck。

e2fsck [options] device

常見選項

-b: 使用備用超級塊檔名。
-c: 此選項使 badblocks 程式執行並標記所有壞塊。
-f: 即使檔案系統看起來很乾淨,也要強制檢查。
-a 或 -p: 自動修復。
-y: 非互動模式

示例

e2fsck -ay /dev/fd0
e2fsck -f /dev/hda2

除錯擴充套件檔案系統

[edit | edit source]

debugfs 程式是一個互動式檔案系統偵錯程式。 它可以用來檢查和改變 ext2 檔案系統的狀態。

debugfs device

常見命令

Help: 提供所有可用命令的幫助。
Stats: 提供介質的統計資訊。
Stat file: 提供介質上檔案的統計資訊。
logdump: 將日誌介質轉儲到檔案

示例

stat haut.gif
Inode: 14   Type: regular    Mode:  0644   Flags: 0x0   Generation: 67558
User:     0   Group:     0   Size: 3786
File ACL: 0    Directory ACL: 0
Links: 1   Blockcount: 8
Fragment:  Address: 0    Number: 0    Size: 0
ctime: 0x3ddf3840 -- Sat Nov 23 09:11:44 2002
atime: 0x3ddf3840 -- Sat Nov 23 09:11:44 2002
mtime: 0x3ddf3840 -- Sat Nov 23 09:11:44 2002
BLOCKS:
(0-3):55-58
TOTAL: 4

轉儲擴充套件檔案系統資訊

[edit | edit source]

要列印擴充套件檔案系統的超級塊和塊組資訊,請使用 dumpe2fs。

dumpe2fs [options] device

常見選項

-b: 列印檔案系統的壞塊。
-h: 只顯示超級塊資訊。

示例

dumpe2fs -h /dev/fd0
dumpe2fs 1.26 (3-Feb-2002)
Filesystem volume name:   floppy
Last mounted on:          <not available>
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              184
Block count:              1440
Reserved block count:     72
Free blocks:              1258
Free inodes:              168
First block:              1
Block size:               1024
First inode:              11
Inode size:               128
...

調整擴充套件檔案系統

[edit | edit source]

要調整擴充套件檔案系統,請使用 tune2fs。

tune2fs [options] device 

常見選項

-i#: 檔案系統檢查之間的間隔 [d|m|w]。
-l: 列出檔案系統超級塊的內容。
-L: 設定檔案系統的卷標。

示例

tune2fs -L floppy /dev/fd0
tune2fs -l /dev/fd0
(Same output as dumpe2fs -h /dev/fd0)
tune2fs 1.26 (3-Feb-2002)
Filesystem volume name:   floppy
Block count:              1440
Reserved block count:     72
Free blocks:              1258
Free inodes:              168
First block:              1
Block size:               1024
First inode:              11
Inode size:               128
...

練習

[edit | edit source]
  1. 在軟盤上構建一個 ext2 檔案系統,塊大小為 2048 位元組。
  2. 將軟盤的標籤更改為 BACKUP。
  3. 嘗試在軟盤介質上新增一個日誌。
  4. 使用 debugfs 驗證軟盤檔案系統資訊,並檢查上次訪問時間。
  5. 使用 watch 監控複製大型檔案時的大小。
  6. 建立一個 shell 指令碼,列出軟盤上所有大於 100 Kb 的檔案。
  7. 顯示所有 MSDOS 檔案系統的檔案系統使用情況。
  8. 哪個目錄必須存在於 / 中才能將此作業系統歸類為 Linux?
  9. /proc 的檔案系統使用情況如何?

.

附加和分離檔案系統

[edit | edit source]
  • 控制掛載和解除安裝檔案系統

概述

[edit | edit source]

描述:候選人應該能夠配置檔案系統的掛載。 此目標包括能夠手動掛載和解除安裝檔案系統、在啟動時配置檔案系統掛載,以及配置使用者可掛載的可移動檔案系統,例如磁帶機、軟盤和 CD。

關鍵檔案術語實用程式包括
/etc/fstab
mount
umount

附加檔案系統

[edit | edit source]

mount 命令用於將某些裝置上的檔案系統附加到大型檔案樹中。

mount [options]
mount [options] [-t vfstype] [-o options] device dir

如果裝置或目錄列在 /etc/fstab 中,您可以使用以下命令

mount [options] [-o options [,...]] device | dir

通常只有 root 才有掛載裝置的許可權,除非在 /etc/fstab 檔案中指定。 示例

# Print all the mounted filesystems (/etc/mtab).
mount
# Mount devices or dirs listed in /etc/fstab.
mount -a
# Mount /dev/hdc partition in read only mode without updating /etc/mtab.
mount -n -o ro /dev/hdc /mnt
# Allow a user to mount the CDROM if the following line is in /etc/fstab:
# /dev/cdrom /media/cdrom iso9660 ro,user,noauto,unhide
mount /media/cdrom 
mount /dev/cdrom
# Sync in realtime
mount -o sync /dev/sdb1 /mnt/usb

分離檔案系統

[edit | edit source]

要將檔案系統從檔案樹中分離,請使用 umount。

umount [options]
umount [options] [-o options [,...]] device | dir

繁忙的檔案系統無法解除安裝。

  • 開啟檔案
  • 程序的工作目錄。

示例

umount -a # Unmount devices or dirs listed in /etc/fstab.
umount /mnt # Unmount the filesystem attached to /mnt.
umount /media/cdrom  # Allow a user to unmount the CDROM if the following line is in /etc/fstab:
/dev/cdrom  /media/cdrom  iso9660  ro,user,noauto,unhide

檔案系統資訊

[edit | edit source]

/etc/fstab 檔案包含執行 mount -a 時將使用到的所有檔案系統及其相關資訊。(引導時)

/etc/mtab 檔案由核心維護,並跟蹤已掛載或未掛載的內容。/etc/fstab 的格式為

#Device     Mount point    Fs type  Options             1 2
/dev/hda3   /              reiserfs defaults            1 2
/dev/hda1   /boot          ext2     defaults            1 2
/dev/cdrom  /media/cdrom   auto     ro,noauto,user,exec 0 0
usbdevfs    /proc/bus/usb  usbdevfs noauto              0 0
/dev/hda2   swap           swap     pri=42              0 0

常見選項

ro: 只讀
noauto: 不要自動掛載
exec: 可以在檔案系統上執行二進位制檔案
suid: 允許設定使用者位
user: 允許使用者掛載/解除安裝它
unhide: 隱藏檔案可見
async: 所有操作都將非同步執行
default: rw、suid、dev、exec、auto、nouser 和 async

練習

[edit | edit source]
  1. 在 /etc/fstab 中建立一行,允許任何使用者訪問軟盤。檢查是否可以掛載軟盤,並可以使用 touch 建立檔案。
  2. 執行以下操作
    • 在軟盤上建立 ext2 檔案系統。
    • 掛載軟盤。
    • 將所有 /etc/*.conf 檔案複製到軟盤。
    • 解除安裝它。發生了什麼事?
    • 重新掛載並檢查所有檔案是否都在那裡。
    • 發出以下命令
    • Tar cvf /dev/fd0 /etc/*.conf
    • 嘗試重新掛載它。發生了什麼事?
    • 使用 tar 檢視軟盤的內容。

管理磁碟配額

[edit | edit source]

概述

[edit | edit source]

描述: 考生應該能夠管理使用者的磁碟配額。本目標包括為檔案系統設定磁碟配額,編輯、檢查和生成使用者配額報告。

關鍵檔案術語實用程式包括
quota
edquota
repquota
quotaon

  • 管理檔案系統的配額
  • 列印配額報告
  • 管理磁碟配額

配額

[edit | edit source]

在系統上,root 可以管理每個使用者和每個檔案系統的磁碟空間使用情況。可以設定的兩個限制是:軟限制 (soft =) 指定配額使用者允許擁有的最大磁碟使用量。硬限制 (hard =) 指定配額使用者無法超過的磁碟使用量的絕對限制。還可以設定寬限期,僅在指定的時間段後強制執行軟限制。

為使用者設定配額

[edit | edit source]

1) 必須在要關注的分割槽的 /etc/fstab 檔案中新增關鍵字 usrquota 或/和 grpquota。

/dev/fd0  /home/yann/mnt auto    rw,noauto,user,usrquota 0 0
/dev/hda5 /home     ext2    defaults,usrquota,grpquota 1 2

2) 在每個 root 檔案系統中新增 user.quota 或/和 group.quota 檔案。

touch /mnt/aquota.user
touch /home/aquota.user
touch /home/aquota.group 
chmod 600  /mnt/aquota.user 
chmod 600  /home/aquota.user 
chmod 600  /home/aquota.group

只有 root 可以執行配額管理,並且建立空檔案後,可以設定一些磁碟配額,例如

  • 檔案和 inode 數量的軟限制。
  • 如果設定了寬限時間,檔案和 inode 數量的硬限制。

為使用者設定配額

[edit | edit source]

3) 檢查設定

quotacheck -v mnt
quotacheck: Scanning /dev/fd0 [/home/yann/mnt] done
quotacheck: Checked 6 directories and 1 files

4) 在磁碟上啟用配額

quotaon -av
/dev/fd0 [/home/yann/mnt]: user quotas turned on

5) 自定義磁碟配額限制

$ edquota -u yann
Disk quotas for user yann (uid 500):
Filesystem    locks       soft       hard     inodes     soft     hard
/dev/fd0       15          0          0          4        0        0
$ edquota -g yann 
$ edquota -t
Grace period before enforcing soft limits for users:
Time units may be: days, hours, minutes, or seconds
Filesystem             Block grace period     Inode grace period
/dev/fd0                      7days                  7days

列出配額

[edit | edit source]

要列出使用者或組的配額,請使用 quota。

quota [options] [user|group]

常見選項

-u: 預設情況下,列印使用者配額。
-g: 列印使用者所屬組的組配額。
-q: 列印更簡潔的訊息,僅包含有關使用量超過配額的檔案系統的資訊。

示例

quota -u yann

顯示配額報告

[edit | edit source]

要顯示配額報告,請使用 repquota。

repquota [options] [user|group]

常見選項

-a: 報告 /etc/mtab 中指示為讀寫並帶有配額的所有檔案系統。
-g: 報告組。

示例

$ repquota /dev/fd0
*** Report for user quotas on device /dev/fd0
Block grace time: 7days; Inode grace time: 7days
                Block limits                   File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root   --       8       0       0              2     0     0
yann   --      15       0       0              4     0     0

練習

[edit | edit source]
  1. 為所有主目錄在 /home 的使用者設定 500M 的軟限制。
  2. 將寬限時間更改為 0。
  3. 以使用者身份登入並檢查限制是否有效。

檔案許可權和安全性

[edit | edit source]

概述

[edit | edit source]

Unix 檔案安全性 檔案和目錄許可權 預設許可權 更改檔案許可權 更改檔案所有者和組 更多許可權 Unix 檔案安全性 最大的安全問題可能是你。你擁有你建立的任何目錄或檔案。你負責你的檔案的可訪問性。你決定誰可以訪問哪些檔案和目錄。

在你的主目錄中,你將能夠對你自己、你組中的使用者以及所有其他使用者授予不同的許可權級別。

檔案和目錄許可權

[edit | edit source]

可以使用 ls -l 檢視檔案或目錄的許可權。

檔案許可權

[edit | edit source]

檔案許可權示例

ls -l readme
-rwxrw---- 1 toto users 14 Jul 5 10:00 readme

這意味著使用者 toto 具有讀、寫和執行許可權,組 users 的成員具有讀寫許可權。其他人沒有許可權。(0760)

ls -l /etc/hosts
-rw-r--r-- 1 root root 14 Jul 5 10:00 /etc/hosts

這意味著使用者 root 具有讀寫許可權,組 root 的成員和所有其他人具有讀許可權。(0644)

目錄許可權示例

ls -ld /bin
drwxr-xr-x 2 root root 4096 Jul 5 10:00 /bin

這意味著使用者 root 具有讀、寫和執行許可權,組 root 的成員和其他人具有讀和執行許可權。(0755)

ls -l /home/toto
drwxr-xr-x 10 toto  users 4096 Jul 5 1:00 /home/toto

這意味著使用者 toto 具有讀、寫和執行許可權,組 users 的成員和其他人具有讀和執行許可權。(0755)

預設許可權

[edit | edit source]

建立檔案時的預設許可權為 0666,建立目錄時的預設許可權為 0777。大多數系統會在引導時使用 umask 程式覆蓋此許可權。通常掩碼值為 022。這意味著組和其他使用者的寫入許可權將被阻止。要檢查或更改掩碼值,請執行

umask 
umask 066  

檔案示例

default: rw- rw- rw- (0666)
umask: 0 2 2 (0022) Block
result: rw- r-- r-- (0644)

目錄示例

default: rwx rwx rwx (0777)
umask: 0 2 2 (0022) Block
result: rwx r-x r-x (0755)

更改檔案許可權

[edit | edit source]

要更改檔案或目錄的許可權,請使用 chmod。要覆蓋現有許可權,請執行

chmod 0755 /tmp #rwx for user, rx for group and others

要更改新增或取消某些許可權而不覆蓋所有現有許可權,請執行

chmod u+w readme  # Add write permission for user
chmod +r readme  # Add read permission for everybody
chmod -r readme  # Remove read permission for everybody
chmod u+x,g=r readme  # Add execution for user and set read for group
chmod u=rwx,go=rx readme  # Set read write and execution for user, read and execution for group and others

要以遞迴模式更改,請使用 -R 選項。

chmod -R +x /sbin/*

管理檔案所有權

[編輯 | 編輯原始碼]

描述: 考生應能夠控制使用者和組對檔案的擁有權。此目標包括能夠更改檔案的使用者和組所有者,以及新檔案的預設組所有者。

關鍵檔案術語實用程式包括
chmod
chown
chgrp

更改檔案所有者和組

[編輯 | 編輯原始碼]

要更改檔案或目錄的所有者,請使用 chown。

chown yann mon_fichier.txt

要更改檔案或目錄的組,請使用 chgrp。

chgrp dialout caller

程式 gpasswd 和 yast2 允許您管理組。

gpasswd [-A user,...] [-M user,...] group

-A: 新增具有組管理員許可權的使用者。
-M: 新增組中的成員。

組管理員可以新增或刪除組成員。

gpasswd -d toto users
gpasswd -a toto users

組管理員可以設定或刪除組的密碼。

gpasswd users
gpasswd -r users

更多許可權

[編輯 | 編輯原始碼]

當用戶執行特定指令碼或程式時,可以透過設定檔案的 uid 或 gid 位來為使用者提供更多許可權。

如果設定了該位,程序將繼承檔案的擁有者的許可權,而不是使用者的許可權。要設定有效 uid 或 gid,請使用 chmod。

chmod 2640 [file] # (2) gid is inheritable for group.
chmod 4640 [file] # (4) uid is inheritable for user.

/bin/passwd 是此類程式的示例。

還可以設定粘性位,可以使程式文字段常駐記憶體。chmod 1640 [file] (1) 檔案程式保留在記憶體中。

1) 使用 chmod 中的字母編寫命令列以設定以下許可權

rwxrwxr-x :
rwxr--r-- :
r--r----- :
rwxr-xr-x :
rwxr-xr-x :
r-x--x--x :
-w-r----x :
-----xrwx :

2) 使用 chmod 中的八進位制數字編寫命令列以設定以下許可權

rwxrwxrwx :
--x--x--x :
r---w---x :
-w------- :
rw-r----- :
rwx--x--x :

3) 對於以下 umask 值,檔案和目錄的建立許可權將是什麼?

umask = 0027
File permissions:
Directory permissions:
umask = 0011
File permissions:
Directory permissions:
umask = 0541
File permissions:
Directory permissions:
umask = 0777
File permissions:
Directory permissions:

4) 建立兩個使用者帳戶

登入 ID: tst1,組 users,使用 bash shell,主目錄 /home/tst1
登入 ID: tst2,組 public,使用 bash shell,主目錄 /home/tst2
為這兩個帳戶設定密碼。

以 tst1 的身份登入,並將 /bin/ls 複製到 tst1 的主目錄中,命名為 myls。將 myls 的所有者更改為 tst1,並將許可權更改為 0710。此許可權值意味著什麼?

以 tst2 的身份登入,嘗試使用 /home/tst1/myls 列出您當前的目錄。它工作嗎?

在 /etc/group 和 /etc/gshadow 中建立一個新的組 labo,其中包含 tst1 和 tst2。將 myls 的所有者組更改為 labo。

再次從 tst2 帳戶嘗試執行 /home/tst1/myls 以列出您當前的目錄。它工作嗎?

[編輯 | 編輯原始碼]

描述: 考生應能夠建立和管理指向檔案的硬連結和符號連結。此目標包括能夠建立和識別連結,透過連結複製檔案,以及使用連結檔案來支援系統管理任務。

關鍵檔案術語實用程式包括
ln

在以下情況下使用連結:您想建立一個指向檔案的路徑名。為檔案設定更短或固定的路徑名。

要將一個檔案連結到另一個檔案,請使用 ln。

ln [options] filename linkname
ln [options] filename linkdirectory

常見選項

-f 強制:覆蓋現有連結
-s 符號連結

預設連結是硬連結(沒有選項的 ln)。在 Windows 上,它們被稱為快捷方式。硬連結只能建立到同一物理裝置上的現有檔案,建立後,無法顯示連結名稱和檔名之間的任何可見關聯。符號連結可以建立到尚未存在的檔案,可以使用 ls 命令檢視連結名稱和檔名之間的關聯。

連結到檔案

[編輯 | 編輯原始碼]

可以使用 ls -l 顯示符號連結和硬連結。符號連結用箭頭表示:link_name->real_filename。

$ ls -l /dev/midi
lrwxrwxrwx   1   root   root        6    Jul 4 21:50   /dev/midi -> midi00

硬連結用連結計數器表示(在本例中為 3-1=2)。

$ ls -l readme
-rwxrwxrwx   3   yann   users       677  Jul 4 21:50   readme

刪除連結名稱時,請使用 rm。只會刪除連結,不會刪除連結的檔案。

  1. 在您的主目錄中建立一個名為 etc 和 bin 的目錄。
  2. 以遞迴模式將 /etc 中的所有檔案複製到您的 etc 目錄中,並將 /bin 中的所有檔案複製到 bin 中。
  3. 在您本地的 etc 目錄中,將所有 *.conf 檔案重新命名為 *.conf.bak。
  4. 在您的主目錄中建立一個名為 dir 的符號連結,指向您本地的 bin/ls。檢查 dir 是否執行 ls。
  5. 刪除 dir 連結。bin/ls 還在嗎?

查詢系統檔案並將檔案放置在正確的位置

[編輯 | 編輯原始碼]
華夏公益教科書