LoginSignup
4
1

More than 1 year has passed since last update.

Ubuntu ローカルのgit設定~SSHキー取得~GitHub,Gitlab連携まで

Last updated at Posted at 2022-04-02

環境

  • Ubuntu18.04

目的

Ubuntuを入れたときにGitHubやGitLabからSSHキーを利用してcloneできるようにしたい。

ローカルのgit設定

$ sudo apt install git
$ git config --global user.name [名前]
$ git config --global user.email [メールアドレス]

SSHキーの作成

$ ssh-keygen -t rsa -b 4096 -C [メールアドレス]
Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter]
(略)
Enter passphrase (empty for no passphrase): 
(略)
Enter same passphrase again:
(略)

メッセージが出てきたらEnterを押します。合計3回Enterを押すだけで良いです。
四角の中に暗号のようなものが入った表示がされたらOK。

SSHキーをコピー

直接コピーできないので以下のコマンドよりコピー。
xclipを最初にインストールしてください。

$ sudo apt install xclip
$ xclip -sel clip < ~/.ssh/id_rsa.pub

GitHub,GitLabにSSHキーを登録

いずれもプロフィール画面をクリックし、
GitHub:SettingsSSH and GPG keysnew SSH key
GitLab:Edit ProfileSSH keys
を押します。
keyの欄に先ほどコピーしたSSH keyを貼り付けます。titleは適当(PCの区別がつくような名前とか)で大丈夫です。

addのボタンを押したらOKです。

conig設定

.sshディレクトリに任意のエディターでconfigファイルを新規作成。

$ cd .ssh
$ gedit config

configファイルに以下を記述。

Host github
    HostName github.com
    IdentityFile ~/.ssh/id_rsa
    User git
    IdentitiesOnly yes
Host gitlab.com
    HostName gitlab.com
    IdentityFile ~/.ssh/id_rsa
    User git
    IdentitiesOnly yes

SSH接続確認

ネットワークに注意してください。学校や会社で管理されているネットワーク内ではSSH接続ができない可能性があります。
SSH接続する際は、外部ネットに接続してください。
以下のように表示されたら完了です。

GitHub

$ ssh -T git@github.com
Are you sure you want to continue connecting (yes/no)? yes
Hi [username]! You've successfully authenticated, but GitHub does not provide shell access.

GitLab

$ ssh -T git@gitlab.com
Are you sure you want to continue connecting (yes/no)? yes
Welcom to Gitlab, [username]!
4
1
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
4
1