Millet Porridge

English version of https://corvo.myseu.cn

0%

OpenSSH Series 2: Use ssh config

Previous login method

In the previous blog, you needed to enter the full command to log in. There is an easy way to simplify that command.

1
~ ❤  ssh [email protected] -i ~/.ssh/id_rsa_test

Use ~/.ssh/config

1
2
3
4
5
6
7
8
9
~ ❤  cat ~/.ssh/config
#ForwardAgent yes
#ForwardX11 yes

Host ali # The host name
User root
HostName 1.2.3.4
Port 22
IdentityFile ~/.ssh/id_rsa_test

You may create this file to help OpenSSH identify the host you want to login, and the command will be:

1
~ ❤  ssh ali

Share some configurations

Here are some common configurations for GitHub and other public repositories. You can copy the content, but you need to replace the config file with your private key path.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## gist.github.com 配置
Host gist.github.com
User git
HostName gist.github.com
IdentityFile ~/.ssh/id_rsa_test

## Github配置
Host github.com
User git
HostName github.com
IdentityFile ~/.ssh/id_rsa_test

## Gitlab配置
Host gitlab.com
User git
HostName gitlab.com
IdentityFile ~/.ssh/id_rsa_test

## bitbucket配置
Host bitbucket.org
User git
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa_test

Here is the command when I want to test the connection with GitHub.

1
2
~ ❤  ssh -T [email protected]
Hi corvofeng! You've successfully authenticated, but GitHub does not provide shell access.

What if ssh keeps asking the passphrase

1
2
~ ❤  ssh ali
Enter passphrase for key '/home/corvo/.ssh/id_rsa_x1c':

The recommended way is to add this sentence to the configuration file (~/.ssh/config):

1
2
3
4
5
6
7
8
AddKeysToAgent yes

## bitbucket配置
Host bitbucket.org
User git
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa_test
# ...

Refer to https://stackoverflow.com/a/40333362