秘密鍵を生成する
すでに秘密鍵をあるが、生成から手順を記しておく。
ssh-keygen -t ed25519 -C "your.email@example.com"
github に接続する
socks5 プロキシが 10.8.0.1 の 1080 ポートを listen している場合のコマンド。
ssh コマンド内でnc (netcat) を実行して、socks5 を経由させる。
$ ssh -T git@github.com -o "ProxyCommand nc -X 5 -x 10.8.0.1:1080 %h %p" -i ~/.ssh/id_ed25519_github_key
Enter passphrase for key '/Users/centipede/.ssh/id_ed25519_github_key':
Hi centipede! You've successfully authenticated, but GitHub does not provide shell access.
コマンドを ~/.ssh/config に記入して接続を簡単にする
$ vi ~/.ssh/config
$ cat ~/.ssh/config
Host github-socks5
HostName github.com
User git
ProxyCommand nc -X 5 -x 10.8.0.1:1080 %h %p
IdentityFile ~/.ssh/id_ed25519_github_key
接続してみる。
$ ssh github-socks5
Enter passphrase for key '/Users/centipede/.ssh/id_ed25519_github_key':
PTY allocation request failed on channel 0
Hi centipede! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
接続に成功。
socks5 経由で git clone をしてみる
$ git -c "http.proxy=socks5h://10.8.0.1:1080" clone git@github-socks5:centipede/example-repo.git
Cloning into 'example-repo'...
Enter passphrase for key '/Users/centipede/.ssh/id_ed25519_github_key':
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (5/5), 5.31 KiB | 2.66 MiB/s, done.
ホスト名に注意
socks5 を経由しない通常の git clone する場合は、 git@github.com:centipede/example-repo.git
を指定するが、github.com のホスト名を、~/.ssh/config で設定したホスト名 github-socks5
に変更して git clone している。
socks5 の指定に注意
私の環境上、github.com の名前解決を sock5 に任せるため、socks5h://10.8.0.1:1080
と指定している。ローカル環境で名前解決をする場合は、socks5://10.8.0.1:1080
でよい。
.git/config に proxy 設定を追加する。
git clone したリポジトリに移動する。
$ tail .git/config.orig
[branch "main"]
remote = origin
merge = refs/heads/main
$ git config http.proxy "http.proxy=socks5h://10.8.0.1:1080"
$ tail .git/config.orig
[branch "main"]
remote = origin
merge = refs/heads/main
[http]
proxy = http.proxy=socks5h://10.8.0.1:1080
最終行に proxy 設定が追加された。この設定で、socks5 経由でリモートリポジトリにアクセスできるようになった。