0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHubで `~/.ssh` 配下を安全に管理する(git-crypt活用法)

Posted at

複数のサーバーやPCを使っていると、~/.ssh/config や秘密鍵の管理がどんどん面倒になりますよね。とはいえ、SSHの秘密鍵を手作業でコピーするのは避けたいし、かといってそのままGitHubで管理するのはリスキー…。

そこで今回は、git-crypt を使ってセキュアに .ssh 配下をGitHubで管理する方法を紹介します。
もちろん、GitHubリポジトリは private に設定してください!

:wrench: 1. git-crypt のインストール

まず、git-crypt を各自の環境にインストールしましょう。

  • macOS:

    brew install git-crypt
    
  • Ubuntu / Debian:

    sudo apt-get update
    sudo apt-get install git-crypt
    
  • その他のOS:

    公式リポジトリ からソースビルドしてください。

:file_folder: 2. リポジトリの準備と git-crypt の初期化

  1. リポジトリを作成・初期化

    cd ~/.ssh
    git init
    
  2. git-crypt を初期化

    git-crypt init
    

:closed_lock_with_key: 3. 暗号化対象ファイルの指定(.gitattributes

git-crypt は、特定のファイル/パスだけを暗号化対象にできます。
例えば、configid_rsa などを含めたい場合は .gitattributes に以下を記述します。

# すべてのファイルを暗号化
* filter=git-crypt diff=git-crypt

# ただし、以下のファイルは暗号化対象から除外
config -filter -diff
README.md -filter -diff

🧪 4. 暗号化チェック用ファイルの作成

暗号化が効いているかの確認用に、以下のような checker.txt を用意しておきます。

If you can read this, encryption is not working.

このファイルがGitHub上で平文で見えていたらアウトです。

ついでに .gitignore も作っておきましょう(例: macOS用):

echo .DS_Store > .gitignore

📤 5. Gitリモート登録とコミット

git remote add origin git@github.com:yourname/.ssh.git
git add --all
git commit -m "Add git-crypt attributes for sensitive files"
git push -u origin main

🧑‍💻 6. GPGキーの登録(復号できるユーザーの追加)

  1. GPGキーがない場合は生成

    gpg --full-generate-key
    
  2. 自分のGPGキーIDを確認

    gpg --list-secret-keys --keyid-format LONG
    

    出力例:

    sec   rsa4096/ABCD1234EFGH5678 2024-01-01 [SC]
    
  3. 復号可能なユーザーとしてGPGキーを登録

    git-crypt add-gpg-user ABCD1234EFGH5678
    

🔄 7. 別のPCで復号する手順(秘密鍵の移動)

✅ 現在のPCでの操作

  1. 秘密鍵をエクスポート

    gpg --export-secret-keys -a ABCD1234EFGH5678 > mykey-private.asc
    
  2. USB/SCPなどで安全に別PCへコピー

    ⚠ このファイルは絶対に流出させないように!

✅ 別のPCでの操作

  1. 秘密鍵をインポート

    gpg --import mykey-private.asc
    
  2. キーがインポートされたか確認

    gpg --list-secret-keys
    
  3. リポジトリをクローン

    git clone git@github.com:yourname/.ssh.git
    cd .ssh
    
  4. git-crypt で復号

    git-crypt unlock
    

    これで、暗号化されていたファイルがローカルで復号されます。

📎 補足:公開鍵も一緒に渡す場合

必要に応じて、公開鍵もエクスポート可能です。

gpg --export -a ABCD1234EFGH5678 > mykey-public.asc

ただし、秘密鍵をインポートすると公開鍵も含まれているので、通常は不要です。

🔒 最後に:セキュリティ上の注意

  • mykey-private.asc は作業後に必ず削除(shred推奨)
  • GPGキーにはパスフレーズをかけておく
  • 信頼できるPC以外では秘密鍵を扱わない

✅ まとめ

  • git-crypt + GPG を使えば、SSH設定や秘密鍵をセキュアにGit管理できる
  • .gitattributes でファイルを選択的に暗号化
  • GPGキーを使って復号権限をコントロール
  • リポジトリは必ず private に!

安全に運用しながら、SSH設定の煩雑さを一気に解消していきましょう 💪

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?