ChatGPT 說:
ChatGPT
如果你想使用另一個帳號(如 sherry)登入 Raspberry Pi,並且希望使用 SSH 金鑰無密碼登入,你不需要重新生成新的公鑰,只需將現有的公鑰添加到新帳號的 ~/.ssh/authorized_keys 檔案中即可。以下是具體的操作步驟:
方法一:使用 ssh-copy-id
確保你已經在 Raspberry Pi 上創建了新帳號 sherry 並且該帳號有 ~/.ssh 目錄。你可以通過以下指令創建帳號(如果還沒有創建):
bash
複製程式碼
sudo adduser sherry
然後根據提示設置密碼。
確保該帳號可以使用 SSH 登入並且有 ~/.ssh 目錄:
bash
複製程式碼
sudo su - sherry
mkdir -p ~/.ssh
chmod 700 ~/.ssh
在 iMac 上使用 ssh-copy-id 將公鑰複製到新帳號 sherry 的 ~/.ssh/authorized_keys 中:
bash
複製程式碼
ssh-copy-id -i ~/.ssh/id_rsa.pub sherry@raspberrypi.local
這個指令會將當前 iMac 上的 id_rsa.pub 公鑰複製到 sherry 帳號的 ~/.ssh/authorized_keys 檔案中。
輸入 sherry 帳號的密碼以完成複製。
方法二:手動複製公鑰
在 iMac 上查看公鑰的內容:
bash
複製程式碼
cat ~/.ssh/id_rsa.pub
登入到 Raspberry Pi,並切換到 sherry 帳號:
bash
複製程式碼
ssh pi@raspberrypi.local
sudo su - sherry
確保 sherry 帳號下的 ~/.ssh 目錄存在:
bash
複製程式碼
mkdir -p ~/.ssh
chmod 700 ~/.ssh
編輯或創建 authorized_keys 檔案:
bash
複製程式碼
nano ~/.ssh/authorized_keys
將 iMac 上顯示的公鑰內容貼入 authorized_keys 檔案中,然後儲存並退出(Ctrl + X,然後按 Y,再按 Enter)。
設置 authorized_keys 檔案的權限:
bash
複製程式碼
chmod 600 ~/.ssh/authorized_keys
在 iMac 上執行以下指令,測試用 sherry 帳號無密碼連線:
bash
複製程式碼
ssh sherry@raspberrypi.local
如果配置正確,應該能夠直接以 sherry 帳號連線到 Raspberry Pi 而不需要輸入密碼。
不需要重新生成公鑰:你可以使用已經存在的公鑰,將它添加到新的帳號的 ~/.ssh/authorized_keys 中。
每個帳號都有自己獨立的 authorized_keys 檔案:每個帳號的 ~/.ssh/authorized_keys 檔案可以包含多個公鑰,允許不同設備和使用者連線。
安全性管理:將公鑰複製到不同帳號的 authorized_keys 檔案中,可以靈活管理不同帳號的訪問權限。