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: `lsa` `ncnu` # Week 03 (2023/03/09) - Book mode: https://hackmd.io/@ncnu-opensource/book [TOC] --- ## FHS ### /bin and /usr/bin - `bin` : 放置系統必備的二進位檔案,for example,系統進入單人修復模式所需的 binary - `/bin This directory contains executable programs which are needed in single user mode and to bring the system up or repair it` - `sbin` - `/sbin Like /bin, this directory holds commands needed to boot the system, but which are usually not executed by normal user` ### 正規表達式 - 正規表達式 - 可以用指令找出關鍵字 - 可以用簡單的語法代表複雜的字串 Matching,可以使系統管理更為便捷 - 沒有用正規表達式 - 可能會需要一一翻閱資料夾找 - 延伸正規表達式 - 可以簡化指令 ### single use mode - single user mode 單使用者模式 - ![](https://i.imgur.com/m3VaRM6.png) > [Runlevel 維基](https://en.wikipedia.org/wiki/Runlevel) > `ls /etc/rc*`: > - k 開頭代表 kill > - `/etc/rc0.d` : 關機,切到 runlevel 0 的時候要做什麼 > ![](https://i.imgur.com/CFPNRdi.png) > `/etc/init.d` 開機時,需要啟動哪一些檔案以及這些檔案啟動的順序 > ubuntu 20 後,都改用 `/etc/systemd/system` - 只提供少數服務及功能 - 和 super user 不同 - 通常只有在系統發生問題時需要維護的時候才會進入這個模式 - 最基本的系統,沒有網路,沒有圖像化,甚至沒有掛載任何東西,所以只可以在機台前面進行除錯 > 從最基本開始一層一層往上檢查除錯 > 出現開機選單按下 E,可以看到一些資訊? ### /lib and /usr /lib :::warning ### `/lib` 與 `/usr/lib` 的差異 - `/usr/lib` 放置的是 `/usr/bin`和 `/usr/sbin`的函式庫,多為系統的普通使用的函式庫 - `/lib` 放置的是 `/bin`和 `/sbin`,多為系統的普遍使用的函式庫 ::: ### 函式庫 :::info ### 什麼是函式庫 - 裡面有多個寫好的 function,合併起來變成一個 libary - 只需要引用就可以直接使用 ::: ### `/sbin` vs `usr/sbin` 在尚未合併之前 - `/sbin` 作為系統管理必備的 binary - `/usr/sbin` : 放置非系統管理員必備的工具 :::info ### 什麼是二進位檔案 - binary file - 以二進位的形式存放,ex: 映像檔、執行檔 - 一般的文書軟體不支援二進位檔案,若執行會出現一大堆亂碼 ::: :::info ### 什麼是掛載 - 使用設備需要把這個設備掛載到目錄上 - 將硬碟中的檔案連接到系統中的某個位置 - ex, 掛載光碟機到 `/dev/cdrom` ::: ### /usr/local :::warning ### `/usr/local/bin`、`/usr/local/bin`及`/usr/local/lib` - `/usr/local`:放置的是使用者自己安裝的應用程式相關檔案,非作業系統本身的檔案 - 下載的應用軟體,編輯後的`./configure`系統通常會預設`prefix`至`/usr/local` - `/usr/local/bin` 儲存以上軟體的一般使用者執行檔案 - `/usr/local/lib` 放置 `/usr/local/lib` 的函式庫 ::: ### 環境變數 - 可以用 `export` 觀察目前的環境變數 - `/var/lib` - for example 使用 mysql,常變動的Library 會放在 `/var/lib`,不常變動的library 會放在`/usr/lib` ### 系統的不同 - bsd 與 linux 的指令有部分不同 - `top` 系統裡面的工作管理員 - 查看 CPU 狀況 ![](https://i.imgur.com/5LN6vQ0.png) - `ps pux` 顯示 process - `-a` 不與 terminal 有關的所有process - `-u` - `-x` ### run level - single user mode - 沒有網路,必須要在機器前才可以實作 - 確認 filesystem 沒有問題 - recovery mode 和 single user mode 通常是一樣 - grub 按 e 可以看到開機的參數 - /etc 下的 rc 開頭檔案會寫哪個 run level 要做什麼 (ex: rc0.d) > `ls /etc/rc*` 可以查看,K開頭的軟連結是kill,S開頭的軟連結是start > 原先使用 `/etc/rc/init` 根據 script 設定的順序執行 - 現在使用 `systemd` 透過 dependicy 來進行管理,相關 dependicy 可以透過 `man bootup` 查看 ``` # man runlevel ┌─────────┬───────────────────┐ │Runlevel │ Target │ ├─────────┼───────────────────┤ │0 │ poweroff.target │ ├─────────┼───────────────────┤ │1 │ rescue.target │ ├─────────┼───────────────────┤ │2, 3, 4 │ multi-user.target │ ├─────────┼───────────────────┤ │5 │ graphical.target │ ├─────────┼───────────────────┤ │6 │ reboot.target │ └─────────┴───────────────────┘ ``` > ![](https://i.imgur.com/yXOhqFK.png) ## 基本指令 - bash redirection - `>` 輸出到檔案 - `>>` 輸出到檔案,將輸出資料append到檔案 - `<` 輸入 - ex - `ls > ls_file # redirect stdout to ls_file` - `python3 print.py 2>&1 > testfile` - `2>&1`: redirect stderr to stdout) - `>`: redirect stdout to file - `whereis` : 會在在這個環境變數底下定義的 搜尋順序`PATH` 列出"所有" binary 的位置 - `which`: 告訴使用者執行檔案放哪 - `echo $PATH` or `env` : 列出底下所有的環境變數 - `:` 加在 path 前面代表指定哪一個目錄底下 - `export PATH=$PATH:[custom path]` > `export` 代表傳遞環境變數到其他 fork 出去的 process,像是 bash 下的 `ls`。 > ![](https://i.imgur.com/dKUKU2r.png) ### 使用者相關 - 使用者相關 - su : - 會需要輸入 root 帳號的 password - 因此系統不會記錄誰進行 root 權限的操作,因為是以 root 身分直接執行,而不是 sudo 以 sudoer 內定義的權限去執行 > 極力不推薦使用 - `/etc/passwd` - 系統只會看使用者的 UID,不會辨別使用者名稱 - `id` - 可以輸出當前使用者的 UID ![](https://i.imgur.com/XPTHq8L.png) - `passwd` 修改密碼 - `finger <username>` username 的使用者資訊,ex: 信箱 ### About group - group - `sudo groupadd <group name>` 新增群組 - 新增成員 - 在 group id 後面增加 username - 中間用 `,` 隔開,且不可以有空格 - ![](https://i.imgur.com/grIv2hM.png) - `sudo gpasswd -A <username> <group>` 新增群組管理員權限 - `sudo gpasswd -M <username1>,<username2>... <group>` - `sudo gpasswd -d <username> <group>` 從群組踢掉 username - `sudo chgrp <group> <file location> - `sudo chown <ownername> <file location>` 改變owner ### About network - 關於network的指令 - `ifconfig`: 查看網卡的資訊 - `route` - `ip` - [redhat ip cheetsheet](https://access.redhat.com/sites/default/files/attachments/rh_ip_command_cheatsheet_1214_jcs_print.pdf) - `-s` : 新增統計資料(TX/RX) - `sudo ip -s link <網卡> show` 顯示網卡資訊 - `netstat` - `ss`: 可查看網路連線( Socket )狀態 - `-t` : 列出 TCP - `-a` 列出所有的 socket(unix-socket, udp, tcp) - [heredoc](https://en.wikipedia.org/wiki/Here_document) - `cat > test << eof` - `eof` 結束 terminal 指令 - 快捷鍵 `ctrl+d` 結束 terminal 指令 ## Network - IP 4 layer arch ![](https://i.imgur.com/vMkKxqj.png) - Link Layer 負責 data frame ![](https://i.imgur.com/rU0Bqrt.png) ### Link Layer - frame 為 link layer 的傳送單位 ![](https://i.imgur.com/wL4kRQH.png) - 前導碼:數據開始傳送前,發送發會發送一個固定長度的前導碼,當接收方收到連續的 0、1,就會知道對方要開始傳送了。 - 以 0、1 表示 - SFD : 區隔前導碼與訊息。 - Destination / Source Address - Lenth or Type - Frame Check Squence(CRC): 校正碼,檢查訊息是否有錯誤 ### Mac Address - 一張網卡只有一個 mac(really? macvlan?) - 同一個區域傳送,只需要知道對方的 MAC address - 不同網域傳送需要知道對方的 IP Address ![](https://i.imgur.com/VolUJSn.png) > 截圖自講義 - 查看網卡 ![](https://i.imgur.com/1EX3YB2.png) - size: 6 Bytes - 偶數是 Unicast,奇數是 Muticast ### CSMA/CD 1. CSMA/CD(Carrier-sense multiple access, collision-detection) - 多人使用同一 link 時才會使用 - 偵測碰撞 2. CSMA/CA(Carrier-sense multiple access, collision-avoidence) - 多人使用同一 link 時才會使用 - 避免碰撞(處在無法偵測碰撞的環境,for example wireless) - 避免碰撞 - RTS (request to send) : - 發送端在傳送資料前先送出 RTS 要求封包 - 接收端 & 發送端傳輸範圍內的其他使用者都會收到 - CTS (clear to send) : - 接收者傳送 CTS 封包,代表接收者已經準備好接收了 - 同時告知其他人,這個時間不可以傳送封包給接收者 ### Connecting device #### hub - 使用 boardcast 的方式發送訊息 - 不安全,容易受到碰撞 - 無法區隔 collision domain #### switch - 全雙工 - 有 arp table 學習、儲存每台電腦的 MAC address 1. 在 switch 內部沒有 Y 的 mac address 2. 只好 broadcast 到全部的 port 3. mac address Y 收到該封包,並回復到 switch 4. switch 記憶這個關係(記錄下 MAC address Y 的所在網域) - 每一個 port 都是獨立 - 可以區隔 collision domain - 如果 switch 查表後發現封包的來源位址跟目的地位址是同網段, switch 就不會理會封包。 - 用 ARP 技術處理 :::info #### 單工 - 資料只會單向傳送 - ex: 從 A server 的 a port 送出 Client B 接收,但是 Client B 不會回覆 #### 半雙工 - 不能同時傳遞資料,但雙方都可以傳輸 - ex: 無線電通訊 #### 全雙工 - 同時間兩邊可以傳遞資料 - ex: 講電話 ::: ### LAN - ethernet 為最常使用的 link layer protocol ### WAN - 廣域網路 - 可以連接多個國家/城市 - 常見於企業分公司,用專案的方式連接 - 傳送介質主要利用公共網路 - 缺點 - 頻寬容易受到限制 - 費用貴森森 ### Internet Layer - 跨網域的傳輸 - 最具代表性是 IP 協定 (Internet Protocol,網際網路控制訊息協定) ### IP Address - 同一個時間,ip 不會重複 ### IPv4 - 32 bits, 8 bits 為一組,結構包含 Network number & Host number - 目前廣泛使用的版本 - 全0 : 表示這個網段的位址 - 0.0.0.0 - 沒有網路的時候 - 同時 listen 多個網卡的 ip address - 全1:broadcast - 255.255.255.255 - 127.0.0.0 - loop-back - 127.0.0.1 - loop-back ### IPv6 - 128 Bits,並由 Hexadecimal 表示 ### Subnet Mask (子網路遮罩) - Net_ID 全部位元是1,Host_ID 全部位元是0 ![](https://i.imgur.com/bCbVf0a.png) ![](https://i.imgur.com/aldcfJz.png) ![](https://i.imgur.com/PlLKBxU.png) - `ip a` : ip 顯示方式會以 `x.x.x.x/x` 顯示 ![](https://i.imgur.com/2hh6PCc.png) :::success ### 題目: - 有一 IP Address : 87.200.79.179/27,請問他的 Net ID 、Broadcast IP 以此網段可分配之主機數? ```shell! 01010111.11001000.01001111.10111101 11111111.11111111.11111111.11100000 (AND) => netmask ------------------------------------- 01010111.11001000.01001111.10100000 ``` - net ID : 01010111.11001000.01001111.10100000 = 87.200.79.160 - brocast IP : 01010111.11001000.01001111.10111111 = 87.200.79.191 - 主機數 = 2^5 -3 = 29 (gateway、廣播、區段網路) ::: ### Router - `sudo route add -host <source> gw <destination>` - `route -n` 查看網路 ![](https://i.imgur.com/jHTlYhb.png) - `sudo route -net <ipaddress/netmask> <網卡名>` 添加子網路 - `sudo route -net <ipaddress> netmask 255.255.255.0 <網卡名>` ### ICMP - 障礙通知:傳遞封包失敗的話,會回傳失敗原因給發送端 - 狀況查詢:發送 ICMP 查詢目前網路狀況 - `ping <Address>` - `ping 127.0.0.1`: ping 本機,可以看自己的 TTL (time to live) - `traceroute` : - 跟著封包去旅行,查看封包前往目的地的過程中經過了哪些路由 - 不是內建需要先安裝 `sudo apt install traceroute` - 指令 : `tracerout [Address]` - 透過傳送從 1 遞增的 TTL 封包給 DST IP,透過 ICMP report 或得相關的 router 的 IP ![](https://i.imgur.com/aDaNIgI.png) ### ARP / RARP - 位址解析協定 / 反向位址解析協定 - ARP : - 以 IP 位置查詢相對應的實體位址(MAC Address) - `arp -n` : 查看 ARP table - `c` : 開機 flags mask 自動生成 ![](https://i.imgur.com/GhIXTdi.png) - `-s [IP] [MAC]` : 新增至 table 中 - `-d [IP] [MAC]` : 從 table 中刪除 - RARP : - 用已知的 Mac address 查詢 IP 位置 - ARP & RARP 封包方式都一樣

    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