When setting up Github, the official documentation requires backing up the current id_rsa and generating a new private key for github login. If you do, the new private key will not be able to log on to the previous machine. This method is a little violent… Fortunately, SSH lets you log in to different domains using different private keys.

First, when the private key is added, different private key files are generated by specifying different file names

ssh-keygen -t rsa -f ~/.ssh/id_rsa.work -C "Key for Work"

ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "Key for GitHub"
Copy the code

Added the SSH configuration file and modified the permission

touch ~/.ssh/config

chmod 600 ~/.ssh/config
Copy the code

Example Modify the contents of the Config file

Host *.workdomain.com

    IdentityFile ~/.ssh/id_rsa.work
    User lee
 
Host github.com
    IdentityFile ~/.ssh/id_rsa.github
    User git
Copy the code

When logging in, SSH will read the corresponding private key file according to the different domain

ssh -T [email protected]
Copy the code

If you want to use the same computer to access two or more repositories on Github, the principle is similar:

1. Run the following command on the terminal to view the key.

ls ~/.ssh
Copy the code

If id_rsa and id_rsa.pub exist, a pair of keys/public keys already exist.

2. Create a new key/public key and specify the key name, for example, id_rsa_x (x is any name).

ssh-keygen -t rsa -f ~/.ssh/id_rsa_x -C "[email protected]"
Copy the code

After the operation is complete, two files id_rsa_x and id_rsa_x.pub are added to the directory.

3. Create a config file in the ~/. SSH/folder

$ touch config
$ vim config
Copy the code

Edit the config file to configure different repositories to point to different key files.

# First account, Default account Host github.com HostName github.com User git IdentityFile ~/. SSH /id_rsa # Second account Host second.github.com # HostName github.com User git IdentityFile ~/. SSH /id_rsa_xCopy the code

Principle 1. SSH client is in a similar [email protected]: githubUserName/repName git * * address which the private key, to identify using the local address of the User is in front of the @ git, Host is github.com after @. 2. If the User and Host of all accounts are git and github.com, only one private key can be used. Therefore, configure the User and Host so that each account uses its own Host and the domain name of each Host is resolved to github.com. For example, Host second.github.com in the preceding configuration. 3. Configure the alias, new address is [email protected]: githubUserName/repName git * * when adding remote warehouse (in use). This allows SSH to distinguish between different accounts when connecting.

4. View the values of SSH keys and add them to the corresponding GitHub accounts

$ cat id_rsa.pub
$ cat id_rsa_x.pub
Copy the code

Copy the values to the SSH keys of your GitHub account.

5. Clear the local SSH cache and add a new SSH key to the SSH Agent

$ ssh-add -D
$ ssh-add id_rsa
$ ssh-add id_rsa_x
Copy the code

Finally confirm that the new key has been added successfully

$ ssh-add -l
Copy the code

6. Test the SSH link

ssh -T [email protected] ssh -T [email protected] # xxx! You've successfully authenticated, but GitHub does not provide bash accessCopy the code

7. Cancel git global username/mailbox setting and set independent username/mailbox

$git config --global --unset user.name $git config --global --unset user.email $git config user.email "[email protected]" $git config user.name "XXXX"Copy the code

View the configuration of your Git project

git config --list
Copy the code

8. Enter the project directory on the command line and rebuild the origin (whatever for the corresponding project address).

$git remote rm origin Note the Host name $git remote add origin [email protected]: githubUserName/repName git $git remote - v # to check the remoteCopy the code

10. Remote push test first create a remote repository named testProj on GitHub, and then set up a local repository.

$ cd ~/documnts
$ mkdir testProj
Copy the code

1. Go to the testProj folder and create the redme. md file 2. Initialize this folder as git 3. Add and commit readme. md to git local repository 4. 5. Push readme. md to the remote repository

$ cd testProj $ echo "# ludilala.github.io" >> README.md $ git init $ git add README.md $ git commit -m "first commit" # If a remote connection has already been added, Do not need to add $git remote again add origin https://github.com/ludilalaa/ludilala.github.io.git $git push -u origin masterCopy the code