20.

Git プロキシ設定完全ガイド(http.proxy / URL 別 / 認証 / SSH ProxyCommand)

編集
この記事の要点
  • 基本: git config --global http.proxy http://user:pass@proxy:8080 で全リポジトリ共通設定
  • HTTPS 用: https.proxy を別途設定(HTTP/HTTPS で別プロキシのケースに対応)
  • URL 別: http.<URL>.proxy で特定ホストだけプロキシ経由 / 直結を切替
  • SSH のプロキシ: ~/.ssh/configProxyCommand または ProxyJump
  • 企業 NTLM 認証は cntlm をローカル中継、解除は git config --global --unset http.proxy

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フラグメント開始と衝突
半角空白%20URL に空白を含めない
# 例: パスワード "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.comDNS 解決不可(プロキシ越し 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 設定が必要。

編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. 用語一覧
  2. エラー一覧
  3. git本体のインストール(Linux)
  4. Linuxサーバーへのgit導入とクライアントのセットアップ
  5. リモートリポジトリをローカルリポジトリとしてクローンする方法
  6. リモートとローカルのリポジトリを同期(pull)する方法
  7. 設定の確認
  8. gitユーザー名とemailの設定
  9. リモートリポジトリの作成
  10. ローカルリポジトリの作成
  11. 新規ファイル/ディレクトリをインデックスに登録
  12. インデックスの登録状態を確認
  13. ローカルリポジトリの変更をコミット
  14. コミット履歴の確認
  15. クライアントからリモートリポジトリの接続設定、確認、削除
  16. リポジトリへのプッシュ
  17. リモートリポジトリからクライアントへのSSHクローン
  18. リモートとローカルの差分表示
  19. バージョンの確認
  20. プロキシの設定
  21. ローカルをリモートリポジトリの状態に戻す
  22. ブランチの作成, 一覧表示, 切り替え
  23. ブランチのマージと削除
  24. リベース
  25. .gitignoreの書き方
  26. .gitignoreの設定が反映されない場合
  27. 特定のファイルをgitの管理から外す方法
  28. 参照(ORIG_HEAD, HEAD, FETCH_HEAD)
  29. git rm [-r --cached] の取り消し
  30. 一部のディレクトリ/ファイルのみをリポジトリから復元する方法
  31. ローカルとリモートリポジトリの有無を同期
  32. pushの取消し方法
  33. マージツールの起動方法
  34. Gitで「MERGING」の状態

最近更新/作成されたページ