李亞軒
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ###### tags: `1121` `lsa` `ncnu` # SSH - Book mode: https://hackmd.io/@ncnu-opensource/book [TOC] :::info ## Port - 通訊埠=>埠號 - 在系統中扮演一個IP地址下的不同服務,每個服務使用的Port皆不同 像是學校辦公室都有不同的處室在幫學生處理校務 ### 為什麼需要port? - 1.區分不同的服務 - 當封包傳回來,就能根據port來判斷這些資料應該傳送到哪個服務 - 2.同時運行多個服務 - 由於每個服務都有自己的port,能夠在同一時間傳送資料,而不會送錯 - 本來要傳送給telegram的封包,結果被送到line ### port number - 範圍:從0~65535 - 16bits表示,共有65535個port(2^16) - ![image.png](https://hackmd.io/_uploads/HytENWwmT.png) - 常用的port會記錄在`/etc/service` Demo ### Well-Known Ports - 0 - 保留port,平常不會啟用 - 1 ~ 1023 需超級使用者權限才能佔用這些port 範圍中的服務都是一些比較古老,對系統重要的服務 ![image.png](https://hackmd.io/_uploads/B1e5B-PX6.png) - 由IANA進行管理分配 ### Registered Ports - 1024~49151 - 公司或組織可以向IANA申請和註冊這範圍內的port 註冊的好處:讓大家知道這個port有人用了,而且這個服務是使用這個port - 不需使用超級使用者權限 - 特定的服務 - MySQL database service (also for MariaDB):3306 ### Private/Dynamic Ports - 49152~65535 - 網路中的這些port未向IANA註冊 - 可以在local或由應用程式動態使用 >雖然Port號劃分成以上這三種,但使用者仍然可以自行去改變服務中的Port號 也就是說Registered Ports的服務可以改成Well-Known Ports,只是不推薦 不推薦的原因:1.怕別人找不到地方連接 2.避免多服務衝突 ### Appendix - IANA - Internet Assigned Numbers Authority ::: > source - Secure SHell Protocol - 解決Telnet明文傳輸的問題,更安全 >美國中央情報局已經開發出能夠在Windows或Linux作業系統中竊取SSH對談的工具。 >BothanSpy_REF - port號預設22 - 加密式連線 > 目前在SSH使用上,主要是利用RSA/DSA/Diffie-Hellman等機制 :::info ## Appendix - 加密式連線 - 將人看得懂的原始資料,經過演算法加密,變成人看不懂的亂碼後進行傳輸,而當接收端收到資訊想查閱時再進行解密。 > 情境例子:我為了不想讓周遭的人聽到我跟朋友的交談內容,我把我講的話翻譯成獨創語言,之後說給聽得懂獨創語言的朋友聽 ### 常見加密方式 > 所以對稱,非對稱各自優缺差別在哪裡 #### 對稱式加密 - 接收端與發送端皆有相同的密鑰,用來做加密 ![image.png](https://hackmd.io/_uploads/rJ2_nbq76.png) REF >REF_AES >REF_DES #### 非對稱式加密 - 一位使用者會有兩隻金鑰 - 公開金匙(Public Key):可給指定對象 - 私密金鑰(Private Key):自己保留用 >簡稱公鑰,私鑰 - 利用其中一隻進行加密,則一定要用另外一隻進行解密 - 公鑰加密私鑰解密 - 私鑰加密公鑰解密 > 兩模式皆可 > 就像兩人持有不同的翻譯機 > 我先拿了中翻英的系統加密,但一定要拿英翻中的系統才能解密 > 但公鑰是拿來給別人用的,所以私鑰加密公鑰解密這件事不會拿來加密訊息 > 加密數位簽章 - ![image.png](https://hackmd.io/_uploads/ByUAi1WX6.png) >Hash雜湊函數 >簡單來說就是加上了寄件者的簽章,而將Hash過的訊息當做簽章 - 到收件方除了利用自己的私鑰解密,也要用寄件者的公鑰進行簽章確認 - 就像是針對資料加密後再上一層指紋鎖 - 流程 - ![image.png](https://hackmd.io/_uploads/rJ24h1bmT.png) - 兩個雜湊數值相同即可證明是寄件人本人傳送 > REF_流程 REF_加密 ::: 連線 - 以client-server model進行傳輸 :::success ## 續-基礎操作 ### 登入與登出 - 登入本機 - `ssh localhost` - `ssh 127.0.0.1` - `ssh [username]@127.0.0.1` - `127.0.0.1` == `localhost` - IP中`127.0.0.1/8`被設定為保留地址,所有像這個IP發送的封包都會轉送到本機 - 而localhost被作為頂級域名, 不得有任何網域註冊此名稱 - 登入遠端 - `ssh [username]@[user_ip]` - 不指定使用者則以目前狀態的使用者進行遠端登入 - 登出 - `ctrl + d` - `exit` - `logut` ### 常用操作參數 - `ssh-p [port num] [user]@[ip adress]`指定server的port號,如port號被更改就需要利用此方式登入 - `ssh [username]@[user_ip] 'cmd'` :執行單行指令(執行完就登出) - ![image.png](https://hackmd.io/_uploads/H1rhZxW76.png) - `ssh -v [username]@[user_ip] `debug 偵錯 `ssh -vv [username]@[user_ip] ` `ssh -vvv [username]@[user_ip] ` v越多越詳細喔 - `ssh -A`允許Forward agent #### 後面會提到 - `ssh -x`允許遠端打開圖形化界面視窗 - `ssh -o`根據ssh_config做單次的設定 - `ssh -o ConnectTimeout-3 [user]@[ip adress]`等待時間如果超過三秒鐘即斷線 - `man ssh_config` 其他 -o 指令的詳細 ::: ![image.png](https://hackmd.io/_uploads/SyDUQgWX6.png) :::info `ssh-keygen -f "~/.ssh/known_hosts" -R "[ip_address]"` ![image.png](https://hackmd.io/_uploads/B1qTQxbma.png) ![image.png](https://hackmd.io/_uploads/H1YuPxbQT.png) ::: ### 免密登入操作 - 免密登入/憑證登入 - 優點:更安全,更方便 - 無需輸入密碼,就像指紋識別或臉部識別不需要密碼即可登入。 - 將client的公鑰放置於server中 ![image.png](https://hackmd.io/_uploads/Hk1GulW7a.png) ) :::success ### 免密登入 #### 產生公私鑰 - `ssh-keygen` - 預設使用rsa加密協議 - 可更換成 ed25519 加密,安全性較高,但舊版本伺服器無法使用。 - `-t`選擇加密協議(無輸入則使用預設),`-b`可選擇密碼長度 (bits) - `ssh-keygen -t rsa` - `rsa` 預設 - 相容性最好,支援稍舊的版本 - 密碼長度可選擇 `1,024` ~`16,384` bits,預設為 2048 bits - `2048`bits 已足夠安全,但如需提升安全性可選擇更長的key,如:`3,072` 或`4,096` bits - `ed25519` 較推薦使用 - 較新,相容性差,版本支援少 - 安全性最好 - 長度固定,無法改變 - `ecdsa` - 有 `256`, `384`, `521` bits 三種 key 長度可以選擇,無法選擇其他的使用 - 長度相比 RSA 較小 - 理論上較複雜難理解,實作困難 - **產生密鑰預設路徑** `~/.ssh` - `id_rsa`**私鑰** - `id_rsa.pub`**公鑰** - ![image](https://hackmd.io/_uploads/ryR1LAM4p.png) - **請注意**公私鑰權限問題 - **`.ssh/`資料夾擁有者可讀可寫可執行** - **公鑰的權限:擁有者可讀可寫,其他人與群組使用者可讀** - **其餘檔案:擁有者可讀可寫** > 抽點Sticky bit跟chmod ### 複製公鑰 - 方法一 - 使用虛擬機直接複製過去 - `~/.ssh/authorized_keys`複製到此處(如果沒這個資料夾跟檔案請自行創建) - 方法二 - `ssh-copy-id ~/.ssh/id_rsa.pub<usr>@ip` - 方法三 - ssh遠端登入後複製 ::: ### ssh 相關設定檔案 - 設定檔中 `#` 為註解,且註解後通常為藍字 - 註解的內容為預設內容 - ssh 相關設定檔案 `/etc/ssh/` - `FIXME`:proxycommand去看,不一定要講 - ForawardAgent驗證代理 - 讓本地SSH key可由遠端server轉送 - PasswordAuthentication - 允許/不允許client使用密碼登入ssh server - CheckHostIP - 確認已連線過的主機是否有IP不同的問題。發現就提醒 - ConnectTimeout - 連線超過指定秒數就斷線 - Port - 預設使用22port進行連線 ### Server - `/etc/ssh/sshd_config` - 基礎設定 - ![image](https://hackmd.io/_uploads/S1Zzc0zV6.png) - port選擇使用port號(預設22) - AddressFamily允許使用的ip類型 - inet: ipv4 - inet6: ipv6 - any: 預設皆允許 - ![image](https://hackmd.io/_uploads/rkI05PhNp.png) - LoginGraceTime - 連線完成後,允許client停留在輸入密碼畫面多久 - PermitRootLogin - 是否允許使用root登入(預設為禁止使用者密碼登入) - PubkeyAuthentication - 是否允許使用Public Key登入(預設為yes) - PasswordAuthentication - 是否允許輸入空密碼登入(預設為no) - **連線狀態維持** - ClientAlivelnterval - Server端每隔幾秒傳訊息給client端,client端收到訊息會回傳以維持連線。 - ClientAliveCountMax - Client端幾次沒回應,Server就進行斷線 > 假設Interval設定30,CountMax設定20,代表Client10分鐘未回應就會斷線 - 針對單獨client設定 - Match User anoncvs - 如果你只想針對某用戶進行特殊設定,可以使用這個功能 - ![image](https://hackmd.io/_uploads/SJmTaP3Va.png) - 針對client端叫ray的人給他這樣設定 :::info #### **Appendix - SSH config設定** - 觀察檔案會有一行 - ![image](https://hackmd.io/_uploads/H1kURvhET.png) - 在/etc/ssh/中會發現有一個目錄sshd_config.d - sshd_config被改壞 - ssh在運作時更改設定很可能會出現問題,所以建議以關閉狀態改變設定或者是在目錄新增.conf檔去做設定 > Demo ::: Forward Agent ![image](https://hackmd.io/_uploads/r10tG_nEa.png) - 範例狀況:client端是員工筆電,ServerA與ServerB為公司Server - ServerA無法使用金鑰登入ServerB,密碼驗證也被關閉 - ServerB設定不允許任何外部IP進入 - ServerB擁有Client的公鑰 >實列:ServerA為網站Server,ServerB為公司用機 >網站要做部置,要從ServerB拿資料,但員工遠端工作在家 - 使用Agent Forwarding可解決 - client端可以使用金鑰登入ServerA與ServerB,但因為ServerB不允許外部進入 - clinet端登入ServerA時將SSH key一起轉給A,A就可以利用client的key登入B ![image.png](https://hackmd.io/_uploads/SJn61Mbma.png) ![image.png](https://hackmd.io/_uploads/SyOCJfWXT.png) ![image.png](https://hackmd.io/_uploads/r1H31zZ7p.png) ![image.png](https://hackmd.io/_uploads/SJAZxfW7a.png) ![image.png](https://hackmd.io/_uploads/r1ymlfZ7a.png) ![image.png](https://hackmd.io/_uploads/HJCaxGbQp.png) ![image.png](https://hackmd.io/_uploads/BJRWWfZQp.png) ![image.png](https://hackmd.io/_uploads/BkV5Qz-mp.png) ![image.png](https://hackmd.io/_uploads/HJZhEfWXT.png) ![image.png](https://hackmd.io/_uploads/rkOMBMWXa.png) ![image.png](https://hackmd.io/_uploads/BkNrHf-Q6.png) ![image.png](https://hackmd.io/_uploads/B1tOHfWX6.png) ![image.png](https://hackmd.io/_uploads/B1h4wzb76.png) ![2023-11-21 21 37 43](https://hackmd.io/_uploads/BJ-TX4qNa.png) :::success #### 自動刪除來源檔案 - rsync -avh --remove-source-files myfolder/bacup/ - 就是剪下貼上 #### 只更新既有檔案 - 只針對已經複製過的檔案 - rsymcrsync -avh --existing myfolder/bacup/ ### rsync Server - rsync Server使用873port - 開啟/etc/rsyncd.conf - ![image](https://hackmd.io/_uploads/rkaUVO2Va.png) ::: ## 助教補充 ### rsync 設定 ```conf= motd file = /etc/rsyncd.motd log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock [test] comment = test path = ~/fileA/ read only = yes ``` ### SFTP 設定 ```conf= Match Group sftp ForceCommand internal-sftp ChrootDirectory /var/sftp/test PasswordAuthentication yes ```

    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