LoginSignup
0
0

More than 1 year has passed since last update.

ソフトウェアエンジニアの為の SSH 鍵ペアの生成方法 2022

Last updated at Posted at 2022-11-23

SSH 鍵ペアの生成
近々作り直す予定があるので備忘録を記載しておきます。
実際には Windows の msys2 bash 環境での例ですが mac ksh, linux bash でもほぼ同様で大丈夫なはずです

鍵を作成する

# 作業用ディレクトリの作成と移動
mkdir ~/ssh-keygen-work
pushd ~/ssh-keygen-work

# 個人用 .ssh ディレクトリの作成
mkdir ~/.ssh
chmod 700 ~/.ssh

# このマシンにログインする時に使える秘密鍵に対応する公開鍵を登録しておくファイルの作成
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

# 鍵ペアの作成, 古い形式はセキュリティ的に推奨できなくなっている可能性があるので作成する時点で最適なアルゴリズムを確認すべき (新しすぎても古いサーバーで使えなかったりするのでちょうどいい所を見極めること)
ssh-keygen -t ed25519 -C 'your_name@example.com-20211217' -f 'your_name@example.com-20211217.key-with-pp'

# 鍵名に含めた key-with-pp は公開鍵には冗長なので名前を変えておく
mv ./your_name@example.com-20211217.key-with-pp.pub your_name@example.com-20211217.pub

# (一般的には非推奨) パスフレーズ解除版を作る場合にコピー
# cp ./your_name@example.com-20211217.key-with-pp     ./your_name@example.com-20211217.key-without-pp

# (一般的には非推奨) パスフレーズ解除版を作る場合に解除
# ssh-keygen -p -f ./your_name@example.com-20211217.key-without-pp

# 秘密鍵は他のユーザーが触れないパーミッションを設定しておく
chmod 600 ./your_name@example.com-20211217.key-with-pp
chmod 600 ./your_name@example.com-20211217.key-without-pp

# 一連のファイルを .ssh に移動
mv ./your_name@example.com-20211217.* ~/.ssh/

# 作成した公開鍵は authorized_keys に追記しておく
cat ~/.ssh/your_name@example.com-20211217.pub >> ~/.ssh/authorized_keys

# 作業用ディレクトリの後始末
popd
rm -rf ~/ssh-keygen-work

PuTTY keygen でインポート

(Windows の場合のみ)
your_name@example.com-20211217.ppk
your_name@example.com-20211217.puttypub

gitlab に OpenSSH 形式の公開鍵を登録

(gitlab の場合の例、但し github や GHE, Bitbucket でも似たようなページがあったはず)
URL は https://brabrabra.example.com/-/profile/keys のような形

~/.ssh/your_name@example.com-20211217.pub
ssh-ed25519 -------------------------------------------------------------------- your_name@example.com-20211217

SourceTree Options General SSH Key に ppk を登録

Windows の場合に設定する.
Mac の場合は指定できないので, ~/.ssh/config に指定して, 更に設定で embedded git ではなく system git を使用するようにする

~/.ssh/config
Host github.com
    IdentityFile ~/.ssh/your_name@example.com-20211217.key-without-pp
    User git

Pagent に ppk を登録

(Windows の場合)
explorer のアドレスに shell:startup を指定して開き, pageant.exe へのショートカットの引数に ppk を指定

target: (リンク先) のパスは PuTTY を実際にインストールしたパスに読み替えて下さい。

ppk ファイルは target: に実行ファイルのパスに続けて引数として指定します。複数の ppk を指定できます。

target:(リンク先)の例
"C:\Users\your_name\programs\PuTTY\pageant.exe" "C:\Users\your_name\.ssh\your_name@example.com-20211217.ppk" "C:\Users\your_name\.ssh\your_name@example.com-20211220.ppk"

git コマンドを直接使用する場合の id_rsa 以外の名前の鍵の指定方法

GIT_SSH_COMMAND="ssh -i ~/.ssh/your_name@example.com-20211217.key-with-pp -F /dev/null" git fetch
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