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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ###### tags: `1111` `lsa` `ncnu` # Week 04 (2022/10/06) [TOC] - Book mode: https://hackmd.io/@ncnu-opensource/book --- ## 作業規定 - 作業都要用開源格式,沒照規定視同沒有繳交 - 記得內容寫的越詳細越好 ## 複習 * sudo == superuserdo * 指令 -> /bin /sbin * 第三方軟體 /opt ## FHS 檔案系統階層標準 ![](https://i.imgur.com/ODQMUf8.png) - `/`: 所有檔案都在根目錄下,放基本指令 - `/boot`: 存放開機要用的檔案 - `/lib` : 放置在開機時會用到的函式庫 - `/dev` : 放各種硬體設備相關的檔案 - `/media`: 可移除裝置自動掛載的檔案之目錄 - `/mnt`: 放置臨時手動掛載的目錄 - `/etc`: 放置系統主要的設定檔 - `/opt`: 存放第三方軟體之目錄 --- - `/run`: 各種服務會需要的暫存檔案 ![](https://i.imgur.com/cElc98W.png) > acpid.pid: 記錄此服務的 proccess 的 id 檔 ![](https://i.imgur.com/A5td7AQ.png) > 如果這個檔案沒有數字代表此服務未啟動 - `/srv` = service - 通常是放置開發的服務 - 通常有對 Unix ? - srv/ftp - `/tmp` = temporary - 讓一般使用者或是正在執行的程式暫時存放檔案的地方 - 777(tmp的權限): 所有使用者與群組的權限**可讀可寫可執行** - 因為 `/tmp` 是讓所有者暫存檔案的地方,所以要**可讀可寫** - 有些程式會將安裝檔暫存在此,所以要**可執行** - 因為只是暫存所以當重新開機後裡面的檔案會不見 - 未經授權的使用者可以寫入惡意程式 - 可能變成病毒的溫床 - 不過可以把特定檔案另外掛載,設定成 ==noexec== - 無法被執行 - 可以避免惡意程式被執行的可能 - ![](https://i.imgur.com/zCbzgh4.png) > 可以看到有哪些服務被掛載在哪邊 - `/home` - 系統預設使用者的家目錄 - 新增的使用者帳號時,預設的使用者家目錄都會規範到這裡 - 家目錄代號 - `~`: 代表目前此使用者之家目錄 - `~<使用者>` :則代表使用者的家目錄 - `/etc/skel` - 新增使用者時,初始化他的家目錄 - `/etc/skel` 下的所有檔案會自動被複製到新增的使用者家目錄 - 如果需要所有人都看到的檔案就可以在這寫 `README.txt` > 使用 `vim README.txt` 來新增檔案 > 所有使用者都會得到這個新增的檔案 - ![](https://i.imgur.com/DeDRsH6.png) > 新增一個使用者 aaa: `sudo adduser aaa` - `ls -al` - 可以看到系統預設隱藏的檔案 - ![](https://i.imgur.com/hAlhXWC.png) > 打 `ls` 沒有看到詳細檔案,打 `ls -al` 才看到隱藏檔案 - `/lib<qual>` - 存放與`/lib`不同格式的二進位函式庫 - 例如:lib64, 支援64位元的函式庫 - `/root` - 系統管理員(root)的家目錄 - 一般使用者的家目錄都在`/home` - `root` 基本上是 linux 系統中的神 :::danger ### 把電腦毀滅的指令 ``` linux= rm -rf --no-preserve-root / ``` - `r`: recursive - `f`: force 強制執行該行指令 - `rm`: remove 欲刪除檔案內的東西 - `rm -rf /`: 下在 Ubuntu 20.04 或 18.04 會有保護機制 **不可以隨便嘗試 !!! 💀 雖然新版的 Ubuntu 有防護機制~** ::: ## `/usr` 與他的子目錄 - Unix Software Resource 的縮寫 - Unix 作業系統軟體資源所放置的目錄,不是使用者的資料 - 系統剛安裝完畢,這個目錄會占用最多硬碟容量 - ![](https://i.imgur.com/F8orN0T.png) ### 需要存在的目錄 - `/usr/bin` - 放使用者非必備的二進位檔案 - 使用者安裝的程式的指令也會存放在此 - `usr/sbin` - 非系統平時運作所需要的指令 - 通常給超級使用者使用 - `/usr/lib` - 不是系統基本的,而是往上安裝的函式庫都放到 `/usr/lib` - 例如: - `python`: python 的函示庫 - `gcc`: C 的編譯器 :::info `bin`、`sbin`、`lib` 已合併 - `/bin` 與 `/usr/bin` 合併成 `/usr/bin` - `/sbin` 與`/usr/sbin` 合併成 `/usr/sbin` - `/lib` 跟 `/usr/lib/` 合併成 `/usr/lib` 變成一樣的子目錄 - ![](https://i.imgur.com/esbvgcf.png) ::: - `/usr/local` - 存放系統管理員額外安裝的軟體 - 這個子目錄下也有 `/bin`、`/etc`...的次目錄 - 當軟體升級時新版系統或新發行版時,可安裝於此 - 無須重新安裝全部程式 - 原始軟體裡面附的程式,避免裡面的軟體資料內容影響、覆蓋到系統源有的檔案,所以可以裝在這邊 - 裝第三方軟體的地方 - `/usr/share` - 主要放置**唯讀**架構的檔案(read only) - 幾乎都是文字檔案 - 例如: `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`:紀錄登入失敗的資訊 - ![](https://i.imgur.com/UI1mI0E.png) - `/var/spool` - 這個目錄通常放置一些佇列資料 - 佇列資料: **排隊等待其他程式使用的資料** - 這些資料被使用後通常都會被刪除 - 例如:印東西 - `/var/mail` - 放置個人電子郵件信箱的目錄 - 有人寄信過來就會放置此目錄下 ## 沒有在 FHS 但重要的目錄 - 以下三個子目錄也很重要,但在 FHS 文件中沒有將他們定義在根目錄 - `/lost+found` - 當發生不正常開關機,檔案系統發生錯誤時,會將一些遺失的片段,那些片段會被放置到這個目錄下 - 這個子目錄平常是空的 > `sudo ls -al lost+found`: 應該要是空的 - `/proc` = process - 一個虛擬檔案系統 (virtual filesystem) - 他放置的資料都是在==記憶體==當中 - 所以使用 `ls -l` 查看時,檔案大小是 0 - 系統開啟後才會看到這些檔案內容 - 例如:系統核心、process資訊、週邊裝置的狀態、網路狀態等 - `/sys` = system - 跟`/proc` 非常類似,也是一個虛擬的檔案系統 - 同樣不占用硬體空間,儲存在記憶體 - 紀錄 kernel (核心) 與系統硬相關的資源 - 例如:已載入的核心模組、核心偵測到硬體設備資訊 ## BT 補充 - 如果是安裝第三方軟體,不要讓他直接安裝進系統裡,要安裝到 `/opt` 或 `/usr/local` 裡面 - web server : - 上網時會連接一台伺服器,瀏覽器會像伺服器傳送特殊的資訊,讓伺服器知道瀏覽器要幹嘛 - 伺服器裝好的時候需要裝 http (網頁伺服器)監聽 - 長期提供服務,可以用 pid 檔去判斷該服務有無開啟 > 例如:apache.pid - 其實是 link 至 /run 底下的 lock 檔案 ![](https://i.imgur.com/09ZanM7.png) - 系統基本指令應該要放置在 `bin` ,額外的指令才應該要放置在 `user/bin` - `whereis`+ <欲尋找位置的指令/檔案> ![](https://i.imgur.com/tvWLgHP.png) - `dpkg -S`: 告訴你現在打的那個指令是哪個軟體(安裝檔)所提供的 - `ls` 為系統基本指令,因此檔案在 `bin` - ![](https://i.imgur.com/aE8KiOM.png) > 如果打 `/usr/bin` 找不到,可以改 `/bin` 試試看 > `/bin` 指向`/usr/bin` > ![](https://i.imgur.com/O6ub3OJ.png) - `var/log` 裡面是存放記錄檔,其中重要的有以下: - `auth.log`: 記錄了各式各樣的登入訊息 - ![](https://i.imgur.com/PbE8o9K.png =450x) > 如果你一直打錯密碼也會被記在這邊 - `boot.log`: 開機流程的一些東西 - `dmesg`: debug message 可以讓你看到系統開機流程,kernel 在哪個時間點做了什麼 - `lastlog`: 此檔案有相對應指令`lastlog`,會秀出系統目前所有使用者目前登入狀態 - `syslog`: 裡面包含了服務 (Service) 什麼時候啟動、關閉、重開、狀態 - `/srv`: 彈性地存放 service,不一定所有 services 都要放進來 - 打開是空的 Don't panic,有些系統裝後可能放在其他地方 - `/tmp`: tmp 跟 temp 都是 temporary的縮寫,但linux的兩者有不同意思 - `temp`:溫度 - `tmp`:暫存檔 - `tmp` 為暫存的地方,預設可讀可寫可執行,故最常被放置病毒 - `/dev/shm`: 此目錄頁也是可讀可寫可執行,一個 memory disk。如果需要大量I/O計算,可以把檔案放在這裡,速度會比檔案放在SSD快。 - 進去 `/dev`,下指令搜尋 shm (`mount | grep shm`): 可以看 shm 的預設容量大小,通常上限為記憶體的一半 - ![](https://i.imgur.com/Xk526vW.png) - 掛載: 把這個儲存空間鏈結到一個目錄,而此目錄就擁有此磁區 - 進去 `/dev`,`mount | grep "/tmp"` : 列出這個目錄所有掛載起來的磁區 - ![](https://i.imgur.com/plgqB0z.png) > 只有兩個磁碟分割區: > 1. `/` > 2. `/boot/efi` - `ro`: 可讀 / `rw`: 可讀可寫 - `nodev`: 不可以有硬體檔案 - `noexec`: 不可執行 - ![](https://i.imgur.com/JOGYlQa.png) - `/home` 目錄 - 使用 `adduser` 這個指令來新增使用者的話,會自動幫新的使用者新增家目錄 > 其實使用者可以隨意指定新建使用者的家目錄在哪個資料夾,但以 FHS 來說用 adduser 指令會比較好 - 只有 root 的家目錄不在 /home 底下 > 以前的作法是 `/home` 放在單獨的硬碟裡 `less /etc/passwd`: 可以看到較少筆的使用者 ![](https://i.imgur.com/rlSHt5v.png) - `/etc/skel`: 當新增新使用者時,這個目錄下的所有檔案會自動被複制到新使用者的家目錄下 - 故如果裡面內容被惡意修改過,接下來新增的使用者就會直接複製範本、直接被感染,因此要注意此資料夾不能任意被寫入 - `.bashrc`: 一開機就開始的執行檔,如果被惡意竄改,也會造成電腦出問題 > ![](https://i.imgur.com/vSj8sSG.png) - `rm -rf /`: 把世界全部摧毀(因為root幾乎等於全部)!但現在會有保護機制 - `rm -rf /*`: 移除根目錄底下的所有子目錄(這個沒有防呆,而且威力跟上面的直接移除差不多) > 所以要再次提醒,sudo 不要亂加 - `/usr`這個目錄原本是代表使用者,是後來才改成Unix - 存放不是系統必要運作的執行檔,而是使用者需要的額外軟體 > firefox、chrome 之類的 - **任何可執行的指令都是一個可執行檔案,放對位置就會變指令** - 新增一個原本不存在的可執行檔: - `nano 檔案(指令)名稱` : 用簡單的方式寫出一個可執行檔 - `chmod +x` + `檔案(指令)名稱`: 告訴這個檔案你是可執行的!(但還沒變成系統指令喔)但是可以執行了 > ![](https://i.imgur.com/G8udAED.png =200x) -> ![](https://i.imgur.com/OceXUdI.png =200x) > 若直接輸入`./lala` 會沒辦法執行 - 把可執行檔變成"命令": - 把檔案移去`/usr/local/bin/` - `sudo mv lala /usr/local/bin/`: 就變成系統指令了(不管在哪個目錄輸入 `lala` 都可以使用了) - `mv`: 用於將檔案從 A 位置移至 B 位置、更變檔案名稱 - `sudo mv /usr/local/bin/lala ./yobin/` > 把 位於/usr/local/bin/ 的 lala 移去 ./yobin/ - 萬物皆檔案,只是在於怎麼去使用與執行 - **環境變數**: - 輸入`export` 顯示該目錄所有環境變數 > `export |grep PWD`:查看pwd位置,可以看見上一次跟現在所在的位置![](https://i.imgur.com/3mXExLA.png) - 只要汙染環境變數,就可以讓很多人做你想要他做的事情? - 如果自己想創造指令,又不想影響其他使用者,可以在自己的家目錄裡新增檔案,在把檔案放進 PATH 裡 > 指令沒有存在系統目錄裡面,但還是可以直接呼叫使用,因為這樣做系統目錄會去你設定的路徑下尋找名稱相同的檔案 - `mkdir 資料夾名稱`: 創造新的資料夾 - `lost+found`: 並不是所有檔案系統都有這個東西,有些支援有些不支援 - 如果不小心非正常關機,開機過程中會發現並檢查檔案系統 - 如果檢查並救回來的時後發現上端結構毀損,就會被放置在裡面 > ![](https://i.imgur.com/WChM4yi.png) - `/mail`: 需要有大量動態的讀跟寫的檔案,所以放在 `/var` 裡面 > 蠻重要的可以先去了解(期中可能會用到) - `/var/log`: 存放動態檔案 - `/var/mail`: 放使用者的郵件目錄 - `/var/store`: 收到的信存放地點 - `.` : 一個點的目錄,代表現在當前的目錄 - e.g.: `./lala` 指到現在這個目錄底下名為lala的檔案 - `..` : 兩個點的目錄,指向**你所指定的那個地方**的上一層目錄 (沒有指定的話就是當前目錄的上一層目錄) - e.g.: `../var` 指的是根目錄 - e.g.: `./..` 當前目錄的上一層 - 是一個相對路徑 > 如果當前位置已經在根目錄但你又再呼叫**根目錄的上一層(不管往上多少層)**,顯示出來的**依舊是根目錄** > ![](https://i.imgur.com/4X8gMyo.png) - **FHS 實例**: - `sudo apt install sl`:可以安裝sl(但apt如何知道他的位址?) - `apt search cowsay`: 上網搜尋 cowsay - 顯示符合的結果 - ![](https://i.imgur.com/n5MKt56.png) - 設定檔應該會存放在 `/etc` 裡面 - 在`/etc/apt/`,裡面有一個`source.list`,紀錄軟體要去哪裡抓、是哪些種類/分類的列表 - ![](https://i.imgur.com/ppWgf4u.png) - `sources.list` 為主要系統設定來源 - `lib`:存放在`/var/lib/apt/lists`,為一般軟體列表檔 - `/usr/lib` 放的是不那麼常變動的東西 > /var/lib 放的是常變動的檔案 - `/var/lib/apt/list`:所有的更新檔案資料列表 - 先用`sudo apt install cowsay`下載,之後用`sudo apt remove cowsay`再`sudo apt install cowsay`,會發現他其實有用cache記錄下來,所以沒有下載那麼多東西 ## 回家作業 linux伺服器磁碟規劃? 先搜看看,下禮拜會介紹細節

    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