NCNU-OpenSource
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ###### tags: `lsa` [TOC] # FHS 檔案系統階層標準 * 全名 : Filesystem Hierarchy Standard 檔案系統階層標準 * 定義了 Linux 作業系統中的主要目錄結構與目錄內容 * 讓使用者了解檔案應該在那些位置 * 由 Linux Foundation 進行維護及協同管理 * 為甚麼要有 FHS? * 使用 Linux 來開發軟體的人越來越多 * 大家的標準都不同,難管理 * 所以制定一個標準,希望大家遵守 :::info * **什麼是 FileSystem** * 是一種 Method、資料結構,讓作業系統可以存取資料 * 如果沒有 FileSystem * 資料都存在同一個磁區中 * 不知道從哪裡開始讀取、什麼時候讀取結束 * 如果有 FileSystem * 資料會切割並命名 * 每一組資料就稱為 `file` (檔案) ::: ## FHS 架構 ![](https://i.imgur.com/hS53GKi.png) * FHS 一層一層的資料結構,像 tree * FHS 定義出以下這三層目錄下應放什麼資料 * `/` : 一切的起始點 * `/usr` : 與軟體、安裝執行有關 * `/var` : 跟系統運作有關 * 萬物皆檔案 :::info **Windows Filesystem v.s. Linux Filesystem** - Windows Filesystem - 檔案存在不同 driver (硬碟) 中 - 一個 driver (硬碟) 一個 Filesystem - 用 driver letter 區分分割區 - 每一個硬碟都用一個字母表示 - 例如: `C:`, `D:`, `E:` - 通常 `C:` 是主分割區 - Linux Filesystem - 所有檔案都存在 `/` 下,按照樹狀結構排序 - driver 的檔案必須掛載到檔案系統下才可以讀取 ::: ## `/` 根目錄與他的子目錄 * `/` 萬物之根源,稱為 root,是系統最重要的目錄 * FHS 規範什麼檔案要放在什麼目錄下 * 避免將各種檔案放在 root 的分割區內,減少系統出錯 * 如果檔案放在不同分割區,其中一個地方壞掉,只有那個目錄被弄壞,不會影響其他的地方 * 如果所有檔案都放在同一個分割區裡,只要磁區有 `1 bit` 壞掉,系統就可能會 crash ### 根目錄下需要存在的目錄 - `/bin` = binary - 放置可以被 root 與一般使用者使用的[基本指令](https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s04.html) - 例如: `cat`, `mv`, `cp`, `which` - `/sbin` - `s` 就是 Super user , 是 Super user Binaries - [放系統管理指令或是只有 root 可以使用的指令](https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s16.html) - 開機/修復/還原系統 所需要的指令 - 例如:`shutdown`, `reboot`, `ifconfig` :::info **ifconfig** - 安裝指令 ```bash= sudo apt install net-tools ``` - 查看所有已啟動的網路介面卡 - ![](https://i.imgur.com/hWnGC24.png) - 關閉網卡 :point_right: 就不能上網了 ```bash= ifconfig <網卡名稱> down ``` - 開啟網卡 :point_right: 要記得網卡名稱 ```bash= ifconfig <網卡名稱> up ``` - 查看所有網卡 ```bash= ifconfig -a ``` ::: - `/lib` = library (函式庫) - 存放所有基礎系統需要用的 library - 放 `/bin` 或 `/sbin` 底下指令的 library :::info **什麼是 library 函式庫?** - library 是用在開發軟體的子程式集合 - 生活化的例子:食譜書 - 食譜中的目錄 :point_right: library - 包含各個食譜 :point_right: 常用的 Function - 在食譜目錄中選擇麻婆豆腐來料理 :point_right: 在 library 中選擇想要呼叫的 Function ::: - `/boot` = bootstrap - 放開機會使用到的檔案 - 包括 Linux kernel 檔案以及開機選單與開機所需的設定檔 - 例如 : `initrd.img` - Initial ramdisk - 開機時會先在記憶體中載入一個暫時的根目錄系統 - 包含真正的 `/` - 掛載真正的 `/` 前的準備工作 - 非必要,但有 `initrd.img`,開機會快很多,因為不用重新編譯 kernel - `/dev` = device - 放置各種硬體設備的檔案 - 因為在 Linux 中,萬物皆檔案,所以對這些硬體檔案做操作就是對硬體做操作 - 例如:滑鼠、鍵盤、記憶體 - `/media` - 放置可移除的裝置**自動**掛載的目錄 - 可移除的裝置:光碟、DVD、USB :::info **掛載** - 維基上掛載的定義:由作業系統使一個儲存裝置(諸如硬碟、CD-ROM 或共享資源)上的電腦檔案和目錄可供使用者通過電腦的檔案系統存取的一個過程。 - 一個儲存裝置安裝後,要告訴他檔案存在哪裡 - 例如:在本機安裝一台光碟機,這台光碟機就會掛載在 `/dev` 裡面,光碟機的檔案會自動放在 `/media` 中 - 簡單來說,就是將==硬碟中的檔案連接到系統的某個位置== ::: - `/mnt` = mount - 放置臨時**手動**掛載的目錄 - 很久以前,這個目錄的用途與 `/media` 相同 :::info **`/dev`、`/media`、`/mnt` 的差別** | 目錄 | 目錄內放的內容 |例如在 Ubuntu 上接 USB| | -------- | -------- | -------- | | `/dev` | 放置各種硬體設備檔案 | 只要 USB 成功連接上了電腦,`/dev` 下就會顯示 USB 的設備檔 | | `/media` | 放置可移除的裝置**自動掛載**的目錄 | **系統**會自動將 USB 中的檔案掛載到 `/media` | | `/mnt` | 放置臨時**手動**掛載的目錄 | **系統管理員**可以手動將 USB 中的檔案手動掛載至 `/mnt` | - 如果系統管理員沒有特別將 USB 中的檔案手動掛載至 `/mnt`,則 `/mnt` 不會顯示 USB 中的檔案 ! ::: - `/etc` - 放置系統主要的設定檔 - 例如 - `/etc/passwd`:user 的帳號密碼檔 - 不用 `sudo` 就可以查看 - `/etc/shadow`:真正存 user 密碼的地方 - 需要使用 `sudo` 才能查看 - `shadow` 的密碼是加密過的 - [講義 `/etc/passwd`、`/etc/shadow`](https://hackmd.io/LPP9vQ-2TqOvbxpyMjKM3Q) :::info **既然有 `/etc/passwd` 了,為什麼還要有 `/etc/shadow`?** - 很久以前,`/etc/passwd` 裡面是有存使用者密碼 - 但那時的加密技術不成熟,而且有很多人把密碼設的很簡單 - 所以 `/etc/passwd` 的密碼移到 `/etc/shadow` 中並加密 - 現在 `/etc/passwd` 的作用主要在察看使用者的資訊 - 只有 root 才可以看 `/etc/shadow` ::: - `/opt` - 放置第三方軟體的目錄 - 怕這個檔案汙染系統的話,可以把雜七雜八的東西放在這裡 - `/usr/local` 也可以放第三方軟體 :::info **第三方軟體** - 自己額外安裝到 Linux 中的軟體 - 只要不是 `apt install` 的軟體,都裝到 `/opt` - 例如 : Chrome - 下載安裝檔 ```bash= wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb ``` - 安裝 ```bash= sudo dpkg -i google-chrome-stable_current_amd64.deb ``` ::: - `/run` - 各種服務會需要的暫存檔案 - 例如: `.pid` - 內容只有一行,紀錄目前的 process 的 id 是多少 (pid) - 防止同個 service 同時啟動多個副本 - 會導致這些副本互相搶資源 - 啟動服務前會先檢查有沒有 `.pid` - 有,就停止啟動服務 - 沒有,就啟動服務,然後把此服務的 pid 存進 `<service>.pid` - 其它同一個 service 的多餘 process 則自動退出 - 當關閉服務時,`<service>.pid` 也會跟著被刪除 (kill) - `/srv` = service - 放置一些網路服務啟動後,這些服務所需要取用的資料目錄 - 通常有對 Unix 跨平台的服務才會被掛載在這邊 - 例如 : FTP (檔案傳輸協定) - 用在網路上 client 和 server 之間進行檔案傳輸的應用層協定 - 預設的 FTP server folder 是在 `/srv/ftp` 欲分享的檔案可以放在這邊 - `/tmp` = temporary - 讓一般使用者或者是正在執行的程式暫時放檔案的地方 - 裡面的檔案重開機就會不見了 - `/tmp` 的權限是 777 - 777:所有使用者與群組的權限**可讀可寫可執行** - 因為 `/tmp` 是讓所有使用者暫存檔案的地方,所以要 可讀可寫 - 有些程式會將安裝檔暫存在此,所以要 可執行 - 未經授權的使用者可以寫入惡意程式 - 可能變成病毒的溫床 - 不過可以把特定檔案另外掛載,設定成 ==`noexec`== - 無法被執行 - 可以避免惡意程式被執行的可能 <!-- :::info **系統中有兩個地方任何人都可以任意寫入** * `/tmp` 跟 `/dev/shm` * `/dev/shm` * 會開啟 tmpfs * tmp filesystem,存在記憶體中 * 可以用 `mount` 查看其底下掛載的目錄 (grep tmpfs) * 讀寫速度很快 ::: --> - `/usr`、`/var` - 下面會詳細說明 - `/home` - 系統預設的使用者家目錄 - 新增的使用者帳號時,預設的使用者家目錄都會規範到這裡 - 家目錄的代號 - `~` : 代表目前這個使用者的家目錄 - `~<使用者>` : 則代表 `<使用者>` 的家目錄 - `/etc/skel` - 新增使用者時,初始化他的家目錄 - `/etc/skel` 下的所有檔案會自動被複制到 新增的使用者家目錄下 - 如果需要所有人都看到的檔案就可以在這寫 `README.txt` - 可用 `ls -a /etc/skel` 可以看你的系統預設有什麼 - `/lib<qual>` - 存放與 `/lib` 不同格式的二進位函式庫 - 例如:lib64,支援 64 位元的函式庫 - `/root` - 系統管理員 (root) 的家目錄 - 一般使用者的家目錄都在 `/home` - `root` 基本上是 Linux 系統中的神 :::danger **把電腦毀滅的指令** ```bash= rm -rf --no-preserve-root / ``` - r:recursive - f:force > **請不要隨便嘗試,後果自負** 🙂 ::: ## `/usr` 與他的子目錄 - Unix Software Resource 的縮寫 - Unix 作業系統軟體資源 所放置的目錄,不是放使用者的資料 - 系統剛安裝完畢,這個目錄會占用最多的硬碟容量 ### 需要存在的目錄 - `/usr/bin` - 放使用者非必備的二進位檔案 - 使用者安裝的程式的指令會在這裡 - `/usr/sbin` - 非系統平時運作所需要的指令 - 通常給超級使用者使用 - `/usr/lib` - 不是系統基本的,而是往上安裝的函式庫都放到 `/usr/lib` - 例如 - `python`:python 的函式庫 - `gcc`:C 的編譯器 :::info **`bin`、`sbin`、`lib` 已合併** - 2021 2 月 Ubuntu 已經將 - `/bin` 跟 `/usr/bin/` 合併成 `/usr/bin/` - `/sbin` 跟 `/usr/sbin/` 合併成 `/usr/sbin` - `/lib` 跟 `/usr/lib/` 合併成 `/usr/lib` - 他們現在真的變成一樣的子目錄了 ::: - `/usr/local` - 存放系統管理員在本機自行安裝自己下載的軟體 - 這個子目錄底下也有 `/bin`、`/lib`、`/etc`... 的次目錄 - 當軟體升級新版系統或新發行版時,可以安裝在這裡 - 無須重新安裝全部程式 - `/usr/share` - 主要放置唯讀架構的檔案 - 幾乎都是文字檔案 - 例如:`man`,線上說明文件,可以教你指令怎麼用 ## `/var` 與他的子目錄 - 主要針對常態性變動的檔案 - 系統運作後才會漸漸佔用硬碟容量的目錄 - 快取 (cache)、紀錄檔 (log file) 以及某些軟體運作所產生的檔案 - 適合存放需要頻繁改寫的檔案 - 例如:系統記錄檔、資料庫裡的檔案 ### 需要存在的目錄 - `/var/cache` - 應用程式本身運作過程中會產生的一些暫存檔 - 例如:`apt install` 下來的 package - `/var/lib` - 放置程式本身執行的過程中,需要使用到的資料檔案 - 例如:mySQL 的 data base 跟 table 通常都會存在 `/var/lib` - `/var/lock` - 某些裝置或者是檔案資源一次只能被一個應用程式所使用 - 同時被兩個程式使用該裝置時,可能產生一些錯誤的狀況 - 所以要將該裝置上鎖 (lock) - `/var/log` - 紀錄檔放置的目錄 - `boot.log` : 開機相關的紀錄檔 - `/wtmp` : 記錄登入者的資訊 - `/btmp` : 紀錄登入失敗的資訊 - `/var/spool` - 這個目錄通常放置一些佇列資料 - 佇列資料 : 排隊等待其他程式使用的資料 - 這些資料被使用後通常都會被刪除 - 例如:印東西 <!-- - `mail` : 有人寄信過來的時候,信會放在此 --> - `/var/mail` - 放置個人電子郵件信箱的目錄 ## 沒有在 FHS 但重要的目錄 - 以下三個子目錄也很重要,但在 FHS 文件中沒有將他們定義在根目錄 - `/lost+found` - 當檔案系統發生錯誤時,會將一些遺失的片段放置到這個目錄下。 - 這個子目錄平常是空的 - `/proc` = process - 一個虛擬檔案系統 (virtual filesystem) - 他放置的資料都是在==記憶體==當中 - 所以使用 `ls -l` 查看時,檔案大小是 0 - 例如:系統核心、process 資訊、周邊裝置的狀態、網路狀態等等 - `/sys` = system - 跟 `/proc` 非常類似,也是一個虛擬的檔案系統 - 同樣不占用硬碟空間,儲存在記憶體 - 記錄 kernel (核心) 與系統硬體相關的資訊 - 例如:已載入的核心模組、核心偵測到的硬體設備資訊

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully