タイトル: プロキシの設定
SEOタイトル: Git プロキシ設定完全ガイド(http.proxy / URL 別 / 認証 / SSH ProxyCommand)
| この記事の要点 |
|
Git プロキシ設定の全体像
社内ネットワークやファイアウォール環境で GitHub / GitLab / 自社 Git サーバへ接続する場合、HTTP/HTTPS プロキシ経由のアクセスが必要になります。Git は http.proxy という設定キーでプロキシを指定できます。設定範囲は 3 段階で、優先度は リポジトリ > ユーザ > システム です。
| スコープ | コマンド | 保存先 |
|---|---|---|
| システム | git config --system http.proxy ... | /etc/gitconfig |
| ユーザ(推奨) | git config --global http.proxy ... | ~/.gitconfig |
| リポジトリ | git config http.proxy ... | .git/config |
基本設定: http.proxy / https.proxy
# HTTP / HTTPS 両方にプロキシを設定
git config --global http.proxy http://proxy.example.com:8080
git config --global https.proxy http://proxy.example.com:8080
# 認証付きプロキシ(ユーザ名・パスワード埋め込み)
git config --global http.proxy http://user:password@proxy.example.com:8080
# HTTPS のみ別プロキシを使う
git config --global https.proxy http://secure-proxy.example.com:8443
# 現在の設定を確認
git config --global --get http.proxy
git config --global --get https.proxy
# 全プロキシ設定を一覧
git config --global --list | grep -i proxy
注意: http.proxy は HTTP プロトコルでのアクセスにだけ使われ、HTTPS 通信時には https.proxy が参照されます。多くの企業環境では同じプロキシなので両方に同じ値を入れます。
URL 別プロキシ設定
「社内 Git サーバには直結したいが GitHub にはプロキシ経由」のように接続先で振り分けたい場合は http.<URL>.proxy を使います:
# GitHub だけプロキシ経由
git config --global http.https://github.com.proxy http://proxy.example.com:8080
# 社内 GitLab は直結(空文字でプロキシを無効化)
git config --global http.https://gitlab.internal.example.com.proxy ""
# 特定ホストパターン
git config --global http.https://*.googleusercontent.com.proxy http://proxy.example.com:8080
# 削除
git config --global --unset http.https://github.com.proxy
パスワード文字列のエスケープ
パスワードに @ や : や # などの記号が含まれると URL パーサが誤動作します。パーセントエンコーディングが必要です:
| 文字 | エスケープ後 | 意味 |
|---|---|---|
@ | %40 | ユーザ・ホスト区切りと衝突 |
: | %3A | ユーザ・パスワード区切りと衝突 |
/ | %2F | パス開始と衝突 |
# | %23 | フラグメント開始と衝突 |
| 半角空白 | %20 | URL に空白を含めない |
# 例: パスワード "P@ss:w0rd!"
git config --global http.proxy http://user:P%40ss%3Aw0rd%21@proxy.example.com:8080
環境変数による設定
git に限らず curl / wget / pip も同じ環境変数を参照します。シェル全体で揃えるならこちらが便利:
# ~/.bashrc / ~/.zshrc / /etc/profile.d/proxy.sh など
export HTTP_PROXY=http://proxy.example.com:8080
export HTTPS_PROXY=http://proxy.example.com:8080
export NO_PROXY=localhost,127.0.0.1,.internal.example.com
# 小文字版も両方書いておくと無難
export http_proxy=$HTTP_PROXY
export https_proxy=$HTTPS_PROXY
export no_proxy=$NO_PROXY
# Windows (PowerShell) では
# $env:HTTPS_PROXY = "http://proxy.example.com:8080"
NO_PROXY にはプロキシを通したくないホストを列挙します。社内 Git サーバや localhost を入れておくと便利です。
SSH 接続をプロキシ経由にする
Git の SSH (git@github.com:user/repo.git) は http.proxy の影響を受けません。SSH の ProxyCommand / ProxyJump を使います:
# ~/.ssh/config
Host github.com
HostName github.com
User git
# nc / ncat を使った HTTP CONNECT 中継(GNU netcat 推奨)
ProxyCommand nc -X connect -x proxy.example.com:8080 %h %p
# corkscrew を使う場合
Host github.com
ProxyCommand corkscrew proxy.example.com 8080 %h %p ~/.ssh/proxy_auth
# 多段 SSH(踏み台経由)
Host github.com
ProxyJump bastion.example.com
GitHub は SSH を 443 ポートでも受け付けます。会社のプロキシが 22 番ポートをブロックしている場合、443 経由なら HTTPS と同じ扱いでプロキシを通れます:
# ~/.ssh/config
Host github.com
HostName ssh.github.com
Port 443
User git
# 接続テスト
# ssh -T git@github.com
# → Hi <user>! You've successfully authenticated...
NTLM 認証(Active Directory プロキシ)対策
古い企業プロキシは Basic 認証ではなく NTLM / Kerberos を要求します。Git は NTLM を直接話せないので cntlm をローカル中継として挟みます:
# Ubuntu
sudo apt install cntlm
# /etc/cntlm.conf 設定例
# Username yourname
# Domain EXAMPLE
# PassNTLMv2 <生成したハッシュ>
# Proxy proxy.example.com:8080
# Listen 3128
# パスワードハッシュ生成
cntlm -H
# 起動
sudo systemctl start cntlm
# Git からは cntlm(ローカル 3128)を見る
git config --global http.proxy http://127.0.0.1:3128
git config --global https.proxy http://127.0.0.1:3128
SSL 証明書まわり
SSL 終端プロキシ(社内プロキシが中間者として独自 CA 証明書を発行)の環境では、自社 CA 証明書を Git に渡す必要があります:
# 自社 CA を Git に教える
git config --global http.sslCAInfo /etc/ssl/certs/company-ca-bundle.pem
# OS の CA バンドルを使う
git config --global http.sslCAPath /etc/ssl/certs
# やむを得ず検証をスキップ(非推奨)
git config --global http.sslVerify false
# 特定ホストだけ検証スキップ
git config --global http.https://internal-git.example.com.sslVerify false
プロキシ解除と確認
# 解除
git config --global --unset http.proxy
git config --global --unset https.proxy
# 全プロキシ系設定を削除
git config --global --unset-all http.proxy
git config --global --unset-all https.proxy
# 確認: もう設定されていないこと
git config --global --get http.proxy # → 何も表示されない
git config --global --list | grep -i proxy
# それでもうまくいかないときは環境変数を疑う
env | grep -i proxy
unset HTTP_PROXY HTTPS_PROXY http_proxy https_proxy
トラブルシューティング
| 症状 | 原因 | 対処 |
|---|---|---|
Could not resolve host: github.com | DNS 解決不可(プロキシ越し DNS 必要) | プロキシ設定 or hosts ファイル |
Failed to connect to proxy.example.com port 8080 | プロキシダウン or ホスト名誤り | ping / telnet で疎通確認 |
error: RPC failed; HTTP 407 | プロキシ認証失敗 | パスワード再設定 / エスケープ確認 |
SSL certificate problem: unable to get local issuer certificate | 自社 CA 未登録 | http.sslCAInfo |
fatal: unable to access ... Empty reply from server | プロキシが HTTPS CONNECT を拒否 | プロキシ管理者に確認 |
FAQ
Q: パスワードを平文で書きたくない
A: cntlm をローカル中継として挟むか、git credential ヘルパーで OS のキーチェーンに保存。~/.gitconfig はパーミッション 600 にしておく。
Q: 在宅勤務時にプロキシ無し、出社時にプロキシ有り、を自動切替したい
A: ~/.gitconfig の [includeIf "gitdir:...] や、シェル関数で git config --global を切替えるスクリプトを作る。ネットワーク自動検出は OS の autoproxy 機能や direnv も活用可。
Q: GitHub Desktop / VS Code の Git 連携にも反映される?
A: git config --global は ~/.gitconfig に書かれるので、内部で同じ git を呼ぶアプリには反映されます。ただし VS Code の組み込み機能(HTTP リクエスト)には別途 http.proxy 設定が必要。