# SSH (Secure Shell) ### SSH, SCP, SFTP ### Port Forward, Tunneling </br> *YoMin Su* --- ## 基本問題 ---- ### 為什麼分享這個? ---- 因為大部分的同學在剛接觸到SSH時,就僅知道它可以用來遠端連線到目標得主機,但並不知道SSH有非常多神奇的功能,若沒有這些方便的功能,VSCode也無法整合的如此易於使用,因此,今天就要帶著各位領略一下這強大的工具。 ---- ### SSH 是能有多複雜? ---- 作為基本的連線工具,確實我們不用在意背後難以理解的密碼學或是金鑰交換方式;或是在目標主機支援的加密方式過老時,該怎麼在Client端開啟對方支援的方法;這些都是在遇到時,再去思考的問題,今天主要還是注重在SSH本身與額外延伸出來的各種用法。 ---- ### 那實際上會講到什麼? ---- - SSH - SCP - SFTP - TAR 主要為上述四項,不過這四項也夠今天的用例了(\`・ω・´) --- ## SSH ---- ### 最標準的 ```bash= # Command ssh <username>@<host> -p <port> # Example ssh jolly@192.168.64.2 -p 22 ``` ---- ![](https://hackmd.io/_uploads/ry27RoNEn.png) ---- ### 若加上指令呢? ```bash= # Command ssh <username>@<host> <execute command> # Example ssh jolly@192.168.64.2 ls -al ``` ---- ![](https://hackmd.io/_uploads/Byj90jNEh.png) ---- ### 通訊埠轉發(遠端 -> 本機) ```bash= # Command, l => local, r => remote ssh <username>@<host> -L <l port>:<r host>:<r port> # Example ssh jolly@192.168.64.2 -L 7080:127.0.0.1:80 ``` ---- ![](https://hackmd.io/_uploads/S12sxn4Vh.png) ---- ### 通訊埠轉發(本機 -> 遠端) ```bash= # Command, l => local, r => remote ssh <username>@<host> -R <r port>:<l host>:<l port> # Example ssh jolly@192.168.64.2 -R ``` ---- ![](https://hackmd.io/_uploads/SkGD424Vh.png) ---- ### 通訊埠轉發(動態) ```bash= # Command, l => local ssh <username>@<host> -D <l host>:<l port> # Example ssh jolly@192.168.64.2 -D 127.0.0.1:7081 ``` ---- 上述功能用途較為特別,會在系統上建立Socks通道,當瀏覽器或系統的Proxy設定完成後,請求就會通過該通道,直接送到遠端主機做代理請求,也就是說,功能類似於平時使用的VPN。 ---- ![](https://hackmd.io/_uploads/Hya2LnVNh.png) ---- ### 補充資料 - [SSH Command - Usage, Options, Configuration](https://www.ssh.com/academy/ssh/command) - [SSH Tunneling (Port Forwarding) 詳解](https://johnliu55.tw/ssh-tunnel.html) - [用 SSH Tunnel 來避免防火牆阻擋實作與反制](https://shazi.info/%E7%94%A8-ssh-tunnel-%E4%BE%86%E9%81%BF%E5%85%8D%E9%98%B2%E7%81%AB%E7%89%86%E9%98%BB%E6%93%8B%E5%AF%A6%E4%BD%9C%E8%88%87%E5%8F%8D%E5%88%B6/) --- ## SCP ---- 簡單的說,是Secure Copy的縮寫,只要是需要移動檔案,且目標主機有SSH服務,就可以透過SCP進行操作! ---- ### 送出去(本機 -> 遠端) ```bash= # Command, l => local, r => remote scp -P <port> <l path> <username>@<host>:<r path> # Example scp -P 22 ./test.js jolly@192.168.64.2:~/ ``` ---- ![](https://hackmd.io/_uploads/S18t52VVn.png) ---- ### 拖回來(遠端 -> 本機) ```bash= # Command, l => local, r => remote scp <username>@<host>:<r path> <l path> # Example scp jolly@192.168.64.2:~/ ``` ---- ![](https://hackmd.io/_uploads/BJ8UsnNEh.png) ---- ### 借個通道(遠端 -> 遠端) ```bash= # Command, r1 => Host 1, r2 => Host 2 scp <user>@<r1>:<r1 path> <user>@<r2>:<r2 path> # Example scp root@192.168.192.254:~/ToolScript/iommu.bash jolly@192.168.64.2:~/ ``` ---- ![](https://hackmd.io/_uploads/ry-mp3V4n.png) --- ## SFTP ---- 就跟以往使用FTP的用法差不多,只要有SSH服務,就能進行連線! :::warning 注意一下,SFTP與FTPS是兩個不同的協定喔,FTPS類似於HTTPS,都是幫原先的協定加上SSL/TLS層。 ::: ---- ### 連線 ```bash= # Command sftp <username>@<host> # Example sftp jolly@192.168.64.2 ``` ---- ![](https://hackmd.io/_uploads/HyxwA2EE2.png) ---- ### 操作指令 | Option | Description | | ------------- | -------------- | | ? | 有什麼可以用 | | ls | 列出遠端目錄 | | lls | 列出本機目錄 | | cd | 切換遠端目錄 | | lcd | 切換本機目錄 | | pwd | 顯示遠端路徑 | | lpwd | 顯示本機路徑 | ---- ### 操作指令II | Option | Description | | ------------- | -------------- | | get | 從遠端下載檔案 | | put | 從本機上傳檔案 | | quit/exit/bye | 退出SFTP | ---- ### SCP 與 SFTP 的差異? SCP主要注重在複製,需要先<span class=bluegreen>明確好遠端檔案的路徑</span>,但相對較快(因算法不驗證每個封包)。 SFTP提供目錄瀏覽,讓你決定好再進行操作,與FTP的用法相同,能管理目錄,也可以<span class=bluegreen>續傳(當連線因意外斷線時,很有用)</span>。 ---- ### 補充資料 - [SCP 比 SFTP 更安全嗎?](https://www.ipswitch.com/tw/blog/is-scp-more-secure-than-sftp) --- ## TAR ---- 打包工具 但今天主要是在說明跟SSH結合時可以怎麼用 ---- ### 神奇的Pipe用法 在目前的大多數作業系統中,都存在所謂的Pipe用法,也就是<span class=orange>將前一條指令的執行結果作為下一條的輸入</span>,接力執行,而今天的用法就是要使用這樣的特性去傳輸資料。 ---- ### 打包備份 ```bash= # 從本機送出去 tar zcvf - /var/www | ssh <username>@<host> "cat > ~/backup/www.tar.gz" # 遠端拖本機過來 ssh <username>@<host> tar zcvf - /var/www/ > ~/backup/www.tar.gz ``` ---- ### 同時打包與解包 ```bash= ssh <username>@<host> 'tar zcf - /some/dir' | tar zxf - ``` ---- ### 額外加碼:DD ### 備份整顆硬碟 ```bash= dd if=/dev/sdx | ssh <user>@<host> 'dd of=sdx.img' ssh <user>@<host> 'dd if=sdx.img' | dd of=/dev/sdx ``` --- # Thanks! <style> .orange { color: #FF8800; } .red { color: #FF0000; } .bluegreen { color: #00FFCC; } .gray { color: #272727; } .blue { color: #00E3E3 } </style>
{"metaMigratedAt":"2023-06-18T03:32:19.106Z","metaMigratedFrom":"YAML","title":"SSH (Secure Shell)","breaks":true,"contributors":"[{\"id\":\"86b6dc70-72ec-4014-84f0-bfb2a06c3dc4\",\"add\":4725,\"del\":145}]"}
    83 views