# 使用 Tailscale + SSH 遠端連線到 Windows 電腦 ## 1. 安裝 Tailscale > 家用電腦通常位於路由器後方,使用 `ipconfig` 指令取得的為 **內部 IP 位址**(例如 `192.168.x.x`),無法直接從外部存取。 > 若無法進入路由器設定頁面開啟 **Port Forwarding**,就需要透過其他方式建立點對點連線。 [Tailscale](https://tailscale.com/) 是一款 **zero-config VPN**,能讓同一帳號登入的所有設備自動連成虛擬內網。 登入 [Tailscale 管理頁面](https://login.tailscale.com/admin/machines) 後,可以在 **Machines** 頁面查看已登入的裝置,並新增新的機器。 在右上角的 **Add device** 選擇 **Client device**,點擊產生的連結即可將該電腦加入虛擬內網。  > Note: 即使要讓這台電腦作為 SSH Server,也是一樣新增為「Client」。 每台機器都會被分配一個固定的 **Tailscale IP**(例如 `100.x.x.x`),可直接用於 SSH 連線。 安裝完成後,在該電腦上執行: ```bash tailscale ip ``` 即可取得該裝置的虛擬 IP。 ## 2. 安裝 OpenSSH.Server 在 Windows 上安裝 SSH 伺服器,請以系統管理員身份開啟 PowerShell,執行以下命令: ```powershell Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 Start-Service sshd Set-Service -Name sshd -StartupType Automatic ``` 確認 SSH Server 是否已啟動: ```powershell Test-NetConnection -ComputerName 127.0.0.1 -Port 22 ``` 若輸出結果包含: ``` TcpTestSucceeded : True ``` 代表 SSH Server 已成功啟動並可接受連線。 ## 3. 建立可登入的本地使用者帳號 若目前登入 Windows 的帳號是 **綁定 Microsoft 帳號**,該帳號沒有本地密碼, 也無法使用 `net user` 修改密碼。此時需建立新的本地帳號以供 SSH 使用。 以系統管理員身份開啟 PowerShell,執行: ```powershell # Create a local user account with password net user <username> <P@ssw0rd> /add # Add the user to administrators group net localgroup administrators <username> /add ``` 建立完成後,即可使用該帳號透過 SSH 登入。 ## 4. SSH 連線 在另一台電腦(macOS / Linux / 其他 Windows)上,同樣安裝並登入 Tailscale。 安裝完成後執行: ```bash ssh <username>@<tailscale-ip> ``` 若設定正確,即可遠端登入 Windows 的 PowerShell 環境。 ## 5. 修改預設 Shell SSH 登入後預設會進入 PowerShell,若希望改為其他 shell(例如 Git Bash),可修改 SSH Server 設定。 ### (1) 找到安裝路徑 在 PowerShell 中輸入: ```powershell where bash ``` 常見結果如下: ``` C:\Program Files\Git\bin\bash.exe ``` ### (2) 修改 SSH 設定檔 以系統管理員身分開啟設定檔: ``` C:\ProgramData\ssh\sshd_config ``` 在檔案末尾新增: ``` ForceCommand "C:\\Program Files\\Git\\bin\\bash.exe" ``` 儲存後重新啟動 SSH 服務: ```powershell Restart-Service sshd ``` > Note: 若使用 VS Code Remote SSH 且將 Git Bash 設為預設 shell,可能會導致啟動失敗。 > 將預設 shell 改回 PowerShell 或 cmd 可解決此問題。
×
Sign in
Email
Password
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