結論先說
- 優先生成
ed25519密鑰:體積小、握手快,macOS 原生 OpenSSH 開箱支持。 - 在
/etc/ssh/sshd_config.d/寫入加固片段:同步設PasswordAuthentication no、KbdInteractiveAuthentication no與PermitRootLogin no。 - 驗證免密後再禁密碼:新開窗測通公鑰登入並跑
sudo sshd -t,再重啟com.openssh.sshd。 - 開啟
ControlMaster與ServerAliveInterval:二次建連毫秒響應並防止 NAT 斷連。 - 掛機跑 Claude Code 需配穩健額度:長時重構可參考 Claude Max 5x/20x 充值指南 與 Claude Max 5x iOS 自助充值。

圖:本站根據 OpenSSH sshd_config 手冊 整理的示意圖
本文根據 Apple 支持文檔(Allow a remote computer to access your Mac)與 OpenBSDsshd_config手冊整理,資料核對於 2026-12-03。配圖為本站整理的示意圖,不是介面截圖。本站(bigolab.com)由益陽市赫山區必高科技有限公司獨立運營,是獨立第三方服務商,不是 Apple / Anthropic 官方網站。
適用於誰
- 把 Mac mini 設為無頭伺服器後,想關閉口令爆破、改用密鑰登入的開發者。
- 常用 SSH 跑 Claude Code 或同步代碼,常遇重連輸密碼或斷連的工程師。
- 需管理多台主機,想用
~/.ssh/config別名一鍵免密切換的用戶。
一、macOS 服務端 SSH 加固關鍵指令與誤區
在 System Settings > General > Sharing > Remote Login 開啟後,系統預設兼顧口令與公鑰認證,且 PAM 會走鍵盤交互提示,單改 PasswordAuthentication 並不能徹底禁密碼:
| 配置指令 | 預設狀態 | 推薦加固值 | 常見誤區與安全影響 |
|---|---|---|---|
PasswordAuthentication | yes | no | 僅關此項時 PAM 仍可能彈出密碼輸入 |
KbdInteractiveAuthentication | yes | no | macOS 上必須同步關閉才能徹底禁密碼 |
PermitRootLogin | 受限或允許 | no | 禁用 root 直接遠程登入,保留審計軌跡 |
PubkeyAuthentication | yes | yes | 確保公鑰通道可用再關閉口令登入 |
AllowUsers | 依系統列表 | 僅列所需帳戶 | 配合介面 Only these users 收斂範圍 |
二、密鑰部署、服務端加固與 ~/.ssh/config 實操
先在控制端筆記本生成密鑰並推入 Mac mini,新開窗口驗證免密登入無誤後,再關閉服務端密碼認證:
# 1. 控制端生成 Ed25519 密鑰並推送公鑰
ssh-keygen -t ed25519 -a 64 -f ~/.ssh/id_ed25519_macmini -C "workstation-to-macmini"
ssh-copy-id -i ~/.ssh/id_ed25519_macmini.pub [email protected]
# 2. 控制端寫入 ~/.ssh/config 別名、連接複用與心跳保活
cat <<'EOF' >> ~/.ssh/config
Host macmini-lan
HostName 192.168.10.20
User devbot
IdentityFile ~/.ssh/id_ed25519_macmini
IdentitiesOnly yes
AddKeysToAgent yes
UseKeychain yes
ControlMaster auto
ControlPath ~/.ssh/cm-%r@%h:%p
ControlPersist 10m
ServerAliveInterval 30
ServerAliveCountMax 3
EOF
chmod 600 ~/.ssh/config
# 3. 驗證免密成功後,在 Mac mini 寫入禁用密碼策略並校驗重載
sudo tee /etc/ssh/sshd_config.d/99-hardening.conf <<'EOF'
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
EOF
sudo sshd -t && sudo launchctl kickstart -k system/com.openssh.sshd
人工核對要點:
- 確認
~/.ssh權限為700、authorized_keys為600,否則sshd會忽略公鑰。 - 保留原管理員窗口不退,新開端測試
ssh -o PubkeyAuthentication=no [email protected]確認返回Permission denied (publickey).。 - 開啟 FileVault 時重啟進系統前 SSH 未啟動,須外設解鎖或用
fdesetup authrestart。

圖:本站根據 Apple 遠程訪問文檔 整理的示意圖
訂閱與開通提示:配置好高速免密 SSH 後,個人日常編碼可選 Claude Pro 充值指南 與 Claude Pro iOS 自助充值;重度代理協作建議搭配 Claude Code 訂閱專題 及 Claude Max 20x iOS 自助充值;全部服務彙總見 全部商品。全站以商品頁實時標價為準,標價不含稅,開具國內增值稅發票另付 6% 稅費;單獨申請自定義明細電子收據免費,企業批量採購支持簽訂正式中文服務合同並對公結算。
常見問題
問:為什麼把 PasswordAuthentication 設為 no 後還是提示輸入密碼?
答:macOS 的 OpenSSH 預設開啟鍵盤交互認證,需同時設定 KbdInteractiveAuthentication no,經 sudo sshd -t 校驗並重啟 com.openssh.sshd 後才會徹底攔截口令。
問:為什麼帶 UseKeychain yes 的配置拿到 Linux 上執行會報錯?
答:UseKeychain 是 Apple 專有擴展字段,跨平台共用 ~/.ssh/config 時應加上 IgnoreUnknown UseKeychain 或拆開主機段。
問:ControlMaster auto 遇到斷網通道僵死怎麼處理?
答:執行 ssh -O exit macmini-lan 強制清理本地複用套接字,再重新發起連接即可恢復。
問:企業採購 Claude 訂閱配合伺服器研發如何開具 6% 增值稅發票?
答:可通過 AI 訂閱與帳號服務總覽 進入 Claude KYC 協助說明 或選用 ChatGPT Business 團隊版定製 辦理對公簽約。全站以商品頁實時標價為準,標價不含稅,開具國內增值稅發票另付 6% 稅費;單獨申請自定義明細電子收據免費。本站明示質保訂閱、不質保封號,請嚴格遵守服務商條款合規使用。
相關教學
- OpenClaw 安全漏洞加固與最小權限沙箱:openclaw security audit 與防 0.0.0.0 暴露
- Claude Code 收費標準與每月費用說明:免費版能不能用、Team Premium 席位要求與 5 小時窗口
- Claude Code 省額度高階策略:子代理分流 Haiku、/effort 推理降檔與動態工具延遲加載
- OpenClaw 報錯 1006 斷連怎麼排查:網關進程崩潰、端口錯配與反代 WebSocket 升級頭
0 條評論
還沒有評論,來搶沙發~