macOS で個人用 SSH キーをセットアップする
The third-party Git Credential Manager (GCM) can be used as alternative method of connecting to Bitbucket Cloud from the Git CLI. If you do not want to configure SSH access for your Bitbucket Cloud account, you can download and install the GCM from Git Credential Manager on GitHub. Note that the GCM works over HTTPS, not SSH. Ensure your Git remotes are using HTTPS, such as:git clone https://{username}@bitbucket.org/{workspace}/{repository}.git
セキュア シェル プロトコル (SSH) は、デバイスと Bitbucket Cloud 間の安全な接続を確立するために使用されます。接続は、非公開 SSH キーから生成される公開 SSH キーを使用して認証されます (非公開/公開キーペアとも呼ばれます)。安全な (暗号化された) 接続を使用して、ローカル デバイスと Bitbucket Cloud 間でソース コードを安全に送信します。SSH を使用して Bitbucket Cloud に接続するようにデバイスをセットアップするには、次の操作が必要です。
SSH エージェントを起動します。
macOS で OpenSSH をインストールする
A version of OpenSSH should be pre-installed on macOS. To check if OpenSSH is installed, open a terminal and run:
ssh -V
出力には、インストールされている OpenSSH のバージョンが表示されるはずです。
Homebrew を使用してさらに新しいバージョンの OpenSSH をインストールするには、以下を実行します。
brew install openssh
OpenSSH が正常にインストールされたことを確認するには、以下を実行します。
ssh -V
出力には、インストールされている OpenSSH のバージョンが表示されるはずです。
SSH エージェントを起動する
git
が SSH キーを使用できるようにするには、SSH エージェントがデバイス上で実行されている必要があります。
すでに実行されているかどうかを確認するには、ps
コマンドを実行します。ssh-agent
がすでに実行されている場合は、出力に次のように表示されるはずです。
$ ps -ax | grep ssh-agent
19998 ?? 0:00.20 /usr/bin/ssh-agent -l
エージェントを起動するには、以下を実行します。
eval $(ssh-agent)
このコマンドを、~/.bashrc
、~/.zshrc
、~/.profile
または同等のシェル設定ファイルに追加する必要がある場合があります。このコマンドをシェル設定ファイルに追加すると、ターミナルを開いたときにエージェントが実行されていることを確認できます。
SSH キー ペアを作成する
SSH キー ペアを作成するには、以下の手順に従います。
ターミナルを開いて、
cd
を使用してホームまたはユーザー ディレクトリに移動します。たとえば次のようにします。cd ~
ssh-keygen
を使用して、次のように SSH キー ペアを生成します。ssh-keygen -t ed25519 -b 4096 -C "{username@emaildomain.com}" -f ~/.ssh/{ssh-key-name}
ここで:
{username@emaildomain.com}
は、仕事用のメール アカウントなど、Bitbucket Cloud アカウントに関連付けられているメール アドレスです。{ssh-key-name}
はキーの出力ファイル名です。bitbucket_work
のような識別可能な名前を使用することをお勧めします。
[パスフレーズを入力] のプロンプトが表示された際には、パスワードを入力するか、パスワードを空白のままにできます。パスワードを入力すると、Bitbucket Cloud にコンタクトする Git コマンド (git push、git pull、git fetch など) を使用した場合のように、SSH を使用するたびにこのパスワードの入力を求められます。パスワードを入力した場合は、デバイスにアクセスできる他のユーザーがそのキーを使用できなくなります。
完了すると、ssh-keygen
によって次の 2 つのファイルが出力されます。
{ssh-key-name}
— 非公開キーです。{ssh-key-name}.pub
— 公開キーです。
SSH エージェントにキーを追加する
SSH エージェント (ssh-agent
) に SSH キーを追加するには、次の手順に従います。
{ssh-key-name}
を非公開キーの名前に置き換えて、次のコマンドを実行します。ssh-add ~/.ssh/{ssh-key-name}
Bitbucket に接続する際に正しい SSH キーが使用されるように、次の設定で SSH 設定ファイル (
~/.ssh/config
) を更新または作成します。Host bitbucket.org AddKeysToAgent yes IdentityFile ~/.ssh/{ssh-key-name}
Where
{ssh-key-name}
is the name of the private key file once it has been added to thessh-agent
.
Bitbucket Cloud に公開キーを提供する
ユーザー アカウントに SSH キーを追加するには、次の手順に従います。
上部のナビゲーション バーにある [設定] (歯車アイコン ) を選択します。
[設定] ドロップダウン メニューから [Bitbucket 個人設定] を選択します。
[セキュリティ] で、[SSH キー] を選択します。
[キーを追加] を選択します。
[SSH キーを追加] ダイアログで [ラベル] を入力して、追加するキーを識別しやすくします。たとえば、
Work Laptop <Manufacturer> <Model>
です。 意味のあるラベルを付けると、将来古いキーや不要なキーを特定するのに役立ちます。Open the public SSH key file (public keys have the
.pub
file extension) in a text editor. The public key should be in the.ssh/
directory of your user (or home) directory. The contents will be similar to:ssh-ed25529 LLoWYaPswHzVqQ7L7B07LzIJbntgmHqrE40t17nGXL71QX9IoFGKYoF5pJKUMvR+DZotTm user@example.com
公開キーファイルの内容をコピーして、[SSH キーを追加] ダイアログの [キー] フィールドに貼り付けます。
Under Expiry, select No expiry to not set an expiry date, or select Expires on and then select the date picker to set a specific date for your SSH key to expire. Note: The default date range for expiry is set to 365 days (one year) from today’s date.
[キーを追加] を選択します。
キーが正常に追加されると、ダイアログが閉じて [SSH キー] ページにキーがリストされます。
If you receive the error
That SSH key is invalid
, check that you copied the entire contents of the public key (.pub
file).
SSH 認証が機能することを確認する
SSH キーが正常に追加されたことをテストするには、デバイスでターミナルを開いて次のコマンドを実行します。
ssh -T git@bitbucket.org
SSH が SSH キーを使用して Bitbucket に正常に接続できれば、コマンドは次のような出力を生成します。
authenticated via ssh key.
You can use git to connect to Bitbucket. Shell access is disabled
この内容はお役に立ちましたか?