王詠平
    • 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
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ###### tags: `1121` `lsa` `ncnu` # SSH & Web Server - Book mode: https://hackmd.io/@ncnu-opensource/book [TOC] ## 進階延伸 * SSH Tunnel * 基於 SSH Protocol 所延伸的技術 * 將網路上的 A、B 兩個端點用 Tunnel 連接起來,形成一個隧道 * 基本過程: * A 點上的某個 Port X 所傳送的資料轉送至 B 點上的 Port Y * 又被稱 Port Forwarding * 好處: * 可突破防火牆限制 * 如防火牆有擋掉特定 port,沒有擋掉 22 port,可利用此種方式穿透此限制 * 將通訊內容加密避免洩漏 * 利用 SSH 加密特性來進行通訊 * Tunneling 中通常會出現以下三種角色: * Client * 任何你可以敲 ssh 指令來啟動 Port Forwarding 的機器 * SSH Server * 可以被 Client 用 SSH 連進去的機器 * Target Server * 某一台你想建立連線的機器,通常是為了對外開放這台機器上的服務 * Client 與 SSH Server 本身都可以是 Target Server,不是真的要有三台機器才可以進行Port Forwarding! ### Local Port Forwarding * 語法: * `ssh -L [bind_address:][port]:[host]:[host_port] [SSH Server]` * `[bind address:]` 不輸入預設為 `localhost` * 情境一:`FIXME: 帶個實際數字舉例` ![截圖 2023-11-09 下午6.18.10.png](https://hackmd.io/_uploads/HyJeMNqXa.png) * 防火牆後有一台 Server ,但防火牆設定不接受 8080 port 連入,不過接受22 port * 使用 tunneling 將 Client 中的某個port(圖中範例 port 9090),完成繞過防火牆限制 ![截圖 2023-11-09 下午6.22.29.png](https://hackmd.io/_uploads/ryGgmE9Qp.png) * `ssh -L 9090:localhost:8080 [username]@[server]` * Client * 你的電腦 * SSH Server * 防火牆後的Server * [username]@[server] * Target Server * 防火牆後的Server * localhost 8080 >`bind address` 沒輸入代表使用預設 localhost >如果要開放對外可以改成 `0.0.0.0` >`host` 的`localhost` 是只針對 `server`的 `localhost` * **情境二**: FIXME: 帶個實際數字舉例 ![截圖 2023-11-09 下午6.33.18.png](https://hackmd.io/_uploads/rkqOHN97p.png) * 前面情境為 Server 能連入的情況,那如果今天全線不足以進入要怎麼辦 * 如果防火牆後有一台你的機器且可用`ssh`連入即完成 ![截圖 2023-11-09 下午6.35.43.png](https://hackmd.io/_uploads/Sk3ZLEqQ6.png) * `ssh -L 9090:192.168.2.10:8080 [username]@[server]` * Client * 你的電腦 * SSH Server * 防火牆後的Server * [username]@[server] * Target Server * 防火牆後的Server ### Remote Port Forwarding * 語法 * `ssh -R [bind_address:][port]:[host]:[host_port] [username]@[server]` * **情境一**: ![截圖 2023-11-09 下午7.05.09.png](https://hackmd.io/_uploads/BJWepE9Qa.png) * 你的電腦上完成了一個服務,並架上 8080 port,但你的電腦沒有對外ip,僅有內部ip * 準備一台有Internet 的機器當作對外機器,即可使用Remote Forwarding 完成 * 對外機器開啟一 port客戶連入(圖片範例port 9090) * 對外機器藉由SSH通道將傳送到port 8080,客戶即可取得服務 ![截圖 2023-11-09 下午7.12.44.png](https://hackmd.io/_uploads/ByK30NcQT.png) * `ssh -R 0.0.0.0:9090:localhost:8080 [username]@[SSH Server]` * 因為要開放對外,所以設定`0.0.0.0` * localhost 是針對你的電腦 * Client * 你的電腦 * SSH Server * 對外機器 * Target Server * 你的電腦 :::warning * 基於安全考量,Remote Forwarding 的預設都只能夠bind在SSH Server的localhost上 * 所以必須要改變設定檔 * `GatewayPorts yes` ::: <!-- :::info | Alice | Bob | Chris | | -------- | -------- | -------- | | 想吃糖果 | 工具人 | 8080號門後花園裏面有糖果 | - Alice 想吃糖, 但是 Chris 8080號門沒開, 只好從 22號門進去找路. ```bash= ssh -L 9090:localhost:8080 chris@chris.家.地.址 ``` - Alice 上次偷晚糖果后, Chris 不開心, 22號門關起來了. 只好去找 Bob, 讓他幫 Alice 拿 Chris 的糖果. ```bash= - ssh -L 9090:Chris.家.地.址:8080 bob@bob.家.地.址 ``` - ::: --> - 情境二 - ![image.png](https://hackmd.io/_uploads/rJds1BcXa.png) - 與情境一不同,服務另外架在其他機器上 - 一樣可以使用Remote Forwarding ![截圖 2023-11-09 下午7.25.19.png](https://hackmd.io/_uploads/r1Xh-Sqm6.png) * `ssh -R 0.0.0.0:9090:192.168.1.100:8080 [username]@[SSH Server]` * `192.168.1.100` 為內部服務ip(相對於你的電腦) * Client * 你的電腦 * SSH Server * 對外機器 * [username]@[server] * Target Server * 內部服務 * `192.168.1.100:8080` ### Dynemic Port Forwarding * 語法 * `ssh -D [bind_address]:[port] [username]@[SSH Server]` * 情境 ![截圖 2023-11-09 下午7.29.59.png](https://hackmd.io/_uploads/r1STzr976.png) * 只要有一台位於內網且具有外部IP的機器,你就可以利用這個方法建立一個SOCKS代理伺服器,讓你能夠從外面連回內網裡的所有服務 ![image.png](https://hackmd.io/_uploads/B1T2PNcXa.png) :::info ftp 10.107.21.201 anonymous 學號 cd practice get File.txt bye ::: :::info ### 甚麼是Server - 提出要求的時候,以任何形式提供回應的,就可以稱為service - 用來提供這些Service的軟體、電腦,就可以稱為Server - 種類有很多 - database server、file server、FTP server、DNS server、**Web Server**... - Server 的功能很多,些我們每天都會使用 - DNS server - 可以透過紀錄Domain Name,讓我們不用記一串IP也能連到網站 ::: ## Web Server ### 什麼是Web Server? * 提供Web 相關服務的Server * 希望網頁可以被世界各地任何上網路的人在瀏覽器上讀出來 >在電腦主機上執行 Web server,他可以幫主機開一個80 port(預設),此時別人就能透過該電腦主機的ip與他建立連線 >![截圖 2023-11-09 下午7.36.36.png](https://hackmd.io/_uploads/H1xLErqQT.png) * 存放網路伺服器軟體、還有網站檔案的主機 * HTML、CSS、JavaScript、圖片、影片 >![截圖 2023-11-09 下午7.38.32.png](https://hackmd.io/_uploads/HyE6VH97a.png) ### HTTP * 全名是 Hypertext Transfer Protocol,中文讀作「超文本傳輸協定」 * Client 和 Server 就是透過HTTP協定進行溝通 * Application layer * 預設 port:80 port >![截圖 2023-11-09 下午7.44.46.png](https://hackmd.io/_uploads/rkoEUBq76.png) >通常Client端指的是Browser(瀏覽器) >SSH 已經有提到 - IP +數字編號 - `127.0.0.1:80` - 用於區分同一個IP地址下的不同服務 `/etc/services` - 記錄各式各樣服務會佔用的port - `cat/etc/services` - service name,port number,Protocol,aliases(別名) - ![image.png](https://hackmd.io/_uploads/rkpXOH5Xp.png) - SMTP service uses TCP on port 25 and also goes by the alias"mail." - 查看現在開啟的port - netstat-atupl - ![image.png](https://hackmd.io/_uploads/BkZFuS9mp.png) - `Proto`:封包 or 通訊協定 - `Local Address`:本地的IP與port,如果`/etc/services`存在對應服務名稱,就以服務名稱顯示 - `Foreign Address`:監聽哪一個IP與Port - `state`:狀態列 - `LISTEN`:表示該port已經在監聽 網路服務 - `ESTABLISHED`: 已建立連線 - `TIME_WAIT`:該連線目前是等待狀態,隨時都有可能斷線 :::info 參數說明: `-a`:顯示出目前所有的網路連線狀 `-t`:顯示tcp封包 `-u`:顯示udp封包 `-p`:顯示此連線的PID `-l`:顯示LISTEN的內容 `-n`:預設情況中,顯示出的host會依host name來顯現,若為n則可以使port與host都以數字顯示 >FIXME:`netstak -atupl` &`netstat -tupl`差異 ::: >1.試試`-n` >2.打開firefox,輸入`netstat -ntupl` >3.kill pid ### 常見的Web Server簡介 - Lighttpd(發音為"lighty") - ![image.png](https://hackmd.io/_uploads/ryuO3rqX6.png) - 佔用系統資源少,運作輕量 - 低記憶體佔用,低CPU負載,速度最佳化 - 支援CGI,FastCGI,SCGI,允許使用任何程式語言撰寫Web應用程式 - 在提供靜態內容方面高效,但動態內容可能會消耗比較多資源 - 靜態內容:預先創建的文件,例如HTML,CSS,和JavaScript - 動態內容:像是php,需要的client傳送才會有內容 -適合提**靜態內容**或系統資源比較守限 - **Nginx** - ![image](https://hackmd.io/_uploads/BkQgJ1m46.png) - 佔有記憶體少,穩定性高,可以同時 承受很大的流量 - 採用模組化設計,有大量的模組庫和第三方模組庫可供選擇 - 設計的目標是為了超越Apache,並且在2019成功超越Apache成為市場佔有率最高的 :::danger 本次課程安裝順序依照Lighttpd->Apache2->Nginx ::: ### 安裝與介紹 :::info `sudo apt install lighttpd` 安裝lighttpd `sudo service lighttpd status` 檢查狀態 `sudo service lighttpd start` 開服務服務 `sudo service lighttpd reload` 不停止服務,重載設定檔 `sudo service lighttpd restart`停止服務重新開啟 `sudo netstat -ntupl` ::查看port號 `127.0.0.1:[port_num]` 成功開啟 `cd /etc/lighttpd/`開啟設定檔位置 `/etc/lighttpd/conf-available`可用但還沒啟用 `/etc/lighttpd/conf-enabled`放到enable才可以啟用 `sudo ln -s /etc/lighttpd/conf-available/<modules>.conf /etc/lighttpd/conf-enabled/` 把conf檔移到enabled `sudo service lighttpd reload `:要reload完才可以用 `sudo lighttpd-enable-mod <modules>`另外一種將conf檔**啟**用的方法 `sudo lighttpd-disable-mod <modules>`另外一種將conf檔**停**用的方法 !!檔案紅色是代表沒有成功!! ::: ![image.png](https://hackmd.io/_uploads/rkZG8rq7p.png) `cd /var/www/html` `sudo vim index.html` `sudo service lighttpd restart` `sudo rm index.html` `sudo vim test.html` `sudo vim lighttpd.conf` 進入lighttpt.conf改port號 ![image.png](https://hackmd.io/_uploads/HJ21pS57T.png) [其他屆的](https://hackmd.io/@ncnu-opensource/book/https%3A%2F%2Fhackmd.io%2F6SlrzT4jT8mfeTqvq42BiA%3Fview)

    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