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
    • 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
    • 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 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
  • 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: 1101, lsa --- - Book mode https://hackmd.io/@ncnu-opensource/book # Week 03(2021/10/07) [TOC] ## FHS - **etc** - 放置系統的主要設定檔(所有設定的東西) - e.g - user的帳號密碼檔 `/etc/passwd` - 裡面有很多行,每行代表每個帳號 - vim的設定檔 `.vimrc` > 查看帳號密碼檔 `cat /etc/passwd` - `/etc/passwd` >example s1073040: x :1000:1000:s1073240...:/home/s1073213040:/bin/bash - 欄位架構(用`:`隔開的都是不同欄位) - 使用者名稱 - 加密後的密碼 - 使用者 id(uid) (Linux中用以辨別使用者的方法) - 使用者所在群組 id(gid) - 使用者的完整名稱 - 登入後第一個所在的目錄 - shell名稱(檔案位置)(e.g. `/bin/bash`) - `/etc/shadow` - 欄位資訊(用`:`隔開的都是不同欄位) - 使用者名稱 - 加密後密碼 (`$number$`為加密方式) - 上一次密碼變更日期 - 從1970年1月1日開始計算天數 - Unix Time - 密碼最短使用期限 - 兩次變更密碼最少必須間隔多久,單位為天 - 如果此欄位為0代表此功能關閉 - 密碼最常使用期限 - 密碼最多可以使用多久,單位為天 - 如果此欄位是空的,則代表關閉此功能 :::info **冷知識** - 如果 密碼最短使用期限 大於 密碼最長使用期限 - 使用者就不能改密碼 - 密碼 `123` 最多可以用10天,但我變更密碼後須等100天才能再改密碼 ::: - 密碼過期限警告 - 密碼過期前多久,對使用者提出警告訊息,單位為天 - 如果此欄位是 空的 代表關閉此功能 - 密碼暫停使用期間 - 當密碼過期後,使用者還有多久的時間可以登入更改密碼,單位為天 - 密碼過期時間 ### 密碼欄位 - `x`:對應到存在`/etc/shadow`的密碼 - `!` 此帳號被鎖定,不能登入 - 用於使用者 - `*` 此帳號被鎖定,不能登入 - 用於服務 - (null):沒有密碼 - 如果此時 `passwd` 會直接進入設定密碼的環節 - 有些服務讀取到 `/etc/shadow` 的檔案可能會因為此欄位是空的,而不提供服務 ### 開玩 - `/etc/shadow` - 密碼欄位 - 刪掉一個字 - 變成`*`、`!` - 變成`(null)` - 可以直接輸入`password`更改密碼 - `/etc/passwd` - 密碼欄位 - 變成`*`、`!` - 變成`(null)` - 使用者id - 新增使用者!! - `sudo adduser 名字` - 刪除使用者 - `sudo userdel 名字` - 查看密碼 - `cat /etc/passwd` - 修改 `shadow` - `sudo vim /etc/shadow` - 切換使用者 - `sudo su - 使用者名字` - `ssh -v 使用者名字@localhost` - 退出使用者 - `logout` - 改密碼 - `passwd (使用者名字)` - 要注意的是只能改當前使用者自己的密碼(除非加sudo) - 如果有密碼會需要先輸入舊密碼 - 沒密碼(null)就可以直接設定新密碼 - 需要進去那個使用者 - 安裝ssh - `sudo apt install ssh` - 指令修改檔案 - `sudo vim /etc/shadow` - `sudo vim /etc/passwd` - `i` 更改 - 其他指令(先按esc) - `:w` 儲存 - `:q` 退出 - `:wq` 儲存並退出 - 用圖形化介面修改檔案(建議) - 方便修改不需要下指令 - `sudo gedit /etc/shadow` - `sudo gedit /etc/passwd` > 先新增使用者再測試 - 找使用者id - `id 使用者名稱` - 新增檔案 - `touch <檔名>` - 偵測是否有使用權限 - `touch test` :::info **冷知識** - 密碼欄位只有是`x`才會到`/etc/shadow`中比對密碼 - 如果密碼欄位是空的,那會新增密碼到passwd中的密碼欄位 - 新增user時,如果沒有指定便會順序地增加 - 在linux裡所有名字都是假的,只有`ID` (user ID 或 group ID)是真的 - linux 中所有檔案都可以設定權限,檔案權限的設定也是用 ID 來設定 - 所有使用者的家目錄都放在`/home`裡面,每個使用者可以變更自己的家目錄(`/home/使用者名稱`) - 真正有權限的是id (**認id**) 名字是給人看的罷了 - 改完id 會變成不同的使用者,使用者就沒有權限更改自己的家目錄 - home directory 如果切換到不存在的路徑,他會回到原本的位置 - 舊時加密不成熟容易被破解,所以passwd的密碼移到shadow中,且只有root可以查看shadow ::: - shell 如果改成 `/bin/false`,就不會讓這個使用者登入,也就是不讓使用者進入bash的互動介面 - `/bin/nologin`,就會不讓這個使用者登入,跟上一個差在會先告訴你不給你登 - shell 是什麼? - 用來跟使用者互動的一種東東 - 連接核心跟使用者 - 目前最多人使用的是**bash** (最基本的互動介面) - 什麼叫作業系統?核心? - 核心負責跟硬體互動,控制硬體設備的程式碼 - linux 只是一個核心(kernel),不是作業系統 ![](https://i.imgur.com/2vgRb8J.png) - 如果手殘把ls打成sl,會出現小火車 - 請先安裝 :::danger **重要** - 請輸入`sudo apt install sl` - [多螢幕效果更佳 (mp4)](https://imgur.com/a/EAFshnR) ::: :::info **`/etc/shadow` 已經可以儲存使用者密碼,為甚麼還需要`/etc/passwd`?** - 以前`/etc/passwd`有存使用者密碼的 - 但那時加密技術不成熟,而且有很多**把密碼設得很簡單** - 把加密後的密碼,丟到伺服器進行暴力破解 - 現在`/etc/passwd`主要是查看使用者的資訊 - 因為`shadow`只有root可以看 ::: - vim 的設定檔 vimrc - **opt** - 怕汙染原本的環境,可以先把雜七雜八的東西放在這,但不是垃圾桶 - 如果第三方覺得FHS架構很麻煩將所有東西都包在同一包了,就可以放在opt裡,不需要擺在其他目錄裡 (e.g. /etc ) - 放第三方**軟體**的目錄(`/usr/local` 也可以) - e.g. Chrome - 下載 - 安裝`sudo dpkg -l google-chrome-stable_current_amd64.deb` :::info **第三方軟體** - 自己額外裝到 linux中的軟體 - 只要不是`apt install`的軟體,都安裝到`opt` - e.g. Chrome - 下載安裝檔 - wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb - 安裝 -`sudo dpkg -l google-chrome-stable_current_amd64.deb` ::: - **run** - 各種服務會需要的暫存檔案 - e.g `.pid` - 內容只有一行,紀錄目前的process的id是多少(pid) - 防止建立太多一樣的副本,而這些副本互相搶資源 - 其他同一個 service 的多於 process 會自動退出 - 啟動服務前會先檢查有沒有`pid` - 當關閉服務時,`<service>.pid`也會被刪除(kill) - **srv** = service - 放置一些網路服務啟動後,這些服務所需要取用的資料目錄 - 通常有對Unix跨平台的服務才會被掛載在這邊 - e.g - FTP:檔案傳輸協定 - **tmp** (=temporary 暫時的) - **所有人**(甚麼權限都沒有的人也可以)都可讀、寫、使用東西(重要) - 讓一般使用者或是正在執行的程序,暫時放檔案的地方 - 裡面的檔案重開機就會不見了 - `/tmp`的權限是777 - **777**: 所有使用者與群組的權限==可讀可寫可執行== - 因為`tmp`是讓使用者暫存檔案的地方 - 有些程式會將安裝檔暫存在此 - linux 中可以分為使用者、群組和其他,它們分別可讀(4)、可寫(2)、可執行(1);所以7(數字相加)則是可讀可寫可執行,而777則是使用者、群組和其他都讀可寫可執行 - 未經授權的使用者可以惡意寫入程式 - 不過可以把特定檔案另外掛載,設定成`noexec` - 無法被執行 - 可以避免惡意程式被執行的可能 :::success ### 冷知識: 系統中有兩個地方任何人都可以任意寫入 - `/tmp`跟`/dev/shm` - `/dev/shm` - 會開啟tmpfs - tmp filesystem 存在記憶體中 - 可以用 `mount`查看掛載目錄下含有`tmpfs`的檔案(grep tmpfs) > `mount | grep tmpfs` - 讀寫速度很快 ::: - **home** - 系統預設使用者目錄 - ~:代表目前這個使用者的家目錄 - `~[name]`:則代表[name]的家目錄 - e.g`/etc/skel` - 新增使用者時,初始化其家目錄 > 當新增新使用者的時候,這個目錄下的所有檔案會自動被複制到新增的使用者家目錄下 - 例如說要所有人都看到的檔案就可以在這裡寫e.g. README.txt - 用 `ls -a /etc/skel` 可以看看你的系統都有預設些啥 - **`lib<qual>`** - 存放與`/lib`不同格式的二進位 - 系統所需的基本函式庫 - eg.lib64 - 支援64位元的函式庫 > 要念 lib 還是 lib 隨便你啦,但我都念 lib [name= BlueT] - **root** - 系統管理員的家目錄,在根目錄`/`底下 - 我是神,我不放在`/home`(放所有使用者的目錄)裡 - 毀滅所有東西只要輸入 `rm -rf /` remove force - `/` 根目錄 - 記得加上`sudo` - 為啥只有 root 放在根目錄?因為很久以前硬碟空間很小 (像是 10G),其他不重要使用者的資料可以放在 NAS 就好,但 root 超重要不能不見 - `/lost+found` - **tmp** - 如果你把 777 權限關掉,那也不行,因為很多程式會需要用 - 但可以把執行權限 (x bit) 關掉,增加安全性 - 就有駭客能把惡意程式解壓縮在這地方,見 [DEFCON-27-Andy-Grant-Unpacking-pkgs.pdf](https://media.defcon.org/DEF%20CON%2027/DEF%20CON%2027%20presentations/DEFCON-27-Andy-Grant-Unpacking-pkgs.pdf) 介紹這攻擊手法的簡報 - temporary,暫時的 - **dev** - 硬碟 - 設備文件通常存放在這(USB驅動、印表機驅動) - **usr** (Unix Software Resource) - 不是系統最需要的 - `/usr/bin` - `/usr/lib` 額外安裝的軟體需要的函式庫 - `/usr/local` 也可以放第三方安裝軟體或原始碼(`/opt`也可以) - 將想測試的系統程式 (例如 bash)存入,再用 /etc/passwd 指定我自己開機用這 shell,不會影響到其他使用者能做測試 - **sbin** - 超級使用者可以使用的命令(具有破壞性) e.g. ifconfig -通常給超級使用者使用 week 02 中已記錄 :::success **bin、sbin、lib 已合併** - 目前新版的 ubuntu 已經將 `bin` 跟 `/user/bin`合併成`/bin` - 他們現在變成一樣的子目錄了 - 淡的藍綠色是鏈結檔(symbol link) - 黃色是charac檔裝置檔 - 白色是一般檔案 - 紅色是檔案不存在/壓縮檔 - 綠色是可執行檔 - 藍色是資料夾 ![](https://i.imgur.com/ouR8Zc8.png) ::: - **/local** - 存放系統管理員在本機自行安裝自己下載的軟體 - 下載軟體可以載到opt或usr/local - 這個目錄底下也有 `bin` `lib` `/etc` ## var&子目錄 ![](https://i.imgur.com/ebMrqpy.png) > var的子目錄: `ls /var` - 系統運作時 - `/var`存放常變動的檔案 - log(紀錄檔)、email、cache(快取)以及某些軟體產生的檔案 - 系統運作才會漸漸佔據硬碟容量的目錄 ### 需要存在的目錄 `/var`裡面的目錄 - **/cache** - 應用程式本身運作過程會產生一些暫存檔 - eg - apt install 留下來的 package - 抓下來的東西都會先存在這邊 - **/lib** - 放置程式本來執行中,需要用到的檔案 - e.g. `mySQL` - **/lock** - **/log** - 紀錄檔案放置的位置 - 這裡的檔案有些重要 - 開機相關 `boot.log` - 登入相關的 `auth.log` - kernel相關的 `kern.log` - 系統的紀錄 `syslog` (系統有問題時來這裡查看~) - 大部分檔案都是純文字檔 > /var/log/ 目錄 > ![](https://i.imgur.com/IU3umCB.png) - btmp & vtmp 是壓縮檔 - **/spool** - 通常放置一些佇列資料 - 這些資料通常被使用後通常會被刪除 - **/mail** - 放置個人電子郵件 - 這個目錄也被放置到`` :::info **`last`** - 會show 出近期有哪些使用者 **`lastlog`** - 最後一次登入有哪些使用者 **`who`** - 現在系統上有哪些使用者登入 **`w`** - 顯示系統上有哪些人,和他們在做什麼 ::: - **/lost+found/** - 每一次開機都會有 - 每一個磁區都有 - 這個子目錄平常是空的,當系統非法關機後,如果你有丟失一些文件,系統能救回來就會放在這裡 - **/proc** = process - 虛擬的目錄 - 放置的資料都是在記憶體當中 - 以前要檢視記憶體中的狀態極難處理,透過 procfs 將系統的狀態理成了一個系統的目錄 > ![](https://i.imgur.com/tZszeu1.png =450x400) - /proc/???/environ 環境變數 - :::info **hidden process** - 通常是惡意程式 - `cat cmdline`: 程式被執行的時候使用的指令 - `cat cwd` - `export` ::: - **/sys** (system) - 虛擬的目錄 - 和`proc`相似,虛擬檔案系統(virtual filesystem) 放置的資料都是在記憶體當中 - 例如: 系統核心、行程資訊(process)、周邊裝置的狀態及網路狀態等等 - 因為都是在記憶體當中,所以本身不佔任何硬碟空間~ - 存放 kernel 裡相關的變數或參數 - 可以直接將想要輸入的值寫入?直達天聽 `ps -aux` - 列出系統在執行的程式?

    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