--- title: macOS ssh to win10 with ssh key tags: ssh description: macOS 免密碼登入 win10 ssh --- {%hackmd theme-dark %} # macOS ssh to win10 with ssh key 因為看好多篇結果一樣沒解決問題,所以寫一篇完整的出來希望能幫到後人 win10安裝openSSH步驟省略,安裝應該沒什麼坑 `C:\ProgramData\ssh\sshd_config` ``` //不要註解下面三行 PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication no //註解下面兩行 #Match Group administrators # AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys ``` 在macOS 透過 `ssh-keygen` 生成 key (都用預設值,不用password) `id_rsa`(私鑰,存於Mac),`id_rsa.pub`(公鑰,傳至Server) 並且透過`key-add -K ~/.ssh/id_rsa`加入macOS系統 將`id_rsa.pub`內容,複製到Server(以win10為例)。 在`C:\Users\{username}\.ssh`底下新增`authorized_keys`,並將`id_rsa.pub`內容貼上 完成後重啟 ``` net stop sshd net start sshd ``` or ``` Restart-Service sshd ``` ## VSCode RemoteSSH `config` ``` Host {設定檔名稱} HostName {serverIP} User {UserID} Port 22 IdentityFile /Users/{MacOSUserID}/.ssh/id_rsa ``` UserID(cmd輸入) ``` $env:USERNAME ``` serverIP(cmd輸入) ``` ipconfig | select-string ('(\s)+IPv4.+\s(?<IP>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(\s)*') -AllMatches | %{ $_.Matches } | % { $_.Groups["IP"]} | %{ $_.Value } ```