LoginSignup
38
40

More than 5 years have passed since last update.

gitのssh接続に使用する~/.ssh/configの設定について

Last updated at Posted at 2017-12-26

git cloneできなかったお話

git cloneをしようとして詰まった話です。

他のサイトを閲覧していると以下のコードで可能だと書いてあります。しかし自分の環境だと実行されずエラーを出力しました。

git clone [リポジトリアドレス]
# git cloneを実行
$ git clone git@github.com:kawahara-hiroyuki/kawa.vimrc.git

#以下エラー内容
Cloning into 'kawa.vimrc'...
Permission denied (publickey).
fatal: Could not read from remote repository.

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

~/.ssh/configの設定について

調べるとホスト名の設定が失敗している時に出るエラーの一つだそうです。
ホストの設定をしてある~/.ssh/configを以下のように記述していました。
またそれぞれの行の意味を下に記述します。

# ~/.ssh/config
Host gitgit
  User gituser
  HostName github.com
  IdentityFile ~/.ssh/test
  IdentitiesOnly yes
Host
User gitの通信をするユーザーを指定
HostName 接続先の設定。FQDNを使用している。
こちらを参考にgithubのIPアドレス192.30.253.112でも良い
IdentityFile 秘密鍵の置き場所。今回は~/.ssh/testという秘密鍵を使用している。
秘密鍵についてはこちらを参考
IdentitiesOnly 使用する秘密鍵をIdentityFileだけにする。
デフォルトではnoであり、noだと全ての秘密鍵を試そうとする。

git cloneコマンドの内訳

git cloneコマンドに使う引数の設定は以下のようになっています。
コマンドでssh接続するのと似た感じでしょうか。

git clone [User]@[Host]:[リポジトリアドレス]

今回はgithubに接続しようとします。
[User]はgithubの設定に従って、gitを使用します。(省略する場合は、~/.ssh/configのUserが使用されます)。
[Host]は~/.ssh/configにしたがってgitgitを使います。

~/.ssh/configを考慮して再度clone

具体的には、今回は以下のコマンドが正解でした。

# 今回の場合のcloneするコマンド
git clone git@gitgit:kawahara-hiroyuki/kawa.vimrc.git

# ~/.ssh/configのUserをgitにしてる場合は以下でも可
git clone gitgit:kawahara-hiroyuki/kawa.vimrc.git
38
40
2

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
38
40