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 (private repositories)
Host github.com
HostName github.com
User <username>
IdentityFile ~/.ssh/<owner>_<device>_<os>_<modifier>
# contributors (public repositories)
# [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/<owner>_<device>_<os>_<modifier>
# personal
Host codeup.aliyun.com
HostName codeup.aliyun.com
User <username>
IdentityFile ~/.ssh/<owner>_<device>_<os>_<modifier>
# corporate
Host git-open.<corporate>.cn
HostName git-open.<corporate>.cn
User <username>
IdentityFile ~/.ssh/<corporate>_<owner>_<device>_<os>_<modifier>
# corporate
Host git-biz.<corporate>.cn
HostName git-biz.<corporate>.cn
User <username>
IdentityFile ~/.ssh/<corporate>_<owner>_<device>_<os>_<modifier>
# corporate
Host git-core.<corporate>.cn
HostName git-core.<corporate>.cn
User <username>
IdentityFile ~/.ssh/<corporate>_<owner>_<device>_<os>_<modifier>
# corporate
Host git-3rdparty.<corporate>.cn
HostName git-3rdparty.<corporate>.cn
User <username>
IdentityFile ~/.ssh/<corporate>_<owner>_<device>_<os>_<modifier>
这样,
对 [email protected]:xianghongai/vscode-javascript-snippet.git
仓库进行操作时,走 Host 为 github.com
的配置。
对 [email protected]:xianghongai/vscode-javascript-snippet.git
仓库进行操作时,走 Host 为 public.github.com
的配置。
通过 HTTPS 启用 SSH 连接
# https://docs.github.com/en/authentication/troubleshooting-ssh/using-ssh-over-the-https-port
# Enabling SSH connections over HTTPS
Host github.com
HostName ssh.github.com
User <username>
IdentityFile ~/.ssh/<key>
Port 443
配置代理
Host github.com
HostName github.com
User <username>
IdentityFile ~/.ssh/<key>
# 方式一:使用 ProxyJump
ProxyJump user@<jumpserver>
# 方式二:使用 ProxyCommand
ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=20171 # HTTPS
ProxyCommand nc -v -x 127.0.0.1:20170 %h %p # SOCKS5, Linux
ProxyCommand connect -S 127.0.0.1:20170 %h %p # SOCKS5, Windows