LoginSignup
0
0

Mac OSでSSH Keyにパスフレーズ(passphrase)を付与する

Last updated at Posted at 2023-05-24

はじめに

SSH Keyにパスフレーズ(passphrase)を付与する備忘録です。
やったほうが良さそうだけどほんとにやって大丈夫?影響は?という点が気になって躊躇していた時期があったのと、権限周りで少し気づきがあったため記事にしてみました。

SSH keyに対するパスフレーズとは?

ソース:https://docs.github.com/en/authentication/connecting-to-github-with-ssh/working-with-ssh-key-passphrases

SSHキーを使用すると、誰かが自身のコンピューターにアクセスした場合、攻撃者はそのキーを使用するすべてのシステムにアクセスすることができてしまいます。
そこで、さらにセキュリティを強化するために、パスフレーズをSSHキーに追加することができます。
さらに、接続のたびにパスフレーズを入力するのを避けるために、パスフレーズをSSHエージェントに安全に保存することもできます。

SSH keyにパスフレーズを設定する方法

次のコマンドを入力すると、キーペアを再生成せずに、既存の秘密鍵にパスフレーズを設定することが出来ます。

$ ssh-keygen -p -f ~/.ssh/AWS.cer
Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.

鍵に既にパスフレーズが設定されている場合は、新しいパスフレーズに変更する前に、パスフレーズを入力するよう促されます。

$ ssh-keygen -p -f ~/.ssh/AWS.cer
Enter old passphrase:
Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.

Keyのパーミッションを400としている場合、パーミッションエラーで弾かれます。
そのため、600など一時的にパーミッションを変更した上でパスフレーズを設定し、再度400に戻す必要があります。
実際のながれは下記のような形となります。

$ chmod 600 ~/.ssh/AWS.cer
$ sh-keygen -p -f ~/.ssh/AWS.cer
Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.
$ chmod 400 ~/.ssh/AWS.cer

なお、777のようにあまりに広いパーミッションを付与すると別のエラーで設定できなくなります。

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0777 for '~/.ssh/AWS.cer' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Failed to load key ~/.ssh/AWS.cer: bad permissions

対応結果

SSH Keyを利用する際にパスフレーズを求められるようになります。

ssh -i "~/.ssh/AWS.cer" user@ip.address.com
Enter passphrase for key '~/.ssh/AWS.cer':

パスフレーズをキーチェーンに保存して毎回入力する必要をなくす

こちらについてはまた次回。

ガイドはこちら:https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent

<説明>
Mac OS X LeopardからOS X El Capitanでは、これらのデフォルトのプライベートキーファイルは自動的に処理されます:
.ssh/id_rsa
.ssh/identity
初めて鍵を使用するときは、パスフレーズを入力するよう求められます。
パスフレーズをキーチェーンで保存することを選択した場合、再度入力する必要はありません。

それ以外の場合は、鍵を ssh-agent に追加する際に、パスフレーズをキーチェーンに保存することができます。
ソース:https://docs.github.com/en/authentication/connecting-to-github-with-ssh/working-with-ssh-key-passphrases#saving-your-passphrase-in-the-keychain

環境をセキュアに守りつつ、検証を進めていきたいと思います。

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