GitHubでは2021年8月13日にパスワード認証が廃止されるそうです。
https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/
パスワード認証が廃止された場合、以下のようなメッセージが表示されるようです。
(2021年7月28日のBrownoutsで確認)
$ git clone https://github.com/username/repository.git
Cloning into 'repository'...
Username for 'https://github.com': username
Password for 'https://username@github.com':
remote: Password authentication is temporarily disabled as part of a brownout. Please use a personal access token instead.
remote: Please see https://github.blog/2020-07-30-token-authentication-requirements-for-api-and-git-operations/ for more information.
fatal: unable to access 'https://github.com/username/repository.git/': The requested URL returned error: 403
という訳で、代わりに使用するであろう個人アクセストークンとsshの設定方法をメモします。
(どちらか一方を設定することで、git cloneができるようになります)
#個人アクセストークン
以下のドキュメントを参考に取得します。
https://docs.github.com/ja/github/authenticating-to-github/creating-a-personal-access-token
##取得手順
GitHubにログインした状態で、以下を実施します。
・設定画面( https://github.com/settings/tokens )を開く
・[Generate new token]をクリック
・noteにトークンのわかりやすい名前を記入
・権限を設定
・[Generate token] をクリック
・トークンをコピー (ページを離れると参照不可になる)
・パスワードの代わりに使用する
※トークンを使用してコマンドラインからリポジトリにアクセスするには、権限設定で[repo] を選択します。
※GitHub Actionを更新する場合は権限設定で[workflows]の選択が必要です。
※セキュリティ上の理由から、GitHubは過去 1 年間使用されていない個人アクセストークンを自動的に削除します。
##使用例
リポジトリのURLはリポジトリ画面の[Code]-[HTTPS]から取得できます。
$ git clone https://github.com/username/repository.git
Cloning into 'repository'...
Username for 'https://github.com': username
Password for 'https://username@github.com': ここで取得した個人アクセストークンを入力する
※個人アクセストークンは HTTPS Git 操作だけにしか使用できません。
#SSH
以下のドキュメントを参考に設定します。
https://docs.github.com/ja/github/authenticating-to-github/connecting-to-github-with-ssh
##設定手順
###SSHキーを生成
以下のコマンドでSSHキーを生成します。
WindowsはGitBashで生成できます。
$ ssh-keygen -t ed25519 # 全てデフォルトでエンター
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/username/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
$ ls ~/.ssh # id_ed25519 id_ed25519.pubが生成される。
authorized_keys id_ed25519 id_ed25519.pub
###GitHubアカウントへ新しいSSHキーの追加
GitHubにログインした状態で、以下を実施します。
・設定画面( https://github.com/settings/ssh )を開く
・[New SSH key]をクリック
・Titleを記入
・Keyにid_ed25519.pubの内容を記入
・[Add SSH Key]をクリック
##使用例
リポジトリのURLはリポジトリ画面の[Code]-[SSH]から取得できます。
git clone git@github.com:username/repository.git
Cloning into 'repository'...
The authenticity of host 'github.com (xx.xx.xx.xx)' can't be established.
RSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])? ← yes を応答する
SSHキー生成の時にpassphraseを設定することでパスワードを要求させることもできます。
$ ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/username/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase): ← ここでパスワード設定
Enter same passphrase again: ← ここでパスワード設定(再入力)
$ git clone git@github.com:com:username/repository.git
Cloning into 'repository'...
The authenticity of host 'github.com (xx.xx.xx.xx)' can't be established.
RSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,xx.xx.xx.xx' (RSA) to the list of known hosts.
Enter passphrase for key '/home/username/.ssh/id_ed25519': ← パスワードを要求される
#余談
EC2インスタンスでGitの設定(ユーザー名とEmailアドレスとか)せずにcommitを実行すると、Authorが以下のような値になるみたいです。
EC2 Default User <ec2-user@ip-xx-xx-xx-xx.ap-northeast-1.compute.internal>
#おわり
ドキュメント見ればわかる内容ですが、ざっくりまとめでした。
今回は以上です。