LoginSignup
23
22

More than 3 years have passed since last update.

BitbucketでSSH接続

Last updated at Posted at 2019-06-14

既に公開鍵が作成されているか確認

$ cat ~/.ssh/id_rsa.pub

cat: /home/vagrant/.ssh/id_rsa.pub: そのようなファイルやディレクトリはありません

と、返ってきたら鍵を作る必要があります。

SSH認証の公開鍵と秘密鍵の作成

    $ cd ~/.ssh
    $ ssh-keygen -t rsa -C mail@mail   <----自分のメールアドレス
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):   <-----Enterを押す
    Enter passphrase (empty for no passphrase):               <-----Enterを押す
    Enter same passphrase again:                       <-----Enterを押す
    Your identification has been saved in /home/vagrant/.ssh/id_rsa.
    Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
    The key fingerprint is:
    0f:19:40:a4:36:b4:89:42:b0:e2:f4:b2:5b:ce:85:39 mail@mail
    The key's randomart image is:
    +--[ RSA 2048]----+
    |o.  .o+          |
    |.. o + .         |
    |+.. *   .        |
    |+... .   o       |
    | o .    S        |
    |  o o    o       |
    | . E .    .      |
    |  = o            |
    | . o             |
    +-----------------+

これで公開鍵と秘密鍵が作成されました。

id_rsa: 秘密鍵
id_rsa.pub: 公開鍵、 Bitbucketに登録する

~/.ssh/configにSSHキーの設定

configに設定を書いていきます。なければ以下のコマンドで作ることもできます。

$ vi ~/.ssh/config

configに以下を書き込みます。

Host bitbucket.org
  HostName bitbucket.org
  IdentityFile ~/.ssh/id_rsa
  User git

設定は以上です!

Bitbucketに公開鍵を登録する

  • Bitbucketにアクセス後、左下のユーザーアイコンを選択すると「Bitbucket settings」が表示されるので選択します。
    スクリーンショット 2019-06-14 12.29.23.png

  • セキュリティのSSH鍵を選択します。
    スクリーンショット 2019-06-14 12.29.38.png

  • 鍵を追加を選択します。
    スクリーンショット 2019-06-14 12.30.05.png

  • SSH鍵登録
    以下のコマンドで公開鍵を表示し、「ssh-rsa〜自分のメールアドレス」をコピーします。

$ cat ~/.ssh/id_rsa.pub
ssh-rsa
**************************************************************************
**************************************************************************
mail@mail

コピーした公開鍵を key に貼り付けます。
Label はなんでもいです。

スクリーンショット 2019-06-14 12.30.15.png

鍵の追加を選択し、完了です。

接続の確認

以下のように表示されれば正常に接続できています。

$ ssh -T git@bitbucket.org
logged in as アカウント名.
You can use git or hg to connect to Bitbucket. Shell access is disabled.

エラーが出る場合

Bad owner or permissions on /home/hoge/.ssh/config

上のエラーが出る場合、パーミッションや所有者を確認する必要があるようですが、
ほとんどの場合、以下で解決できるそうです。権限を変えてあげましょう。

$ chmod 600 ~/.ssh/config

参考記事

https://bokunonikki.net/post/2018/0301_bitbucket_ssh/
https://qiita.com/colorrabbit/items/837810ef6176b3b6f4cc

23
22
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
23
22