0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🔐 Mac Studioを公開鍵のみでセキュアにSSH運用するまでの記録

Posted at

先日Mac Studio M4Maxモデルを購入し、外部からここで動かした将棋AIを叩きたいぜ!と思ってSSHサーバーのセットアップを行いました。セキュリティ強化の過程を、自分用のメモも兼ねてまとめます。「なぜパスワード認証が無効にならないのか?」と悩んだ方の参考になれば幸いです。

✅ 背景と発端

自宅サーバーとして使っているMac Studioに、iPhoneやWindowsからSSHで接続したい。

ただしパスワードによるログインは絶対に許可したくない(公開鍵認証のみにしたい)。

Available client authentication methods: password,keyboard-interactiveと表示され、意図しない認証方式が有効になっている可能性に気づいた。

🔍 原因調査と判明したこと

macOSのsshd_configは次のように書かれていました:

Include /etc/ssh/sshd_config.d/*
...
UsePAM no

一見すると UsePAM noが有効に見えますが、Includeの後に UsePAMを記述しても優先されないことが判明。

OpenSSHの仕様では、最初に出現した値が優先される(後勝ではなく、先勝)。

/etc/ssh/sshd_config.d/100-macos.confに UsePAM yesが書かれているため、これが有効になっていた。

🔧 実施した対策:01-custom.conf 作成

macOSは /etc/ssh/sshd_config.d/にある設定ファイルを辞書順で読み込むので,

次のファイルを作成:

sudo nano /etc/ssh/sshd_config.d/01-custom.conf

内容:

PasswordAuthentication no
KbdInteractiveAuthentication no
UsePAM no

これにより、UsePAM yesより前に UsePAM noが読み込まれるようになりました。

🔁 設定の反映と確認

sudo /usr/sbin/sshd -t
sudo launchctl kickstart -k system/com.openssh.sshd

反映されたかどうかは以下で確認:

sudo /usr/sbin/sshd -T | grep authentication
sudo /usr/sbin/sshd -T | grep usepam

期待される出力:

passwordauthentication no
kbdinteractiveauthentication no
pubkeyauthentication yes
usepam no

✅ 結果

公開鍵認証のみでログインが許可されるようになった。

UsePAMを無効化したことで、keyboard-interactive認証も無効化。

セキュアな状態で、安心してSSH接続ができるようになった。

📋 まとめ

macOSのsshd設定では、Includeディレクティブの位置と読み込み順が非常に重要とのことですUsePAM noを有効にするには、01-custom.confのような早い順序の設定ファイルを作成するのがもっともシンプルで安全な方法でした。

SSHの設定をしっかりしておくことで、自宅Mac Studioをより安心して外部から活用できるようになりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?