Skip to content

Instantly share code, notes, and snippets.

@rsmn
Forked from jexchan/multiple_ssh_setting.md
Last active October 1, 2020 08:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rsmn/6adbcc498bd6994ba9bf27bcfa18f76f to your computer and use it in GitHub Desktop.
Save rsmn/6adbcc498bd6994ba9bf27bcfa18f76f to your computer and use it in GitHub Desktop.
How to add Multiple SSH keys to Gitlab and Add Sites from both account to Laravel Forge

Multiple SSH Keys settings for different gitlab account

Example Use Case:

  • Server: Digital Ocean
  • Server Name: serverone
  • First Gitlab Account Username: account1
  • Second Gitlab Account Username: account2

create different public key

Presumable you already have your first ssh key

~/.ssh/id_rsa

so we create a second ssh key

$ ssh-keygen -t rsa -C "root@serverone"

give the new key a different name

id_rsa_account2

So now we have 2 keys:

~/.ssh/id_rsa
~/.ssh/id_rsa_account2

Modify the ssh config

$ cd ~/.ssh/
$ touch config
$ subl -a config

Then add

#account2 account
Host gitlab.com-account2
	HostName gitlab.com
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/id_rsa_account2

Go to account2 Gitlab Settings and add SSH Key

copy paste your public ssh key from here

$ cat ~/.ssh/id_rsa_account2

and add it to gitlab ssh settings

Go to Laravel Forge to add your new site

In Laravel Forge, choose Custom when installing repository, and enter the path below

git@gitlab.com-account2:account2/awesomeproject.git

You're good to go!

@tracer8
Copy link

tracer8 commented Mar 29, 2018

Best solution here!

@ronssij
Copy link

ronssij commented Dec 7, 2019

Host gitlab.com-appetiser
  Hostname gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_appetiser

git clone git@gitlab.com-appetiser:appetiser/testing.git

$ git clone git@gitlab.com-appetiser:appetiser/testing.git
Cloning into 'testing'...
ssh: Could not resolve hostname gitlab.com-appetiser: Name or service not known
fatal: Could not read from remote repository.

@shotaK
Copy link

shotaK commented Oct 1, 2020

If the repo exists under other company's account try: git clone git@gitlab.com-username:company/project.git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment