leafish
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me 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
    • Save as template
    • 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 Create Help
Create Create new note Create a note from template
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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me 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
    --- title: 基本指令 --- [TOC] ## Linux 常用的指令 ### 查閱系統指令、工具和功能的說明檔案的工具 | 指令|簡介| |-|-| |man|Manual,用來查閱系統中每個命令或程式的手冊| |info|命令提供了比 `man` 更詳細、更結構化的檔案說明| |whatis|更簡潔的指令說明,它只會返回一行簡短描述| |type|查詢某個指令的型態| |file|確定檔案型態| |uname|顯示有關 Linux 系統基本資訊| |!!|上一個指令 ex.`sudo !!`會用sudo執行上一個指令| |^r|搜尋之前用過的指令| ```bash= man ls # 查看 ls 命令的手冊頁 <--ls 命令的手冊頁(下略)--> info ls # 查看 ls 命令的 info 文檔 <--ls 命令的 info 文檔(下略)--> whatis ls # 返回一行描述 ls 命令的功能 ls (1) - list directory contents LS (6) - display animations aimed to correct users who accident... type ls # 查詢 ls 命令的型態 ls is alias to 'ls --color=auto' file /bin/ls # 確定 /bin/ls 的檔案型態 /bin/ls: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=36b86f957a1be53733633d184c3a3354f3fc7b12, for GNU/Linux 3.2.0, stripped uname #顯示有關 Linux 系統基本資訊 Linux uname -a #顯示詳細系統資訊 Linux leafish 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux ``` ### 常用的網路相關工具 | 指令|簡介| |-|-| |netstat|查看系統的網路連接和統計數據| |ss|現代化的 socket 統計工具,替代 `netstat`,顯示網路連接、監聽port等| |dig|DNS 查詢工具,用於獲取域名的 DNS 資訊| |ping|測試網路連通性,檢測主機是否可達並計算延遲時間| ```bash= netstat -an # 顯示所有連接並以數字格式顯示 netstat -r # 顯示路由表 ss -tuln # 顯示所有 TCP、UDP 監聽端口,以數字格式顯示 ss -p # 顯示與連接相關的程序 dig example.com # 查詢 example.com 的 A 記錄(IPv4 地址) dig google.com MX # 查詢 google.com 的 MX 記錄(郵件交換器) dig @8.8.8.8 example.com # 使用 Google 的 DNS 伺服器(8.8.8.8)查詢 ping example.com # 向 example.com 發送 ICMP 數據包,測試連通性 ping -c 4 google.com # 向 google.com 發送 4 個 ICMP 數據包 ``` ### 操作檔案相關的指令 | 指令|簡介| |-|-| |pwd|<font color="red">p</font>rint <font color="red">w</font>orking <font color="red">d</font>irectory 顯示當前目錄位置| |ls|<font color="red">l</font>i<font color="red">s</font>t 將當前目錄底下的檔案列出來<br>| |cd|<font color="red">c</font>hange <font color="red">d</font>irectory 切換路徑| |mkdir|<font color="red">m</font>a<font color="red">k</font>e <font color="red">dir</font>ectory:建立資料夾| |touch|新增檔案:touch 檔名.副檔名| |cat|~~貓~~ 正確解釋:con<font color="red">cat</font>enate, 但我比較喜歡:call at terminal的說法| |head|用來顯示檔案的**前面幾行**,預設顯示 10 行| |tail|用來顯示檔案的**最後幾行**,預設顯示 10 行| |more|用來**分頁顯示**檔案內容的命令,適用於顯示較大的檔案| |less|類似 more,但可以上下滾動並支援搜索功能,更靈活| |cp|<font color="red">c</font>o<font color="red">p</font>y :要被複製的檔案路徑 複製檔的檔案路徑| |rm|<font color="red">r</font>e<font color="red">m</font>ove| |rmdir|<font color="red">r</font>e<font color="red">m</font>ove <font color="red">dir</font>ectory ([與 `rm -r ` 差異](https://teamtreehouse.com/community/rm-r-vs-rmdir))| |mv|<font color="red">m</font>o<font color="red">v</font>e| ### pwd 顯示當前目錄位置 <font color="red">p</font>rint <font color="red">w</font>orking <font color="red">d</font>irectory <br>![image](https://hackmd.io/_uploads/SyLtNXppA.png) *** ### cd 切換路徑 <font color="red">c</font>hange <font color="red">d</font>irectory ![image](https://hackmd.io/_uploads/ByZ9d7paA.png) * `cd ..`:可以回到上一個目錄 * `cd .`:當前目錄 * `cd -`:前一個工作目錄 * `cd ~`: 當前 user 的家目錄, 直接下 cd 也可以回家目錄 *** ### mkdir 建立資料夾 <font color="red">m</font>a<font color="red">k</font>e <font color="red">dir</font>ectory ![image](https://hackmd.io/_uploads/rJRWiXTpA.png) *** ### touch 新增檔案 ![image](https://hackmd.io/_uploads/Skr9hE6pC.png) ::: info **文字編輯** #### nano 指令`nano <檔名>.txt`進入文字編輯,開始撰寫內容。 要離開的話,下方已經很好心的提示我們怎麼離開:^X Exit > 這邊的 ^ 是 ctrl 的意思,在 linux 環境裡面是很常見的縮寫方式。 ![image](https://hackmd.io/_uploads/By1FR46pC.png) 按下 ctrl + X 之後,nano 會提示是否要存檔,依據指示設定存檔格式之後,就可以看到剛剛建立的檔案了。 --- #### vim 指令`vim <檔名>.txt`,進入vim。 vi 共分為三種模式,分別是『一般指令模式』、『編輯模式』與『指令列命令模式』。 - 一般指令模式 (command mode):一打開就直接進入(預設模式),可以刪除、複製、貼上檔案內的文字,無法編輯檔案內容。 - 編輯模式 (insert mode):在一般模式當中,輸入『i, I, o, O, a, A, r, R』後才會進入編輯模式,離開時按Esc即可回到一般指令模式。 - 指令列命令模式 (command-line mode):在一般模式當中,輸入『 : ,/, ? 』三個中的任何一個按鈕進入指令列命令模式,對檔案進行讀取、存檔、大量取代字元、離開 vi 、顯示行號等等的動作。 :::spoiler 三種模式關係圖 ![image](https://hackmd.io/_uploads/H1tnzIxCA.png) [圖鏈來源](https://linux.vbird.org/linux_basic/centos7/0310vi.php) ::: *** ### cat 印出檔案內容 con<font color="red">cat</font>enate ![image](https://hackmd.io/_uploads/SJcQ1HpaR.png) *** ### ls 將當前目錄底下的檔案列出來 <font color="red">l</font>i<font color="red">s</font>t ![image](https://hackmd.io/_uploads/B15fr7apC.png) 開頭為`.`的為隱藏檔案,使用`ls -a`即可看到![image](https://hackmd.io/_uploads/B165Lmap0.png) ->想看到更多詳細資訊,用`ls -l`顯示詳細資訊![image](https://hackmd.io/_uploads/ryr_PQTaA.png) ->組合技`ls -la`,顯示詳細資訊+隱藏檔案![image](https://hackmd.io/_uploads/BydG_7TTA.png) **其中含義** ![image](https://hackmd.io/_uploads/BJVYGEa6C.png) 1. `白色` **檔案類型與權限(Mode)**:第一個字元表示檔案的類型: * -:一般檔案(regular file) * d:目錄(directory) * l:符號連結(symbolic link) * b:區塊設備檔案(block device file) * ex. `/dev/loop` * c:字元設備檔案(character device file) * ex. `/dev/tty` 2. `紅色` 隨後的 9 個字元分為 3 組,分別表示檔案擁有者、群組、其他使用者的讀取、寫入與執行權限: * r:讀取權限(read) * w:寫入權限(write) * x:執行權限(execute) 3. `橘色`**連結數量(Links)**: 顯示這個檔案或目錄的連結數量。 4. `黃色`**擁有者(Owner)**: 檔案的擁有者名稱。 5. `綠色`**群組(Group)**: 檔案所屬的群組名稱。 6. `藍色`**檔案大小(Size)**: 檔案的大小,以位元組(bytes)為單位。 7. `粉色`**最後修改時間(Timestamp)**: 檔案最後修改的日期和時間。格式為月、日和時間。如果修改時間超過一年,會顯示年份而非時間。 8. `紫色`**檔案名稱(Name)**: 檔案或目錄的名稱。如果是符號連結,會顯示連結到的目標。 :::info #### 檔案時間參數 * mtime(modification time) `ls -l` * 當該檔案的『內容資料』變更時,就會更新。 * ctime(status time) `ls -lc` * 當該檔案的『狀態 (status)』改變時,就會更新這個時間 * 像**權限**與**屬性**被更改了,都會更新這個時間。 * atime(access time) `ls -lu` * 當『該檔案的內容被取用』時,就會更新這個讀取時間 (access)。 * 像使用 cat 去讀取,就會更新該檔案的 atime 。 ::: * `ls -r`: 以反向排序列出檔案 * `ls -R`: 列出目錄下所有檔案,包含目錄下的資料夾內的所有檔案 * `ls -t`: 依照檔案建立時間列出 * `ls -S`: 依照檔案大小排序 :::success #### 牛刀小試 試試`ls -rRts`? ::: *** ### cp 複製檔案 <font color="red">c</font>o<font color="red">p</font>y :要被複製的檔案路徑 複製檔的檔案路徑 (source to destination) ![image](https://hackmd.io/_uploads/S1UQzH6pC.png) * `cp -r <source> <destination>`:複製資料夾(r = recursive) * `cp -p <source> <destination>`: 連同檔案的權限、擁有者、時間都複製 -> 常用於備份 *** ### mv 移動檔案(剪下貼上)/重新命名 <font color="red">m</font>o<font color="red">v</font>e ![image](https://hackmd.io/_uploads/SJEDISa60.png) - destination:可以是 路徑+新檔名/改路徑/改檔名 *** ### rm 刪除檔案 <font color="red">r</font>e<font color="red">m</font>ove ![image](https://hackmd.io/_uploads/B1VkQraaA.png) * `rm -r`:刪除資料夾(r = recursive) * `rm -rf`:可<font color="red">強制</font>刪除檔案,<font color="red">要小心使用!</font> :::danger **摧毀系統(電腦)的指令** ```bash= rm -rf --no-preserve-root / ``` 後果自負:D ::: > `rmdir`與`rm -r`的差異: > `rmdir` only works if the folder is empty. `rm -r` will remove the folder and everything inside of it. *** ### adduser & useradd 新增使用者 `adduser` 和 `useradd` 兩個都是用來新增使用者的命令,雖然功能相似,但還是有些不同。 - `adduser` 是功能比較多的命令,它會自動建立家目錄、設定密碼等。 - `useradd` 需要手動設定選項。它不會自動建立家目錄或設定密碼。 -> 沒有特別用處就不要用這個建立使用者 ```bash= sudo adduser newuser # 添加新使用者 newuser sudo useradd -m newuser # 創建新使用者並自動創建家目錄 # 通常會搭配 passwd 命令來設定新使用者的密碼: sudo passwd newuser ``` - `userdel --remove <username>` :移除使用者 *** ### su 切換使用者 - `su`:僅僅是切換使用者身份,保持當前的 shell 和環境變數。 - 如果在普通使用者的目錄中使用 su,切換到 root 後,你仍然會停留在普通使用者的目錄,並保持原有的 PATH。 ``` su username ``` - `su -`:不僅切換身份,還會初始化目標用戶(如 root)的登入環境,進入該用戶的 home 目錄,並使用該用戶的環境變數。 - 切換使用者後用 `exit` 指令來退出 **:-1: 不要用 su 切換成 root** - Root 密碼外洩風險:su 需要輸入 root 密碼,一旦有人獲取了 root 密碼,就可能做任何系統層級的改動。 - 無法辨識原始使用者:當你使用 su 切換到 root 後,系統只知道當前正在執行的是 root,而無法追溯是哪個原始使用者進行的切換和操作。 - 無法追蹤使用者行為:使用 su 切換到 root 會使系統失去對具體使用者的操作日誌的追蹤能力。這使得難以確定誰做了什麼,更加增加了調查和審核的困難度。 - 無法限制行為:使用 su 後,你將完全擁有 root 權限,這使得你能夠在系統上做任何事情,而不受任何限制。 **:+1: 使用 sudo** - 無需洩漏 root 密碼:使用者只需自己的密碼,而不需要知道 root 密碼。 - 可追蹤行為:sudo 日誌會記錄每個使用者執行的每條命令,這樣可以清楚地知道誰執行了什麼操作。 - 權限控制:sudo 配置文件 (/etc/sudoers) 允許系統管理員為不同使用者設置特定的權限,限制他們只能執行某些命令,而不是獲得完全的 root 訪問權限。 *** ### UID、GID 概念 - **UID(User Identifier)**:在 Linux/Unix 系統中,每個使用者都有一個唯一的使用者 ID(UID)。這是系統用來識別每個使用者的方式。 - UID 0 通常是 root(超級使用者)。 查看某個使用者的 UID: ```bash= id -u username # 查看使用者 username 的 UID ``` - **GID(Group Identifier)**:每個群組都有一個唯一的群組 ID(GID)。檔案的群組所有權由其 GID 來決定。 查看某個使用者的 GID: ```bash= id -g username # 查看使用者 username 的 GID ``` 查看檔案的 UID 和 GID: ```bash= ls -l filename # 列出檔案的詳細信息,包括所有者 UID 和群組 GID ``` ::: :::success **DEMO** 變更使用者 id - 把兩個使用者的 UID 變成一樣的 - 把兩個使用者的 UID 對調 更改使用者 id -> sudo vim /etc/passwd ::: ### groupadd 建立新群組 ```bash= sudo groupadd -g <group-ID> <group-name> ``` - 用 `sudo usermod -aG <group_name> <username>` 來新增使用者至群組 - `usermod`:修改使用者帳號的命令 - 查看群組:`cat /etc/group` - ![image](https://hackmd.io/_uploads/rkXw-wm11g.png) *** ### chown(Change Owner) `chown` 命令用來更改檔案或目錄的所有者。 ```bash= sudo chown newowner filename # 將檔案的擁有者權改為 newowner ``` Why 要 sudo? -> 因為還要對方同意,所以直接 sudo 調解 *** ### chgrp 改變所屬群組 要被改變的群組名稱必須要在`/etc/group`檔案記憶體在。 ```bash= sudo chgrp newgroup filename # 將檔案的群組所有權改為 newgroup ``` *** ### chmod 更改檔案或目錄權限 chmod 命令用來更改檔案或目錄的權限(讀取、寫入、執行)。 權限模式: - `r`:讀取權限(4)。 - `w`:寫入權限(2)。 - `x`:執行權限(1)。 ```bash= chmod u=rwx,g=rwx,o=rwx filename chmod u+w filename # 給檔案擁有者增加寫入權限 chmod u-w filename # 給檔案擁有者拔除寫入權限 chmod 755 filename # 將權限設為 rwxr-xr-x(所有者可讀寫執行,群組和其他人可讀執行) # 每個權限使用一個數字來表示,三位數分別代表所有者、群組、其他人的權限,權限位可以疊加。 ``` 權限分為所有者(user)、群組(group)、其他人(others)三類。 **特殊權限** - 1 為 SBIT - 2 為 SGID - 4 為 SUID :::info 如果你執行 ```bash= ls -ld /tmp ``` 你應該能看到類似這樣的輸出: ![image](https://hackmd.io/_uploads/SJt32FDRR.png) 注意最右邊的有個 t,這叫做 sticky bit,一種特殊的 bit,它的主要作用是限制檔案的權限。 **Sticky Bit 的作用** 當 sticky bit 被設定在一個目錄上時,該目錄內的檔案和子目錄只能由以下幾類使用者刪除或重命名: - 檔案的所有者。 - 目錄的所有者。 - root 超級使用者。 即使其他使用者擁有該目錄的**寫入(read)權限**,他們也不能**刪除或修改他人建立的檔案**。這在像 `/tmp` 這種目錄中特別有用,因為它可以防止使用者之間相互刪除檔案。 **這種權限被設定成 `1777`** ::: ### SUID、SGID 概念 - **SUID(Set User ID)**:SUID 位是針對可執行檔案設置的一個特殊權限。能執行此檔案的使用者在執行這個檔案時,都會以檔案擁有者的身份來運行該程式,而不是以執行者自己的身份。 **情境** passwd 指令允許用戶更改自己的密碼。這需要修改 /etc/shadow 檔案,該檔案只有 root 能讀寫。但普通用戶仍然能夠執行 passwd 並更改自己的密碼,因為 passwd 程式使用了 SUID 權限。 ![image](https://hackmd.io/_uploads/S1D3y_Ly1e.png) - 用`chmod 4xxx filename` - 當 s 出現在**檔案擁有者的 x 權限**上時(-rwsr-xr-x)就被稱為 Set UID。 ![image](https://hackmd.io/_uploads/HkNgey2AA.png) - **SGID(Set Group ID)**:可設置在檔案或目錄。當 s 標誌在群組的 x 時則稱為 SGID ![image](https://hackmd.io/_uploads/SJHBBsjyJx.png) - **設定在檔案上的 SGID**:任何使用者執行該檔案時,將會以該檔案的**群組身份**執行,而不是以執行者的當前群組身份。 - **設定在目錄上的 SGID**:這個目錄內新創建的所有檔案和子目錄,將自動繼承該目錄的群組,而不是繼承創建者的主群組。 -> 這樣每次要建立群組共編檔案就不用一個一個改權限了! - 用`chmod 2xxx filename` :::info - 小 LAB - `vim printUid.c` - ```c= #include <stdio.h> #include <unistd.h> int main() { printf("The uid is: %d\n", geteuid()); return 0; } ``` - `sudo apt install gcc` - `gcc printUid.c -o printUid` - `./printUid` - `sudo chown root printUid` - 將 printUid 的檔案擁有者改成 root - `sudo chmod 4755 printUid` - 最左邊的 4 表示設置 SUID 位:當普通使用者執行這個檔案時,該程式會以檔案擁有者 root 的身份執行 - 7 表示擁有者有讀、寫、執行的權限 - 5 表示群組和其他使用者有讀和執行的權限 - `./printUid` ::: ::: *** ### head 顯示檔案的前面幾行 head 命令用來顯示檔案的前面幾行。 預設情況下,head 顯示檔案的前 10 行,但可以使用選項來指定顯示的行數。 ```bash= head filename.txt # 顯示前 10 行 head -n 5 filename.txt # 顯示前 5 行 ``` *** ### tail 顯示檔案的最後幾行 tail 命令用來顯示檔案的最後幾行。 與 head 相似,預設顯示最後 10 行,並可透過選項調整顯示行數。此外,tail 還可以持續監控檔案末尾的變動,常用於日誌檔案的實時監控。 ```bash= tail filename.txt # 顯示最後 10 行 tail -n 20 filename.txt # 顯示最後 20 行 tail -f logfile.log # 實時監控檔案的變動(常用於監控日誌) ``` *** ### more 分頁顯示檔案內容 more 是一個用來分頁顯示檔案內容的命令,適用於顯示較大的檔案。 它會一次顯示一頁的內容,按空格鍵可以繼續往下看,但只能從上往下查看,**不能返回**。 -> 會一次讀完整個檔案 ```bash= more filename.txt ``` - 按下 `空格鍵` 向下翻一頁。 - 按下 `Enter` 向下翻一行。 - 按下 `q` 退出。 *** ### less 上下滾動查看檔案內容 less 命令功能與 more 相似,但比 more 更加靈活。less 可以上下滾動查看檔案內容,並且不需要一次將整個檔案加載到記憶體中,因此在查看大檔案時非常有效率。 ```bash= less filename.txt ``` - 使用 `Page Up` 和 `Page Down` 進行翻頁。 - 使用 `q` 退出。 - 搜尋功能,按下 `/` 鍵後輸入搜尋字串,並按 `n` 查找下一個。 *** ### echo 將字串輸出到終端上 ![image](https://hackmd.io/_uploads/HkwBF8p60.png) * `echo -n`:不換行 * `echo *.副檔名`:印出所有的‘.副檔名‘檔案 * 使用指令 `echo $PATH` 印出 PATH 環境變數 * path:系統要到哪些路徑底下找執行檔 * 內容全部都是路徑,每個路徑之間用分號( ; )隔開 * ![image](https://hackmd.io/_uploads/SyfKkiWAR.png) :::info 在前面我們有提到 > 每個執行的 Process 都被表示為檔案,可以在/proc目錄下找到相應的檔案,如/proc/{PID}/status和/proc/{PID}/cmdline。 那我們在的終端機裡打指令,終端機也會有一個 pid,而查看當前終端機 pid 的方法就是`echo $$` * 使用指令 `echo $$` 印出當前 shell 的 pid (process id) * ![image](https://hackmd.io/_uploads/Bk8xz_8Jyg.png) -> 24198 為我目前的 shell pid * ![image](https://hackmd.io/_uploads/SJ07G_Ikye.png) -> 查看 vim 的 pid 方法(不能離開 vim) 除此之外,在 Linux/Unix 系統中,`top` 和 `ps` 則是可用來監控和管理 process 的兩個重要工具 **top** 的主要功能: - 顯示系統當前狀態:包括 CPU 使用率、RAM 使用情況、正在運行的 Process 數量等 - 動態更新:每隔幾秒鐘(預設是 3 秒)自動更新 - 按需排序和篩選:可以根據 CPU 使用率、RAM 使用等指標排序,查看最消耗資源的 Process - 與 Process 交互:可以直接在 `top` 界面中發送信號(如 `kill`)來終止 Process。 **ps** 只會顯示命令執行時的系統狀態(靜態),不會持續更新 **ps** 的主要功能: - 列出 Process 狀態:包括 Process 的 PID、狀態、CPU 和 RAM 使用等。 - 靜態輸出:與 `top` 不同,`ps` 是靜態的,顯示當前某一刻的系統狀態,不會自動刷新。 - 過濾和篩選程序:可以通過各種選項篩選、過濾或格式化輸出 Process 資訊。 ```bash= ps aux # 列出所有程序的詳細信息 ps -ef # 顯示程序的完整格式 ps -u username # 列出特定使用者的程序 ps -p PID # 顯示特定程序的詳細信息 ``` ::: *** ### history 顯示用戶執行過的命令歷史 在 Bash 中,會記錄之前輸入的命令。我們可以通過 `history` 查看這些被紀錄的命令。 ```bash= history ``` `history` 的一些進階用法 引用命令行 第`n`行 ```bash= !n ``` 引用命令列中的倒數第 `n` 行 ```bash= !-n ``` 引用上行指令,亦即 `!-1` ```bash= !! ``` 引用最近的以 string 開始的命令。 ```bash= !string ``` ### alias 將指令簡化 - `alias`:查看 alias - `unalias <command>`:刪除 alias From this :cry: ![image](https://hackmd.io/_uploads/r1AOS0P0A.png) To this :sunglasses: ![image](https://hackmd.io/_uploads/ryQP8RPAA.png) **Get ccat** ```bash= # terminal wget https://github.com/jingweno/ccat/releases/download/v1.1.0/linux-amd64-1.1.0.tar.gz tar xfz linux-amd64-1.1.0.tar.gz #將檔案複製到 usr/local/bin sudo cp linux-amd64-1.1.0/ccat /usr/local/bin/ #將檔案賦予執行權限 sudo chmod +x /usr/local/bin/ccat ``` **Replace cat with ccat** ```bash= vim ~/.bashrc # add a line in bashrc alias cat='/usr/local/bin/ccat' #退出後執行(更改 .bashrc 後都要執行) source ~/.bashrc ``` ### df 檢查磁碟的使用量與剩餘空間 ![image](https://hackmd.io/_uploads/SJAnyqqJyl.png) - **欄位解釋** - Filesystem:磁碟或分區的名稱。 - Size:總大小。 - Used:已使用空間,單位是 1KB。 - Avail:可用空間。 - Use%:使用比例。 - Mounted on:該磁碟或分區被掛載到的目錄。 - `-h`:`df`是用 KB 為單位,加上 -h 可變成適合閱讀的方式顯示磁碟用量。 - `-T`:查看各個磁碟分割區的檔案系統類型。 -> 較好去修復壞掉的儲存裝置 :::spoiler 圖中詳細資訊 - tmpfs (/run): tmpfs 是一種基於 RAM 的臨時文件系統,用來存放運行時需要的暫時文件。 這個具體掛載點是 /run,用來存放一些需要在系統運行時保存在記憶體中的文件。 - /dev/mapper/vgubuntu-root (/): 這是根目錄 / 的文件系統。 - tmpfs:基於 RAM 的臨時文件系統 - (/dev/shm)用來做共享記憶體。 - (/run/lock)專門用來保存鎖文件(lock files),這些文件可以防止多個進程同時操作某些資源。 - (/run/user/1000)專門為 UID 1000(通常是當前登錄用戶)分配的臨時文件系統,用來存放這個用戶運行過程中的暫時文件。 - /dev/sda2 (/boot/efi): 這是 sda2 是你硬碟的第二個分區,用來存放啟動加載器和其他系統啟動相關的文件。 ::: *** ### lsblk 列出系統的磁碟與磁碟分割資訊 顯示系統中的設備(如磁碟、分區等),並展示它們的層次結構和掛載點。主要用於系統中的磁碟結構,而非磁碟的空間使用情況。 ![image](https://hackmd.io/_uploads/BJGhecckkl.png) - **欄位解釋** - NAME:設備名稱。 - MAJ:MIN:kernel 認識的裝置的代碼,主要:次要裝置 代碼。 - RM:表示設備是否可移除(Removable)。值為 1 表示該設備是可移除的(如 USB 隨身碟),值為 0 則表示不可移除。 - SIZE:該磁碟或分區的大小。 - RO:該磁碟是否是只讀(Read-Only)。值為 1 表示只讀,0 表示可讀可寫。 - TYPE:設備的類型,包括 disk(磁碟)和 part(分區)。 - MOUNTPOINTS:該磁碟或分區掛載到的目錄,表示該磁碟的文件系統在哪個目錄被使用。 :::spoiler 圖中詳細資料 loop 裝置:用來將普通文件(例如 ISO 映像文件或壓縮的 Snap 軟體包)模擬為塊設備,從而可以掛載並操作這些文件。其中的 Snap 是用於在多個 Linux 發行版上提供一致的軟體安裝方式。 sda:代表系統中的一個物理磁碟裝置(通常是 HDD 或 SSD)。 ::: ### mktemp 自動建立暫存檔案 自動建立檔名不重複的暫存檔案或目錄,方便程式或指令存放資料。 ![image](https://hackmd.io/_uploads/SJqU35c11g.png) - `mktemp -d`:建立暫存目錄 *** ### awk 將輸出變成指定格式 awk 指令在讀取檔案或資料流時,以一行為單位,一次單以一行進行處理,預設的欄位以空白或 Tab(\t) 進行分隔,分隔完的欄位值從左邊第一欄為 $1 開始,往右遞增累加。在使用 awk 時,除了直接讀取檔案之外,也可以利用 pipe(|) 將資料流導向給 awk 進行處理。 - 取得欄位值 ![image](https://hackmd.io/_uploads/r1ls4jqJJe.png) - BEGIN,END 的用法 利用 BEGIN 關鍵字幫 awk 產出的結果在第一行加上一些說明,更可以使用 END 幫產生的結果加上一些註釋。 ![image](https://hackmd.io/_uploads/r1hbLo9ykl.png) - if-else 用法 (創建一個文件 (dd.txt)) ![image](https://hackmd.io/_uploads/HkYP_iqJkx.png) - 正規表示式篩選 用法 ![image](https://hackmd.io/_uploads/Bkphui5J1l.png) -> 列出 B 開頭的資料列 *** ### sl - 重要套件 - 會考會考歐 ```bash sudo apt install sl ``` *** :::warning ### df -h -> zfs 截ext4的圖 介紹欄位、意思,尤其是**mount on** 哪個partition掛在哪個掛載點 ->較好去修復壞掉的儲存裝置 ![image](https://hackmd.io/_uploads/ryhTCxyR0.png) - 有隨身碟掛載點 - 有 nfs - Synology NAS to Ubuntu with ZeroTier One(VPN) - 所以 IP 才會是 `192.168.193.64`,可自己設定 ::: :::warning 需補的指令 * grep -> (簡單的)正規表達式 -> 蓬萊人偶 需補參數 * \>、>> ->蓬萊人偶 stdin、stdout、stderror ::: :::info 好康的分享 - Ubuntu Dock like Windows(點一下左邊應用程式圖示(Ex. Firefox)可以打開和最小化視窗) [name=RegChien] `gsettings set org.gnome.shell.extensions.dash-to-dock click-action 'minimize'` ::: :::danger 助教的筆記 1. /boot 是 bootstrap? 電腦自己會載入/boot再找到映像檔(? 2. inode 1 是什麼?為什麼root不是 1 ? 保留給虛擬檔案,指到1的都沒有儲存在硬碟上 ![image](https://hackmd.io/_uploads/Hy0j_W1AR.png) [資料來源](https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout#Special_inodes) [資料來源2](https://unix.stackexchange.com/questions/198673/why-does-have-the-inode-2) 3. inode怎麼教? 在ls教 4. cd 是builtin,why? 改變pwd,切換環境(working directory) 5. `sudo -i` 為什麼是打使用者的密碼,那這樣大家都打各自的密碼都能到root? 要有sudo權限才可以(創使用者時可將該使用者拔除sudo權限) 6. 救援檔案->檔案會存在硬碟內(資料沒有被馬上刪除)->mtime影響 7. etc/skel -> 放骨架。創建新使用者時,此檔案內的資料會一併複製到新使用者家目錄。 8. getent group sudo -> 看 sudo 群組 9. sudo usermod -aG sudo <Account> -> 把使用者加進 sudo :::

    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