0
2

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 KEYの新規作成

Posted at

概要

  • MacOS Sierra以降
  • GitHub, bitbucketを想定

1_ディレクトリの作成

存在しない場合は作成しておく

$ mkdir -p ~/.ssh

2_SSH KEYの作成

  • 鍵長4096、暗号方式をRSAで作成
  • GitHub, bitbucketに登録したメールアドレスを使用
  • 任意のファイル名 (例 github_rsa
$ ssh-keygen -t rsa -b 4096 -C YOU@EMAIL -f ~/.ssh/FILE_NAME

パスフレーズを設定する場合は入力、設定しない場合は入力せずにenter

Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 

上で設定した場合は再入力、未設定の場合は入力せずにenter

Enter same passphrase again:

以下のようになっていればOK!

Your identification has been saved in /Users/YOUR_NAME/.ssh/FILE_NAME.
Your public key has been saved in /Users/YOUR_NAME/.ssh/FILE_NAME.pub.
The key fingerprint is:
SHA256:UCKqxoUaL0owDCIwFCCIFgInbU+wyAtPvZ2XC0X7FSs YOU@EMAIL
The key's randomart image is:
+---[RSA 4096]----+
|^==.. . o   .    |
|@*++.. + .   o   |
|B=++. . o E o    |
|=Bo .o + o o     |
|+=o . + S .      |
|+.     o .       |
|.       .        |
|                 |
|                 |
+----[SHA256]-----+

3_SSH Agentに秘密鍵を登録

安全、便利に使うためにSSH Agentへ秘密鍵を登録しておく

$ eval `ssh-agent`
$ ssh-add -K ~/.ssh/FILE_NAME

設定したパスフレーズを入力してenter

Agent pid 17551
Enter passphrase for /Users/YOUR_NAME/.ssh/FILE_NAME:
Identity added: /Users/YOUR_NAME/.ssh/FILE_NAME
(/Users/YOUR_NAME/.ssh/FILE_NAME)

4_SSH configの設定

configファイルを作成

$ vi ~/.ssh/config

下記の設定を書いておく

GitHub
# GitHub
Host github_main
  HostName github.com
  User git
  Port 22
  IdentitiesOnly yes
  IdentityFile ~/.ssh/FILE_NAME
  ForwardAgent yes
  AddKeysToAgent yes
  UseKeychain yes
  TCPKeepAlive yes
  Compression yes
  UseRoaming no
bitbucket
# bitbucket
Host bitbucket_main
  HostName bitbucket.org
  User git
  Port 22
  IdentitiesOnly yes
  IdentityFile ~/.ssh/FILE_NAME
  ForwardAgent yes
  AddKeysToAgent yes
  UseKeychain yes
  TCPKeepAlive yes
  Compression yes
  UseRoaming no

5_SSH公開鍵の登録

公開鍵をコピー

$ pbcopy < ~/.ssh/FILE_NAME.pub

GitHub, bitbucket各サイトで公開鍵を登録

SSH接続の確認

GitHub
$ ssh -T github_main
bitbucket
$ ssh -T bitbucket_main

GitHub fingerprintsのfingerprintと一致するかを確認、yesと入力してenter

GitHub
The authenticity of host 'github.com (192.30.255.112)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?

下記のように表示されていれば完了

GitHub
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.

Bitbucket fingerprintsのfingerprintと一致するかを確認、yesと入力してenter

bitbucket
The authenticity of host 'bitbucket.org (18.205.93.0)' can't be established.
RSA key fingerprint is SHA256:zzXQOXSRBEiUtuE8AikJYKwbHaxvSc0ojez9YXaGp1A.
Are you sure you want to continue connecting (yes/no)? yes

下記のように表示されていれば完了

bitbucket
You can use git or hg to connect to Bitbucket. Shell access is disabled.

6_SSH KEY作成用シェルスクリプト

SSH configファイルの設定を事前に行っておく。

#!/bin/bash

# Setup SSH Key for Github and Bitbucket.
# --------------------------------------- / ssh_setup

# Enter service name to setup.
echo ""
echo "===== SSHを使用するサービス名を入力してください: [github or bitbucket]"
read sshService

# If there is no directory, Create directory.
if [[ ! -d ~/.ssh ]]; then
    mkdir -p ~/.ssh
    echo ""
    echo "===== .sshディレクトリを作成しました。"
fi

# If there is no file, run.
if [[ ! -f ~/.ssh/"$sshService"_rsa ]]; then
    echo ""
    echo "===== "$sshService"のメールアドレスを入力してください:"
    read gitServiceEmail

    # Generate SSH Key.
    echo ""
    echo "===== SSH Keyを作成します..."
    cd ~/.ssh
    ssh-keygen -t rsa -b 4096 -C $gitServiceEmail -f ~/.ssh/"$sshService"_rsa

    # Start the SSH agent and add a private key.
    echo ""
    echo "===== SSH Agentに秘密鍵を登録します..."
    eval `ssh-agent`
    ssh-add -K ~/.ssh/"$sshService"_rsa

    # Copy the public Key.
    pbcopy < ~/.ssh/"$sshService"_rsa.pub
    echo ""
    echo "===== SSH公開鍵をクリップボードにコピーしました。"

    # Open the Git service website.
    echo ""
    echo "===== "$sshService"で公開鍵を登録してください。"
    read -p "===== "$sshService"へ移動しますか?: [Y/n]"
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        if [[ $sshService = github ]]; then
            open https://github.com
            read -p "===== 登録が完了したら [RETURキー] =>"
            ssh -T github_main
        elif [[ $sshService = bitbucket ]]; then
            open https://bitbucket.org
            read -p "===== 登録が完了したら [RETURキー] =>"
            ssh -T bitbucket_main
        fi
    elif [[ $REPLY =~ ^[Nn]$ ]]; then
        echo ""
        echo "===== "$sshService"でSSH接続を使用するには公開鍵の登録が必要です。"
    fi
else
    echo ""
    echo "===== 既に"$sshService"のSSH Keyが存在するため中止しました。"
fi

あとがき

Qiitaの初投稿になります。何か間違いや、不備があればご指摘いただけると喜びます。今後も自分へのメモを含めて、誰かの何らかの役に立てればと思います。

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?