LoginSignup
2
3

More than 5 years have passed since last update.

GitHubのsshキーをGitLabでも使えるようにする(備忘録)

Posted at

つい最近までGiLabにあるリポジトリにPushができていて問題ないと思っていたけど、先日、GitLabにある別プロジェクトをクローンしようとしたら以下のエラーが出ました。

Cloning into 'wordcloud'...
git@gitlab.xxx.xx.xx: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

GitLabに登録したsshキーは最初にGitHub用に作成したsshキーです。この同じsshキーをconfigで管理して使っていて、sshキー自体は~/.ssh直下ではなく~/.ssh/github/フォルダ内に置いています。

configの設定はこんな感じです。

~/.ssh/config
# GitHub
Host GitHub
        HostName github.com
        IdentityFile ~/.ssh/github/id_rsa
        TCPKeepAlive yes
        IdentitiesOnly yes
        User git

# GitLab
Host GitLab
        HostName gitlab.xxx.xx.xx
        IdentityFile ~/.ssh/github/id_rsa
        TCPKeepAlive yes
        IdentitiesOnly yes
        port 18636
        user git

今回の場合は、GitLabにプッシュはできるけど、クローンができない状況です。最初にGitLabにsshキーを登録してプロジェクトをクローンしたときには、sshキーは~/.sshフォルダ直下にあり、クローンした後でsshキーを~/.ssh/githubに移動させて使っていたのが原因かな、と思い、GitLabのssh登録ページを調べてみると、以下の説明がありました。

Working with non-default SSH key pair paths

If you used a non-default file path for your GitLab SSH key pair,
you must configure your SSH client to find your GitLab private SSH key
for connections to your GitLab server (perhaps gitlab.com).
For your current terminal session you can do so using the following commands
(replacing other_id_rsa with your private SSH key):

ssh-add ~/.ssh/other_id_rsa

つまり、GitLabで使うsshキーがデフォルトではない場合、このssh-addコマンドで認証を追加する必要があるようです。このother_id_rsa~/.ssh/github/id_rsaに変更してクローンしてみると、正常に動作しました。

2
3
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
2
3