2.

SSH 認証に失敗しました。再試行してください の対処

編集
この記事の要点
  • fatal: Authentication failedgit の認証情報が不正 / 期限切れ
  • 原因 ①: GitHub は 2021-08 から password 認証廃止 → Personal Access Token (PAT) 必須
  • 原因 ②: 古い認証情報がキャッシュされている
  • 対処 ①: PAT を生成して使う
  • 対処 ②: SSH 鍵認証に切り替え

 

エラーの状況

$ git push origin main
Username for 'https://github.com': myuser
Password for 'https://myuser@github.com':
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/.../switching-remote-urls
fatal: Authentication failed for 'https://github.com/user/repo.git/'

# または
remote: HTTP Basic: Access denied
fatal: 認証に失敗しました。再試行してください。

原因

GitHub: 2021-08-13 以降、HTTPS 経由の git 操作でパスワード認証が完全廃止。Personal Access Token (PAT) または SSH 鍵を使う必要があります。

GitLab / Bitbucket: 同様に PAT / SSH 推奨。

対処方法 1: Personal Access Token (PAT)

GitHub の場合

  1. GitHub にログイン → SettingsDeveloper settings
  2. Personal access tokensTokens (classic)Generate new token
  3. Note: トークンの用途を入力(例: "my-laptop")
  4. Expiration: 有効期限(30日 / 90日 / なし)
  5. Scopes: 必要な権限にチェック
    • privateリポジトリ操作 → repo
    • workflow → workflow
  6. Generate token表示されたトークンをコピー(再表示不可)

使い方

$ git push origin main
Username for 'https://github.com': myuser
Password for 'https://myuser@github.com': [PAT をペースト]
# → 成功

# 毎回入力するのは面倒 → キャッシュ
$ git config --global credential.helper cache
$ git config --global credential.helper "cache --timeout=3600"  # 1 時間

# macOS Keychain (推奨)
$ git config --global credential.helper osxkeychain

# Windows Credential Manager
$ git config --global credential.helper manager

# Linux
$ git config --global credential.helper store  # 平文保存、注意
$ git config --global credential.helper cache  # メモリ、安全

対処方法 2: SSH 鍵認証(推奨)

① SSH 鍵生成

# Ed25519 (推奨)
$ ssh-keygen -t ed25519 -C "your@email.com"

# 場所: ~/.ssh/id_ed25519 (秘密鍵) と id_ed25519.pub (公開鍵)
# パスフレーズは設定推奨

# 公開鍵を表示
$ cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAA... your@email.com

② 公開鍵を Git ホスティングサービスに登録

  • GitHub: Settings → SSH and GPG keys → New SSH key
  • GitLab: User Settings → SSH Keys
  • Bitbucket: Personal settings → SSH keys

③ リモート URL を SSH に変更

# 現在の URL 確認
$ git remote -v
origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)

# SSH に変更
$ git remote set-url origin git@github.com:user/repo.git

# 確認
$ git remote -v
origin  git@github.com:user/repo.git (fetch)

# 接続テスト
$ ssh -T git@github.com
Hi user! You've successfully authenticated...

キャッシュされた認証情報の削除

# macOS Keychain
# キーチェーンアクセス.app → "github.com" を検索 → 削除

# Windows Credential Manager
# コントロールパネル → 資格情報マネージャー → "git:..." を削除

# Linux (file ベース)
$ rm ~/.git-credentials

# キャッシュタイムアウト
$ git config --global credential.helper "cache --timeout=0"  # 即削除

HTTPS URL に PAT を埋め込む(非推奨)

# 一時的にトークンを URL に
$ git clone https://USER:PAT@github.com/user/repo.git

# .git/config に保存される (PAT が平文で残る)
$ cat .git/config
[remote "origin"]
    url = https://USER:PAT@github.com/user/repo.git

# セキュリティリスクあり、credential.helper の方が安全

SSH 鍵で複数アカウント使い分け

# 個人用と仕事用で別の SSH 鍵
$ ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_personal
$ ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work

# ~/.ssh/config
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal

Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work

# リポジトリ URL を分岐
$ git clone git@github.com-personal:myuser/my-repo.git
$ git clone git@github.com-work:company/work-repo.git

2 要素認証 (2FA) との関係

2FA を有効にしていても影響なし。むしろ:

  • HTTPS: PAT 必須(2FA 有無に関わらず password 直接は不可)
  • SSH: 公開鍵認証、2FA の影響なし

credential.helper オプション

helper動作OS
cacheメモリに一定時間保持全 OS
store平文ファイル保存全 OS (非推奨)
osxkeychainmacOS KeychainmacOS
managerCredential ManagerWindows
libsecretGNOME KeyringLinux

関連エラー

  • Authentication failed: このページ
  • Permission denied (publickey): SSH 鍵が登録されていない / 認識されていない
  • Host key verification failed: known_hosts に登録されていない(初回接続)
  • fatal: unable to access: プロキシ / DNS / ファイアウォール
  • SSL certificate problem: 証明書エラー

関連記事

編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. SSH2秘密鍵の読み込みに失敗しました
  2. 認証に失敗しました。再試行してください。