
我希望在 Raspberry Pi 4 上安裝新的 Ubuntu Server 20.04 時自動設定 SSH 金鑰 - 主要作為學習練習。我正在嘗試使用以下ssh_import_id
選項雲端初始化從我的 GitHub 帳戶中提取我的 SSH 公鑰並將其新增至~/.ssh/authorized_keys
使用者pi
。
我似乎無法ssh_import_id
啟動配置。~/.ssh
pi
/var/logs/cloud-init.log
/var/log/cloud-init-output.log
user-data
SD卡根目錄下我的目前內容:
- name: pi
groups: [sudo]
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_import_id: # import public key from github
- gh:my_cool_github_account
lock_passwd: true # disable password login
如果我使用 手動輸入公鑰ssh_authorized_keys
,一切都會正常工作,這將適合我的工作流程,但我希望獲得 GitHub 事物設置,因為它很時髦。
我的理解cloud-init
肯定有點基礎,所以我可能錯過了一些東西。我透過執行cloud-init clean
then 來觸發更改cloud-init init
,感覺運作良好,因為它正在重建使用者/常規位元/根 ssh 金鑰和指紋。
我想必也在使用NoCloud
資料來源。
誰能幫我弄清楚我做錯了什麼?
答案1
這只是您的用戶資料的一小段嗎?如果沒有,您還需要#cloud-config
標頭和users:
.像這樣的東西應該有效:
#cloud-config
users:
- name: pi
groups: [sudo]
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_import_id:
- gh:torvalds
lock_passwd: true
我剛剛測試並找到了密鑰/home/pi/.ssh/authorized_keys
。您確定您嘗試匯入的 github 使用者的金鑰位於https://github.com/settings/keys
?
/var/log/cloud-init.log
包含:
2021-08-09 13:56:24,302 - helpers.py[DEBUG]: Running config-ssh-import-id using lock (<FileLock using file '/var/lib/cloud/instances/me/sem/config_ssh_import_id'>)
2021-08-09 13:56:24,302 - cc_ssh_import_id.py[DEBUG]: Importing SSH ids for user pi.
2021-08-09 13:56:24,302 - subp.py[DEBUG]: Running command ['sudo', '-Hu', 'pi', 'ssh-import-id', 'gh:torvalds'] with allowed return codes [0] (shell=False, capture=False)
答案2
根據 cloud-init 的文檔ssh_import_id
模組僅適用於 Ubuntu 和 Debian,但根據 Ubuntu/Debian 的風格和版本,ssh-import-id
cloud-initssh_import_id
模組在幕後實際檢索金鑰所使用的二進位檔案可能會安裝,也可能不會安裝在你的發行版上。
例如,https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-amd64.qcow2沒有附帶它,因此 cloud-init 無法從 Github 導入我的金鑰。就我而言,我command not found
在 中看到了一個明顯的錯誤/var/log/cloud-init-output.log
。
檢查ssh-import-id
初始化的系統中是否有命令。如果它不存在,那麼您可能想嘗試將此區塊添加到您的user-data
:
packages:
- ssh-import-id
答案3
嘗試:
users:
- name: pi
ssh_authorized_keys:
- ssh-rsa <your key here>
這應該把你的密鑰放在用戶.ssh/authorized_keys
的文件中pi