設定 SSH 金鑰免密碼登入

  • 2025.04.17
  • SSH

Local Client: Windows (PowerShell)

生成 SSH 金鑰

 

ssh-keygen -t rsa -b 4096 -C "{comment(usually an email address)}"

啟用 ssh-agent

 

Set-Service -Name ssh-agent -StartupType Automatic
Start-Service ssh-agent

加入私鑰進 ssh-agent

 

ssh-add "$env:USERPROFILE\.ssh\id_rsa"

Remote Host: Ubuntu

複製公鑰至 Remote Host

 

scp ~/.ssh/id_rsa.pub user@remote-host:~/

# ssh 登入遠端:
ssh user@remote-host

mkdir -p ~/.ssh
chmod 700 ~/.ssh
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
rm ~/id_rsa.pub

測試 SSH 登入

 

ssh user@remote-host

(選用) 設定 .ssh/config 管理主機和金鑰

 

Host {HostName}
  HostName {IP/URL}
  User ...
  IdentityFile ~/.ssh/id_rsa
  Port 22

以後只需要:ssh {HostName}

Last Updated on 2025/04/17 by A1go

目錄
Bitnami