跳到主要内容

SSH 配置

为不同环境配置和启动不同账号、不同密钥。

系统配置文件,默认路径为 /etc/ssh/ssh_config,为未指定的值提供默认值。

用户配置文件,默认路径为 ~/.ssh/config,没有则创建 (mkdir -p ~/.ssh && chmod 700 ~/.ssh && touch ~/.ssh/config),完成编辑后改回只读 (chmod 600 ~/.ssh/config)。

配置文件可分为多个配置区段,每个配置区段使用 Host 来区分。

配置示例

# ~/.ssh/config
Host *
IgnoreUnknown AddKeysToAgent,UseKeychain
AddKeysToAgent yes
UseKeychain yes
IdentitiesOnly yes

# personal
Host github.com
HostName github.com
User <username>
IdentityFile ~/.ssh/<place>_<device>_<os>_<platform>_git_id_ed25519

# contributors
# [email protected]:xianghongai/vscode-javascript-snippet.git →
# [email protected]:xianghongai/vscode-javascript-snippet.git
Host public.github.com
HostName github.com
User <username>
IdentityFile ~/.ssh/<org>_<device>_<os>_<platform>_git_id_ed25519

# personal
Host codeup.aliyun.com
HostName codeup.aliyun.com
User <username>
IdentityFile ~/.ssh/<org>_<device>_<os>_<platform>_git_id_ed25519

# corporate
Host git-open.<corporate>.cn
HostName git-open.<corporate>.cn
User <username>
IdentityFile ~/.ssh/<corporate>_<asset_tag>_<os>_<platform>_git_id_ed25519

# corporate
Host git-biz.<corporate>.cn
HostName git-biz.<corporate>.cn
User <username>
IdentityFile ~/.ssh/<corporate>_<asset_tag>_<os>_<platform>_git_id_ed25519

# corporate
Host git-core.<corporate>.cn
HostName git-core.<corporate>.cn
User <username>
IdentityFile ~/.ssh/<corporate>_<asset_tag>_<os>_<platform>_git_id_ed25519

# corporate
Host git-3rdparty.<corporate>.cn
HostName git-3rdparty.<corporate>.cn
User <username>
IdentityFile ~/.ssh/<corporate>_<asset_tag>_<os>_<platform>_git_id_ed25519

这样,

[email protected]:xianghongai/vscode-javascript-snippet.git 仓库进行操作时,走 Host 为 github.com 的配置。

[email protected]:xianghongai/vscode-javascript-snippet.git 仓库进行操作时,走 Host 为 public.github.com 的配置。

Resources