It's very simple, just run this command:
$ ssh-keygen
Choose the name for your SSH key like this:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/path/to/home/dir/.ssh/id_rsa): /path/to/home/dir/.ssh/id_rsa_keyname
Skip passphrase by pushing enter button:
Enter passphrase (empty for no passphrase): [press enter]
Enter same passphrase again: [press enter]
Your identification has been saved in /path/to/home/dir/.ssh/id_rsa_keyname.
Your public key has been saved in /path/to/home/dir/.ssh/id_rsa_keyname.pub.
If you don't skip passphrase after all, you will have to enter it each time when you use your SSH key.
After creating the key you should add it to your GitHub account, of course.
After creating SSH key add it to ssh-agent, using this command:
$ ssh-add ~/.ssh/id_rsa_keyname
If you don't have config
file in you .ssh
directory you should create it:
$ cd ~/.ssh
$ touch config
Then open config
file by any editor you have (it could be: nano, vim or sublime):
$ nano config
Add your repo information to you SSH config, by following this pattern:
Host github.com-keyname
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_keyname
Now, to clone repos using your new key you should write it in the host of your repo url, like this:
$ git clone git@github.com-keyname:username/repo.git
If you already clone the repo just change remote url:
$ git remote set-url origin git@github.com-keyname:username/repo.git
That's all. Don't forget to config your user.name
and user.eamil
for the repo:
$ git config --local user.name "username"
$ git config --local user.email user@email.com
If you're still confused, just look at the full example below.
This is a full bash example of how to create multiple SSH keys and how to use them for different GitHub accounts.
For example let's say that we already have two GitHub accounts
like https://github.com/first-account
& https://github.com/second-account
. First account it's your primary account and you already use id_rsa.pub
SSH key to get an access to it.
On the second account you have some project https://github.com/second-account/project
and you want to get access to it by using another SSH key.
Let's start from the home
directory. Use $ cd ~
to get there. Here we go:
$ cd ~
$ ls -al .ssh/
total 48
drwx------ 7 username staff 238 Feb 11 12:25 .
drwxr-xr-x+ 43 username staff 1462 Feb 11 09:48 ..
-rw------- 1 username staff 1675 Feb 6 2016 id_rsa
-rw-r--r-- 1 username staff 412 Feb 6 2016 id_rsa.pub
-rw-r--r-- 1 username staff 6780 Sep 14 18:04 known_hosts
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/id_rsa): /Users/username/.ssh/id_rsa_secondkey
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/username/.ssh/id_rsa_secondkey.
Your public key has been saved in /Users/username/.ssh/id_rsa_secondkey.pub.
The key fingerprint is:
SHA256:9kSBg4TjUeOIvAQhAej+IKchY05Vzg5e1FhTLNyZ1fM username@device.local
The key's randomart image is:
+---[RSA 2048]----+
|Oo +O+oo=.. |
|oo .+*.=+= . o |
|. +.*o. ... o |
| o +.+ . oE |
|. + + S . |
|==.. . . o |
|*=o . . |
|.. . |
| |
+----[SHA256]-----+
$ ls -al .ssh/
total 48
drwx------ 7 username staff 238 Feb 11 12:25 .
drwxr-xr-x+ 43 username staff 1462 Feb 11 09:48 ..
-rw------- 1 username staff 1675 Feb 6 2016 id_rsa
-rw-r--r-- 1 username staff 412 Feb 6 2016 id_rsa.pub
-rw------- 1 username staff 1675 Feb 11 12:25 id_rsa_secondkey
-rw-r--r-- 1 username staff 407 Feb 11 12:25 id_rsa_secondkey.pub
-rw-r--r-- 1 username staff 6780 Sep 14 18:04 known_hosts
Go to second-account
on GitHub. Find Settings
, then SSH and GPG keys
. Press New SSH key
.
And add your id_rsa_secondkey.pub
. Then continue:
$ ssh-add ~/.ssh/id_rsa_secondkey
$ touch ~/.ssh/config
$ nano ~/.ssh/config
Host github.com-secondkey
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_secondkey
$ cd ~/projects/
$ git clone git@github.com-secondkey:second-account/project.git
Cloning into 'project'...
remote: Counting objects: 13230, done.
remote: Compressing objects: 100% (143/143), done.
remote: Total 13230 (delta 72), reused 0 (delta 0), pack-reused 13086
Receiving objects: 100% (13230/13230), 137.44 MiB | 3.46 MiB/s, done.
Resolving deltas: 100% (8058/8058), done.
Checking connectivity... done.
$ git config --local user.name "Second account"
$ git config --local user.email second.account@email.com
That's all. Know you're free to work.
ssh-keygen
- Generate SSH keys
ssh-add
- Add SSH key to ssh-agent
ssh-add -D
- Remove SSH key from ssh-agent
ssh-add -l
- List all added SSH keys
This guide based on Multiple SSH keys for different github accounts
Great thanks to @vvboyko92, who help me setting up multiple SSH keys on real project.