【Git】 Git ~ 初期設定 / Windows 編 ~

 【1】インストール

1)公式サイトからインストーラをダウンロードする

https://git-scm.com/

2) インストーラでインストールする

「Select Components」では、全てチェックを付ける。(特に「Git Bash」は後で使う)
それ以外はデフォルト・インストール。

参考文献
https://qiita.com/toshi-click/items/dcf3dd48fdc74c91b409

 【2】SSH公開鍵認証でアクセスする

1)Git Bashを起動

 * [スタート]メニューの[Git]-[Git Bash]を選択

2)以下のコマンド ssh-keygen により、秘密鍵と公開鍵を作成する

ssh-keygen -t rsa -b 4096 -C "your-email@sample.com"

# t "鍵の種類"
# -b "鍵の長さ"
# -C "コメント文(e-mailを入力)"

Enter file in which to save the key (/c/Users/user_id/.ssh/id_rsa): (鍵ファイルの設定場所。Enterキー押下)
Enter passphrase (empty for no passphrase): (何も入力せずに、Enterキー押下)
Enter same passphrase again: (何も入力せずに、Enterキー押下)

 => 秘密鍵(id_rsa)と公開鍵(id_rsa.pub)を作成

3)サーバ側に認証用公開鍵を設置する

mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
chmod 600  ~/.ssh/authorized_keys

4)サーバ側のsshd_configの設定

サーバ側の「/etc/ssh/sshd_config」を修正し、SSH公開鍵認証できるように修正

sudo vi /etc/ssh/sshd_config

修正前

#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

修正後 (修正例)

PubkeyAuthentication yes <<公開鍵認証の許可するかどうか(yesを指定)>>
RSAAuthentication yes <<RSA認証の許可するかどうか(yesを指定)>>

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys <<公開鍵の保存場所を指定>>

 => 「sudo systemctl restart sshd」で再起動

5)サーバ側のファイアウォール確認

sudo firewall-cmd --list-all
# services の欄に ssh が入っているか確認

# ssh がなかったら以下を実行
sudo firewall-cmd --permanent --add-service=ssh

6)動作確認

 * TeraTermなどのSSHクライアントソフトなどで秘密鍵を使ってアクセスしてみる

  + ユーザ名:対象ユーザ入力(ex admin)
  + 鍵認証:秘密鍵を指定(ex id_rsa)

参考文献

https://algorithm.joho.info/programming/git/github-ssh-key/