apt でのプロキシ対応
準備
テストに使用できるプロキシを用意します。
やり方は以下で説明しています。
https://qiita.com/m-tmatma/items/7b5ce812c85f30546209
前提条件
この記事ではプロキシの IP アドレスを 192.168.11.9、プロキシのポートを 3128 として説明します。
実際のプロキシの情報に置き換えて設定すること。
apt のプロキシの指定方法
-
/etc/apt/apt.confで指定する方法 -
/etc/apt/apt.conf.d以下に設定ファイルを配置して指定する方法 -
aptの引数の-oで指定する方法 - 環境変数
http_proxyおよびhttps_proxyで指定する方法 (rootユーザーで実行する場合) - 環境変数
http_proxyおよびhttps_proxyで指定する方法 (sudoで実行する場合)
方法1: /etc/apt/apt.conf で指定する方法
以下の内容で /etc/apt/apt.conf を作る
Acquire::http::Proxy "http://192.168.11.9:3128";
Acquire::https::Proxy "http://192.168.11.9:3128";
方法2: /etc/apt/apt.conf.d 以下設定ファイルを配置して指定する方法
以下の内容で /etc/apt/apt.conf.d/proxy.conf を作る
Acquire::http::Proxy "http://192.168.11.9:3128";
Acquire::https::Proxy "http://192.168.11.9:3128";
方法3: apt の引数の -o で指定する方法
export http_proxy=http://192.168.11.9:3128
export https_proxy=http://192.168.11.9:3128
sudo apt update -y -o Acquire::http::Proxy=$http_proxy -o Acquire::https::Proxy=$https_proxy
sudo apt upgrade -y -o Acquire::http::Proxy=$http_proxy -o Acquire::https::Proxy=$https_proxy
方法4: 環境変数 http_proxy および https_proxy で指定する方法 (root ユーザーで実行する場合)
root になる
sudo -i
export http_proxy=http://192.168.11.9:3128
export https_proxy=http://192.168.11.9:3128
apt update -y
apt upgrade -y
方法5: 環境変数 http_proxy および https_proxy で指定する方法 (sudo で実行する場合)
方法5-1: /etc/sudoers に追加
Note: ubuntu 22.04 (ubuntu 21.04も同様) などでは 方法5-2 のほうがよい。
以下の内容を /etc/sudoers に追加する。
Defaults env_keep = "http_proxy https_proxy"
以下のように実行する
export http_proxy=http://192.168.11.9:3128
export https_proxy=http://192.168.11.9:3128
sudo apt update -y
sudo apt upgrade -y
方法5-2: /etc/sudoers.d 以下にファイルを作成 (ubuntu 22.04/21.04 等)
/etc/sudoers.d/proxy (ファイル名は任意) を以下の内容で作る
Defaults env_keep = "http_proxy https_proxy"
以下のように実行する
export http_proxy=http://192.168.11.9:3128
export https_proxy=http://192.168.11.9:3128
sudo apt update -y
sudo apt upgrade -y
Note: ubuntu 22.04 (ubuntu 21.04も同様) では /etc/sudoers が readonly に設定されている。
/etc/sudoers の最後に以下が指定されている。
@includedir /etc/sudoers.d
$ ls -l /etc/sudoers
-r--r----- 1 root root 1671 2月 8 2022 /etc/sudoers
$ sudo tail /etc/sudoers
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "@include" directives:
@includedir /etc/sudoers.d