6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

デザイナーが学ぶSSHでGitホストに接続

Last updated at Posted at 2015-05-21

せっかくコマンドライン環境を構築したので、Gitもコマンドラインから操作する。
HTTPSで接続すると毎回パスワードを入力しないといけないので、SSHでGitホストに接続する方法を調べた。ほぼメモ。

SSH認証の仕組み

ただしく扱えるように仕組みを理解しておく

  • 事前に
    • 自分が秘密鍵と公開鍵を作成する
      • 秘密鍵はパスフレーズで暗号化も可能
    • 公開鍵を相手に登録する
  • アクセス時(以下が自動で行われる)
    • 相手が適当なデータ(乱数とか)を公開鍵で暗号化(秘密鍵でしか解けない)
    • 暗号化されたデータを受け取る
    • 秘密鍵で暗号化されたデータを解読(復号化)
    • 復号したデータを相手に渡す
    • 相手が復号化されたデータと元データを比較
    • 一致すれば認証

つまり、「自分にしか解けない暗号データをちゃんと解ければ認証」ということらしい。

やってみる

今回は bitbucket のリモートリポジトリにSSH接続してみる

秘密鍵と公開鍵を作成する

  • ssh-keygenコマンドを実行(鍵のタイプはRSA)
    • 場所は~/.ssh/、ファイル名は任意
ターミナル
$ ssh-keygen -t rsa -f ~/.ssh/imatomix_bitbucket_rsa
  • ~/.ssh/ に、以下のファイルが作成される

    imatomix_bitbucket_rsa : 秘密鍵
    imatomix_bitbucket_rsa.pub : 公開鍵

鍵の管理設定を行う

  • ~./ssh/config ファイルを開く
ターミナル
$ vi ~/.ssh/config
  • 鍵の情報を追記する
    • Host: 管理設定名
    • User: ユーザー名
    • HostName: ホスト名(bitbucket.org)
    • IdentityFile: 鍵の場所
config
Host imatomix.bitbucket.org
User imatomix
HostName bitbucket.org
IdentityFile ~/.ssh/imatomix_bitbucket_rsa

ホストに公開鍵を登録する

  • Bitbucketにログインして、「アカウントの管理」へ

image

  • 左メニューから「SSHキー」にアクセス

image

  • 鍵を追加をクリック

image

  • 「Label」には適当な名前を入力。「Key」に公開鍵の内容をペーストして「鍵を追加」をクリック

    • 公開鍵の内容は、以下コマンドでコピーできる
    ターミナル

$ cat ~/.ssh/bitbucket_rsa.pub | pbcopy
```

image

HTTPSアクセスのリポジトリをSSHアクセスに変更する

  • ローカルリポジトリのconfigファイルを開く
ターミナル
$ cd ローカルリポジトリパス
$ vi .git/config
  • こういう記述がある
[remote "origin"]
        url = https://imatomix@bitbucket.org/imatomix/repository.git 
        fetch = +refs/heads/*:refs/remotes/origin/*
  • こう書き換える
    • url = git@設定名:ユーザー名/リポジトリ名.git
[remote "origin"]
        url = git@imatomix.bitbucket.org:imatomix/repository.git 
        fetch = +refs/heads/*:refs/remotes/origin/*

これでパスワード入力なしにコマンドラインからgitが使用できるようになった

6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?