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 05 (2022/10/13) [TOC] - Book mode: https://hackmd.io/@ncnu-opensource/book --- ## 創建、切換 使用者 - ![](https://i.imgur.com/PivyHy1.png) - `whoami`: 顯示目前使用者 - `sudo whoami`: root - `groups`:群組 - ![](https://i.imgur.com/APgCv83.png) - `adduser`:創建使用者以及他的家目錄 - `useradd`:創建使用者 (不會自動增加其新來的使用者的 home 目錄) - 創造使用者需要比較大的權限,因此需要先輸入 `sudo` - `sudo su -` + username : 切換使用者 - `exit`: 離開這個使用者 ## 顯示目錄裡面的檔案 - `pwd`: 顯示目前工作之目錄 - `ls` + 其他參數:可以看到其他資訊 - `-a`: 會顯示隱藏的內容(.file) - `-al`: 看到更詳細的資訊 - `d` 開頭代表是資料夾,可以透過`cd`切換過去 - `-` 開頭代表是檔案 - 數字: link數(包含hardlink & softlink) - `ln` + 檔案名稱 + link的名稱: 建立一個指到檔案的link ## hardlink v.s. softlink - <font color="blue">例子前提: `a`記住了`某項東西`</font> - hardlink: 兩個檔案都指向同一個東西 - <font color="blue">怕之後找不到`a`,找不到`某項東西`,所以請`b`也幫忙記</font> - 可以幫忙做同個檔案的 link 備份,可以避免檔案不小心刪除 > 像是渦蟲,可以無限分裂 > 多人幫忙記得這個東西,如果一個人忘記了(被刪掉之類),還有另一個人記得 - softlink: 一個檔案指向另外一個檔案 - <font color="blue">再新增一個`c`,記住「`a`記住了`某項東西`」這件事情,但是`c`本身並沒有記`某項東西`的位置,所以如果`a`被刪掉,只靠`c`也不會知道`某項東西`在哪邊</font> - 請別人幫忙記住「知道這個檔案的檔案」 > 比較像影分身: 如果本體gg,分身也會消失 > [硬連結?軟連結?](https://www.techbang.com/posts/12538-hard-links-soft-links-archives-does-not-fashu) ## 文字編輯器 - vim: vi的升級版,可以調一些文字顏色或設定 > [[Linux] vi 與 vim 指令一覽表整理](https://code.yidas.com/linux-vi-vim-command/) - `i`: 可以進入編輯模式,左下角出現 --insert-- - `esc`: 變回一般模式 (以下要在一般模式下進行) - `a`:跳到下一個位置 - `A`:跳到最後一個位置 - `o`: 自動開新的一行 - `cc`:刪掉一整行 - `:wq`: 存檔離開 - `:q!`: 不存檔直接離開 - `:u` : 回到上一步 - `gg`: 移到第一行的第一個位置 - `ZZ`: 儲檔並關閉檔案,等同於 `:wq` - `h`: 左 - `j`: 下 - `k`: 上 - `l`: 右 - `:set nu` : 顯示第幾行 - `5G`: 跳到第5行 - `v`: 選取某個範圍 - `V`: 框起一整行 - `vim .vimrc`: 增加設定,這樣下次系統自動就會幫我們下好指令 > [vimrc設定](http://wiki.csie.ncku.edu.tw/vim/vimrc) > `rc`: run command > 要在家目錄(`~`)打才有用 - nano: 很古早,會有一些換行的問題 ## 檔案權限 - rwx = - read(權限: 4) - write(權限: 2) - execute(權限: 1) - ![](https://i.imgur.com/YzNDA5Q.png) 1. 第一組代表: owner ![](https://i.imgur.com/4cAUhf3.png) 2. 第二組代表: group ![](https://i.imgur.com/R74DUkb.png) 3. 第三組代表: other user ![](https://i.imgur.com/iHMOlpI.png) - chmod: 設定權限 - `chmod u=r a.txt`: - `chmod u+x a.txt`: 給這個user執行 `a.txt` 的權限 - `chmod u-x a.txt`: 不給予這個user執行 `a.txt` 的權限 - `chmod 777 a.txt`: 所有人都可以讀、寫、執行 `a.txt` - ![](https://i.imgur.com/2RJrL0v.png =300x) :::info - 7: 可讀可寫可執行 - 對於檔案來說,下 `777` 是給予很大的權限,故要小心不要隨便給 other user 那麼大的權限 - 5: 可讀可執行 - 1: 只可執行 ::: - `chown` : 把檔案轉交給其他人 - sudo 不被規範在權限內 - `ls -al a.txt`: 看a.txt的詳細資料 - `mkdir <資料夾名稱>` : 新增資料夾(make direction) ## `/etc/` 檔案目錄 - `/etc/passwd`: 放使用者的密碼檔 - ![](https://i.imgur.com/9F4epY2.png) - 一般使用者也可以查看(不需要`sudo`) - 每一行都是一個使用者的帳號資訊 - 有一些帳號是為了系統正常運作而存在的,不可以亂刪 - ex. nobody - 不同的資訊都是用`:`去隔開 - ![](https://i.imgur.com/2wRD1T4.png) - `使用者名稱`:`密碼(顯示x)`:`UID`:`GID`:`使用者資訊`:`使用者家目錄在哪`:`在哪下command` - `/etc/shadow`: 存放真正的密碼,資料都經過加密 - 要使用sudo才可以查看 >GID : group ID ### `/etc/passwd` - 欄位架構: - 使用者名稱 - 密碼 - 使用者id - 使用者所在的群組 - 使用者資訊 - 使用者家目錄所在 - 下指令的位置 ### `/etc/shadow` - 要使用 sudo 才能進入(一般使用者無法看) - 欄位架構: - 使用者名稱 - ![](https://i.imgur.com/j0irfui.png) - 加密後的密碼 - ![](https://i.imgur.com/ehlGpTO.png) - $n$ : n為數字,代表加密的方式 - **上一次密碼變更的天數** - ![](https://i.imgur.com/iZniMos.png) - 從 1970 年 1 月 1 日算起的天數 - [Unix Time](https://zh.wikipedia.org/zh-tw/UNIX%E6%97%B6%E9%97%B4) - 密碼最短使用期限 - ![](https://i.imgur.com/kfibQ4u.png) - 兩次變更密碼最短需要間隔多久,單位為天 - 如果此欄位為0, 表示隨時可更改密碼 - 密碼最長使用期限 - ![](https://i.imgur.com/xWNTb0o.png) - 密碼最多可以使用多久,單位為天 - 如果此欄位為空,表示關閉此功能 :::info - 如果**密碼最短使用期限** $>$ **密碼最長使用期限** - 使用者就不能改密碼 - 例如 - `密碼最長使用時間` 設為100天 - `密碼最長使用期限` 設為 10天 ::: - 密碼過期前發出警告的期限 - 密碼過期前多久,系統會對使用者發出警告,單位為天 - 如果此欄位是`0`則代表關閉此功能 - 密碼過期後的放寬天數 - 當密碼過期後,使用者還有多久的時間可以登入更改密碼,單位為天 - 如果此欄位是空的,則代表關閉此功能 - 帳號過期時間: - 此帳號過期的時間點 ### 密碼欄位 - `x`: `/etc/passwd` 中密碼都用 `x` 表示 - 對應到存在`/etc/shadow`的密碼 - `!`:此帳號被鎖定,不能用密碼登入 - 用於使用者 - `*`: 此帳號被鎖定,不能用密碼登入 - 用於服務 - ` `:沒有密碼 - 如果此時`passwd` 會直接進入設定密碼的環節 - 有些讀取 `/etc/shadow` 的檔案,可能會因為此欄位為空就不提供服務 ### 使用者帳戶相關實作 修改`/etc/shadow` :::danger **注意事項** 要注意自己下什麼指令,並做好改壞要重裝的決心。 ::: - 新增使用者: `sudo adduser 使用者名稱` - 登入(兩種方式): 1. 切換使用者: `sudo su - 使用者名稱` 2. `ssh -v 使用者名稱@主機名稱` > `-v` :若有失誤時可以看出哪裡的問題 - `logout`: 登出切回原本使用者 - ![](https://i.imgur.com/OlTZVsU.png) - `sudo vim /etc/shadow` : 透過vim編輯器改動`/etc/shadow/`的檔案 - 密碼為`!`或`*`表示將使用者鎖定,使用者無法修改密碼 - 密碼為空值,為更改密碼 ### 使用者帳戶相關實作 修改`/etc/passwd` - `id`: 查看目前使用者資訊 - ![](https://i.imgur.com/flTT8v2.png) - 系統是使用 uid 來查找使用者,並非名稱,所以如果把 uid 拿掉了,系統會找不到此使用者 - `id` 是逐漸增加的數字 - `pwd`: 查看目前使用者的所在路徑 - 登入後會處於的目錄 - ![](https://i.imgur.com/MIhkzTE.png) > 倒數第二個欄位 綠色反光的位置 - 若把登入後的路徑更改為一個不存在的目錄,登入後會進到一個預設路徑(通常為原本使用者的家目錄底下) :::info - Q: 我們可以到別人家目錄下新增檔案嗎? - A: No,一般使用者沒有權限在別人的家目錄做新增檔案的動作,因為在家目錄底下新增檔案的權限通常只有開放給自己 - Q: 如果將 自己 的檔案的 uid 改為 別人 的uid 會發生什麼事? - A: 自己會沒有**owner權限**使用自己原本的檔案,因為系統會依據 uid 辨別使用者,因此此時系統會將此檔案的owner 視為別人的 - A: Yes, 可以成功新增檔案到他人的家目錄,因為uid相同,會被視為同一個使用者 ::: - 修改下 commend 的位置: ![](https://i.imgur.com/HtQw63i.png) - `bin/bash`: 使用者登入的起始位置 - `bin/false`: 若嘗試登入則系統不會告訴你原因,也不會讓你登入 - `/usr/sbin/nologin`: 若嘗試登入,系統會告訴你原因,但不會讓你登入 - 改為某個指令的位置: 一登入就會執行那個檔案 > e.g.: 改為`/usr/games/sl`,一登入就會跑出小火車 :train: :::info ### 既然有`/etc/passwd`了,為什麼來要有`/etc/shadow`? - 很久以前,`/etc/passwd` 裡面是有存使用者密碼 - 但那時候的加密技術不成熟,而且有很多人把密碼設的太簡單 - 所以 `/etc/passwd`的密碼移到`/etc/shadow` 中做加密 - 現在 `/etc/passwd` 主要查看使用者資訊 - 只有`root`才可以看`/etc/shadow` ::: ## 檔案相關指令 ![](https://i.imgur.com/hHei3I6.png) - `touch`: 產生一個空白檔案 / 更動檔案的時間 - `stat 檔名` : 檔案的詳細資料 - Access: 檔案建立時間 - Modify: 改變檔案權限的時間 - Change: 改變檔案東西的時間 - 讀取檔案: - `cat 檔名`: 把檔案裡面的資訊全部直接讀出來 - `more 檔名`: 會先將整個檔案讀取過並透過分頁方式呈現(只能一頁一頁看) - press `space`: 下一頁 - press `b`: 上一頁 - press `q`: 離開 - `less 檔名`: 會先讀取部分檔案,建議檔案很大時使用 (可以用 行 或是用 頁 來看) - `tail 檔名`: 只顯示最後 10 行 - `tail -行數 檔名`: 可以指定要看最後多少行 - `head 檔名`: 只顯示前面 10 行 - `tac` : 反著讀檔案,從最後一行開始讀 - `echo 要寫入的資訊 > 檔名`: 直接將資料寫入檔案裡面 - e.g.: `echo 1235 > asd.txt`: 將1235寫入`asd.txt`裡面,清除原先的資料再寫入`>`前的資料 - `echo 欲寫入的資訊 >> asd.txt`: 不刪除原先資料,直接寫入檔案 - `lss 2> error.txt`: - 把發生的錯誤資訊寫入後面的檔案中 > 把 lss 這個錯誤的指令寫到 error 裡面 - `ls > file.txt`: 把ls的正常輸出寫到`file.txt` - `ls > file.txt 2>&1`: 把錯誤輸出視為正常輸出,寫到`file.txt` - 數字`2`代表錯誤輸出,數字`1`代表正常輸出 :::info - **紀錄錯誤訊息(把錯誤寫進 file.txt)有什麼用?** 因為使用者不是隨時都顧著伺服器故錯誤輸出很可能被忽略,所以才需要一個檔案紀錄。 ::: - `tee` - 將結果同時輸出到螢幕和把錯誤寫進檔案 - `ls | tee` - `|`: 把pipe前的輸出當作pipe後面的輸入,只能接受正常輸出,如果指令錯誤,`cat`指定的紀錄檔案會發現沒有東西 - `ifconfig`: - 要先安裝`net-tools` - `sudo apt install net-tools` - `ssh -v 使用者名稱@某台電腦的主機名稱`: 到某台電腦的使用者位置 - e.g.: 進到本機端的 test 使用者介面 - e.g. ssh -v test@localhost - e.g. ssh -v test@127.0.0.1 - `netstat -ntupl`: 可以看到目前電腦網路的詳細狀況 - `netstat -tupl`: 變成localhost - `n`: 會把能變成數字的資訊變成數字,所以拿掉`n`之後,127.0.0.53:53會變localhost:domain - `t`: 顯示 tcp - `u`: 顯示 udp - `l`: 目前監聽到的

    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