# [Mac] Mac ssh 免密碼連線至 Windows ##### 2024-08-27 post by sean. :::info :bulb: My Setting MacOS Ventura 13.6 Windows 11 家用版 23H2 ::: ## 📦 Win 安裝 OpenSSH 在 windows 中以管理員打開 PowerShell. 執行命令安裝 OpenSSH Client ``` Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0 ``` 安裝 OpenSSH Server ``` Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 ``` 安裝完成後執行命令檢查是否安裝成功 ``` Get-WindowsCapability -Online | ? Name -like 'OpenSSH*' ``` 安裝成功顯示 ![image_2024_08_27T02_06_28_663Z](https://hackmd.io/_uploads/SyLCN3qjR.png) :::success 安裝完成可能需要重啟電腦. ::: #### 開啟 sshd 服務. PowerShell 執行命令開啟 sshd 服務 ``` Start-Service sshd Set-Service -Name sshd -StartupType 'Automatic' ``` 檢查防火牆是否允許 OpenSSH 服務 ``` Get-NetFirewallRule -Name *ssh* ``` 若 Enabled 為 True, Action 為 Allow 表示允許通過 ![image_2024_08_27T02_17_58_374Z](https://hackmd.io/_uploads/HJ2ed29sA.png) ## 📦 準備連線階段 取得 windows 使用者名稱 ``` $env:USERNAME ``` 取得 windows IP Address(IPv4位置) ``` ipconfig ``` #### 在 mac 上升成 SSH 密鑰 在 mac 上開啟 terminal 執行以下命令來生成一個新的 SSH 密鑰 ``` ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ``` * -t rsa:指定密鑰類型為 RSA。 * -b 4096:密鑰長度為 4096 位元。 * -C "your_email@example.com":添加一個標籤(通常是你的電子郵件地址)。 執行命令時,系統會問你是否要指定密鑰文件的保存路徑(默認位於 ~/.ssh/id_rsa),以及是否要設置密碼。你可以按 Enter 鍵選擇預設路徑和不設置密碼(無密碼的話每次登錄都不會提示密碼,視安全需求決定)。 :::info 請勿使用 sudo 執行 sudo 產生密鑰的位址後續無法進行操作 ::: #### 將公鑰複製到遠端 Windows 伺服器 windows 以管理員身份開啟 PowerShell, 執行: ``` notepad $env:ProgramData\ssh\sshd_config ``` 移動至最後,將以下兩行註解掉,保存 ``` Match Group administrators AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys ``` 重新啟動 sshd 服務 ``` Restart-Service sshd ``` 在 mac 上開啟 id_rsa.pub 或執行命令複製 mac 公鑰 ``` cat ~/.ssh/id_rsa.pub ``` 在 windows 開啟 authorized_keys 文件並將 mac 上的公鑰寫入後存檔 ``` notepad $env:USERPROFILE\.ssh\authorized_keys ``` #### 在 mac 測試連線 ``` ssh username@ip ``` 若成功即可進入遠端 windows termianal ### 🔗 參考資料 #### [配置mac与windows之间的ssh互连](https://blog.csdn.net/raelum/article/details/133108158)