Git SSH Cloneが出来なかった
はじまり
コマンドラインから、自分のプロジェクトをSSHでCloneした時にPermission denied
でgit clone
が出来ませんでした。
git@gitlab.com: Permission denied (publickey,keyboard-interactive).
git clone git@gitlab.com:[USER]/[PROJECT]
エラー出力:
Cloning into 'projectName'...
git@gitlab.com: Permission denied (publickey,keyboard-interactive).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
自分の開発環境:
MacBook Pro (13-inch, M1, 2020)
ターミナル での実行
そもそもGitLabのアカウントにてSSHキーの設定をしていなかったので、まずはGitLabのマイページからSSHキーの設定から、以下の手順で行なっていく。
GitLabでのSSHキー設定手順
- ssh鍵の作成。公開鍵の内容をコピーする。
- GitLabの右上アイコンからPreferenceを選択
- 左側のリストからSSHキーを選択
- SSHキーのページで、Add an SSH keyのクリップボードに公開鍵の内容を貼り付け。
- Addボタンをクリック
ssh鍵の作成はターミナル上で以下のコマンド。
このコマンド入力後、色々聞かれるがデフォルトで良ければ全部EnterでOK。
ssh-keygen -t rsa -C "your_email@example.com"
デフォルトでは、~/.ssh/id_rsa.pub
に公開鍵が保存されるはずなので、
公開鍵の内容をコピーしておく。
ここまでで、SSHキーの設定は完了。GitLabから設定完了のメールが届くはず。
まだできない
ここでもう一度、最初のgit clone
を実行したところ同様のエラーが出力された。
エラー出力:
Cloning into 'projectName'...
git@gitlab.com: Permission denied (publickey,keyboard-interactive).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
.ssh/configの設定をする
.ssh/config
に以下の情報を追記する。
Host git_ssh (任意の名前)
User ユーザー名
Port 22
Hostname gitlab.com
IdentityFile ~/.ssh/id_rsa (秘密鍵のパス)
IdentitiesOnly yes
この時点で、先ほどのコマンドを入力すると同様のエラー出力。(省略)
git clone git@gitlab.com:[USER]/[PROJECT]
解決
.ssh/configの内容を考慮
以下を参照したところ、.ssh/configの内容を考慮してコマンドを叩く必要がある。
https://qiita.com/kawahara_hiroyuki/items/3ad35c548d1007c12109
git clone git@git_ssh:[USER]/[PROJECT]
# git clone git@[.ssh/configのHost名]:[USER]/[PROJECT]
これで無事に、git clone ができた。
別解
ssh-agent
実際には、上記の方法に気づくまでかなり時間がかかり別の方法での解消となった。
以下の記事を参考にして、ssh-add
で、ssh-agentを確認して追加する方法で解消した。
https://zenn.dev/takazumi/articles/951f674fc2ea19
ssh-add -l
>> The agent has no identities.
ssh-add [秘密鍵のPATH]
>> Identity added: [秘密鍵のPATH]
このコマンドを叩いた後に、当初のコマンドを叩いたところ無事にcloneができた。
こちらの方法では、Host名を考慮して叩かずとも無事にCloneできる。
git clone git@gitlab.com:[USER]/[PROJECT]
参照記事