1
1

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 1 year has passed since last update.

GitLab で公開鍵認証でSSH接続を行う方法

Posted at

GitLab はパスワード認証でもOKだけれども、こちらでも公開鍵認証を使いたい。
設定方法は GitHub で設定する方法と変わることは無いけど改めて記録。

登録方法

こちらから、SSH keysを登録します。
https://gitlab.com/-/profile/keys

image.png

SSH keys はローカルマシンで以下のようにして作成します。(以下はUbuntu20.04/22.04で実験)


$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/nanbuwks/.ssh/id_rsa): 
/home/nanbuwks/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/nanbuwks/.ssh/id_rsa
Your public key has been saved in /home/nanbuwks/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:bjw9NphLCem2iuasOx5CxSGyqCD5a7xLWNyF9pA1AYo nanbuwks@nanbuwks-ThinkPad-X230
The key's randomart image is:
+---[RSA 3072]----+
|.. ...+.         |
|o+o..+ .         |
|E .o= .          |
|+o.o + .         |
|..+ . + S        |
|.+ . . + =       |
|o.=   o X =      |
|o+oo . + + o     |
|+B*.... .        |
+----[SHA256]-----+
$ ls ~/.ssh/ -alh
合計 32K
drwx------  2 nanbuwks nanbuwks 4.0K 10月 24 16:01 .
drwxr-xr-x 82 nanbuwks nanbuwks 4.0K 11月  5 13:51 ..
-rw-------  1 nanbuwks nanbuwks 2.6K 11月  5 15:18 id_rsa
-rw-r--r--  1 nanbuwks nanbuwks  585 11月  5 15:18 id_rsa.pub
-rw-r--r--  1 nanbuwks nanbuwks 1.6K 10月 29 13:03 known_hosts

できた ~/.ssh/id_rsa.pub の内容を貼り付けます。

アクセス方法

登録後、アクセスするレポジトリから SSH のアドレスをコピーします。

image.png

新しくHTTPS で clone したものを変更する場合は

$ git clone <アドレス>

とします。

既に clone したものを操作する場合は、

.git/config

のアドレスを変更します。
例:

url = https://gitlab.com/nanbuwks/AKBONE2022.git

url = git@gitlab.com:nanbuwks/AKBONE2022.git
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?