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
      • 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 Sharing URL Help
Menu
Options
Versions and GitHub Sync 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
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 06(2021/10/28) [TOC] ## 指令 - **`top`** 整合介面,顯示機器各種資源狀況 >[Linux "top" command](https://unix.stackexchange.com/questions/18918/linux-top-command-what-are-us-sy-ni-id-wa-hi-si-and-st-for-cpu-usage]) - **`up`**: uptime,從開機到現在經過多少時間 - **`user`**: 目前線上同時有幾個 user 在使用 - **`load average`**: 參考值,數字代表有多少個 process 在等待 CPU 及 Disk I/O 等資源運算 - 很高的話代表系統通常有問題,但也不一定 - 三個數字分別代表 1, 5, 15 分鐘累積的負載情況 - **`Tasks`**: 有多少個背景 process 正在執行 - running : 正在執行 - sleeping: 休眠狀態 - `zombie`: 殭屍,占用效能、記憶體空間, 不做任何事 - 所有 child process 都有 parent process,若有一 parent process 有很多 child process 卻自己消失了,剩下的 child process 會: 1. 死掉kill後沒有人幫他收屍,變成 zombie (太多殭屍的話系統會不穩定) 2. 被拉回主線程 :::info 最開始是世界上會有一個 pid 0,然後她會開始叫醒大家(pid1、pid2...) ::: - **`us`**: user space占用CPU的百分比,大部分的CPU都吃在`us` - **`sy`** : kernel執行的程式,內核空間佔用CPU的百分比 e.g.一直在for迴圈 > 健康的系統: 大部分都吃在`us`,`sy`比較少 - **`ni`** : nice值,數字越少,等級越高、優先權越高(越快被分配到系統資源)(CPU的) > `ni` 值越高,優先權越低,其數值範圍為 `-20`~`+19`。 > 當一個 process 的 `ni` 值是 `+19` 時代表它是一個~~爛好人~~,可以接受其他 process 搶他的 CPU 執行時間,這也是 `nice` 值這個名詞的由來。[name=某某城市傳說] ```terminal= man top ``` - 設定nice值 `nice -n 10 <指令>` - **使用者決定優先順序** - > `ionice` (硬碟的) > [niceness & priority](https://ubuntuqa.com/zh-tw/article/1176.html) - `id` : ~~idol~~ idle(閒置的,系統不忙的比例) , 如果id 加上其他人會幾乎等於100。如果數字很高,是正常的(很好),因為他不忙。 - `wa` : 硬碟讀寫,做大量讀寫時,數值會特別高(花了多少時間成本在等待 IO,waiting成本) - `hi` : hardware interrupts - `si` : software interrupts > 通常`hi`、`si`都很低 - `MiB Mem` - total 物理性(實體)的記憶體 - free 剩餘記憶體容量 - used 已使用的記憶體容量 - `buff/cache` 暫存記憶體的容量 - `MiB Swap` : 在記憶體找一個區塊當延伸記憶體(置換空間) - 現在的系統如果oom(out of memory)會把那些看起來很久沒用的記憶體**隨機**砍掉 - 特性: 慢,很慢,超慢,如果開swap的話會拖慢效能(因為放在硬碟),但可以避免當機 - OOM: out of memory 的時候會用到 > 伺服器 SWAP 要開!他是最後一根救命稻草 [name=BT] - 建議開(設定)兩倍記憶體的空間!! - 當記憶體快滿,要執行新的process,把很久沒用到的東西丟進swap - 如果很久沒用的東西丟到swap還不夠,就會把正在執行的也丟到swap,造成有大量的`swap in`,`swap out`,`wa`會很高 - 關鍵字 : swappiness - `PR` priority 優先度: 行程優先度 - 20 是優先度最低的 - 越小優先權越高 - **kernel 決定的** - `SHR` : share memory,共用記憶體大小 - `S` sleep - `I` idle - `R` run - `%CPU` : 他使用的CPU 的 memory - >如果你有1顆以上的CPU,那CPU的總和就會超過100%,所以使用情形可能超過100% > 一顆CPU 100%,兩顆 200% - `TIME` : CPU time(兩顆CPU ,time 乘以2) - 換算成一顆CPU的執行時間 >就像高中老師說的 :【你遲到一分鐘,五十個人就等了你五十分鐘】[name=匿名高中老師] - CPU(s): 所有CPU的總和使用情形 - 按1會顯示其他的CPU ![](https://i.imgur.com/Bu9z5JM.png) - 系統出問題: 大部分都出現在sy - CPU不夠力: 大部分都出現在us - 通常us要比較高,sy要比較低才是正常的 - PID : process id - 當我們發現某process的CPU執行率很高,想看到process進一步的相關資訊,可以到`/proc`下 進入 那個process (指令`cd <pid>`) 下 查看 - `/proc` 可以看到底下有很多目錄,就是每個 PID 擁有自己的一個目錄 - `cat cmdline` 可以看到當初是用啥指令執行的 - 還可以看到環境變數等等 > 示意圖: ![](https://i.imgur.com/NWJnEmt.png) - `uptime` : 開機到現在經歷了多久時間 - 如果時間很短,代表系統可能有問題,導致她一直重開機 - 也可能是記憶體出問題 - `nice` 調整 nice 值 - `free` 顯示目前記憶體使用情況 - 原意是空的,不是自由 - 預設的單位是kb= Mem * 1024 - ![](https://i.imgur.com/aEZLkVz.png) - `free -m` : 改以mb為單位顯示 > used - Used memory. It is calculated as: used = total - free - buffers - cache > > https://linuxize.com/post/free-command-in-linux/ - `ps aux`: 整個系統裡面在執行的process - 格式: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND - `ps`: process - `sudo iotop` 監控主機io的使用狀態(讀寫狀態) - 看哪個process在大量使用 - 看有沒有用到 swap in swap out - 安裝 ```terminal= sudo apt install iotop ``` - `TID`: thread ID - ~~`PRID`~~ `PRIO` - `IO>` : 每種硬碟都會有IO的上限,吃掉多少percent的硬碟 - `COMMAD` 可能是哪個程式或IO在做大量的讀寫 > 示意圖 > ![](https://i.imgur.com/2rTl5A4.png) - 監控主機的狀態,主要是input、output 的讀寫狀態 - `-o` : 顯示正在讀寫的程式 - `-a` : 顯示累積讀寫資料量 - `sudo apt install net-tools` - `ifconfig`: 網卡的資訊 - ![](https://i.imgur.com/fKkx2dd.png) x- 10.0.2.15: IP - netmask: 遮罩 - `netstat -ntupl` n 數字 t: tcp,顯示 TCP 的連線狀況 u udp,顯示 udp 的連線狀況。 p l - Proto: protocal e.g. tcp, tcp6, udp, udp6 - shutdown - `shutdown now` 現在關機 - `shutdown -r <時間>` 某個時間重新開機 - `shutdown -k <時間>` 印出幫你安排的關機時間 - `shutdown -c` 取消安排的關機時間 - `shutdown -h <時間>` 讓系統暫時停止,會關機,會在<時間>的時候關機,沒寫就是直接關機 - `shutdown -c` 取消關機的時間 - `poweroff` ==盡量不要使用== 不管記憶體的東西有沒有全部寫完,直接關機! - `sync` 把暫存記憶體寫入硬碟 - `reboot` 重新開機 (盡量不要使用) - `exit`或`logout` 關掉遠端連線 - 如果目前是在原 使用者的地方 - logout 其他使用者要先login 才能logout - exit 直接登出,terminal會關掉 :::info **為啥 shutdown -k 可以把訊息印到所有人螢幕上?** - 在 linux 裡面,所有東西都是檔案,包含 tty - 用 `write <username> pts/<number>`,寫東西到某人 terminal 上 (看使用者在用的 tty 是幾號用 `w`) - 其實也可以用 `echo hello > /dev/pts/<number>` 來把 hello 寫過去讓他顯示出來 ::: - 因為linux所有東西都是檔案,所以也可以去/dev/2,傳送指令給tty? e.g. `echo "lll" > 2` - 所有登入的資訊會寫在/dev? 【是這樣講嗎?】 - `w`: 顯示現在有幾個使用者在連線 ## 遠端登入連線 - 主機板、CPU、RAM、硬碟、網路卡,這部主機就能提供你所需要的網路服務了 - 如果你需要設定這部主機,該如何登入主機取得類似bash、shell的介面,進行操作/修改 - 伺服器管理者 - 透過遠端連線伺服器的服務 ### 主要類型 1. 文字介面 - 明文傳送(目前非常少用) - telnet - rsh - 密文傳送(以取代telnet, rsh) - **ssh** 2. 圖像化介面 Xdmcp , VNC,RDP 等較為常見 ### Telnet - 支援的使用者軟體比較多 - 使用明文來傳輸資料(未加密) - 可能會受到有心人士利用監聽軟體(e.g tcpdump )的監聽竊取 - 例子: 遠端連線進主機時,需要輸入帳號密碼,若沒有加密,帳號密碼都會被別人看光光 - 還有哪些明文傳送? - rsh 一個命令列介面的電腦程式 - ftp 檔案傳輸伺服器 ### SSH - **S**ecure **SH**ell server - 優點:安全、輕量 - 安裝openSSH(安裝SSH server) - `sudo apt install openssh-server` 針對ssh端安裝 - `sudo service ssh status` 確認ssh server狀態 - `sudo netstat -tnlp | grep ssh` 可以看到ssh是在 22 port 登入 - `ssh 127.0.0.1` 登入本機 - `ssh <userName>@localhost` 登入本機 - `exit` 退出此次連線 ECDFA 數位簽章的算法(橢圓曲線數位簽章) SHA256後面那串就是要連進去的公鑰 那個公鑰會記錄在家目錄底下的.ssh底下的 known_hosts檔 :::info > 示意圖![](https://i.imgur.com/eoiPDBV.png) - 公鑰記錄檔 : ~ /.ssh/known_hosts - 查看記錄檔: `cat known_hosts` ![](https://i.imgur.com/Rj3F35I.png) ::: - `ssh -p <port num> <user name>@localhost` :指定 server 的 port - `ssh -v <user name>@localhost`: 印出執行中發生的事 - `ssh -f <user name>@localhost 指令`: 只做一次指令就登出(結束連線) - `ssh -X <user name>@localhost` : Enables X11 forwarding (打開圖形化介面(大寫)) <!--偷嘴 CentOS 不安全 -->

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