【CentOS】SSHキー生成手順まとめ。

BitbucketなどにRSAキーを登録したいとき、コンソールでRSAキーを生成するためのコマンドまとめ。

1
2
3
4
5
6
7
8
9
10
11
# 下記ディレクトリに移動
cd ~/.ssh
 
# フォルダを作成
mkdir bitbucket
 
# 作成したフォルダに移動
cd bitbucket
 
# 下記コマンドを実行
ssh-keygen -t rsa -C "yourEmailAddress"

上記コマンドを実行すると生成するファイル名を聞かれるので任意のファイル名を指定する。

1
2
3
4
5
6
7
8
# 任意のファイル名を指定
Enter file in which to save the key (/root/.ssh/id_rsa): id_rsa.bitbucket.org
 
# パスワードを聞かれるので任意のものを設定
Enter passphrase (empty for no passphrase): password
 
# 確認のためもう一度聞かれるので上記と同じものを入力
Enter same passphrase again: password

完了すると下記二つのファイルが生成される。

1
2
id_rsa.bitbucket.org
id_rsa.bitbucket.org.pub

「~/.ssh/config」に設定を追加する。

1
vi ~/.ssh/config

下記を追記。

1
2
3
4
Host bitbucket.org
  HostName bitbucket.org
  IdentityFile ~/.ssh/bitbucket/id_rsa.bitbucket.org
  User git

最後にBitbucketへ公開鍵の登録を行う。

2282dcb2f1eba4d4f1ab88f680f75b12

先ほど生成した「id_rsa.bitbucket.org.pub」ファイルの内容を全てコピーして張り付ければ完了。