LoginSignup
22
30

apt のプロキシ対応

Last updated at Posted at 2021-05-05

apt でのプロキシ対応

準備

テストに使用できるプロキシを用意します。

やり方は以下で説明しています。
https://qiita.com/m-tmatma/items/7b5ce812c85f30546209

前提条件

この記事ではプロキシの IP アドレスを 192.168.11.9、プロキシのポートを 3128 として説明します。
実際のプロキシの情報に置き換えて設定すること。

apt のプロキシの指定方法

  1. /etc/apt/apt.conf で指定する方法
  2. /etc/apt/apt.conf.d 以下に設定ファイルを配置して指定する方法
  3. apt の引数の -o で指定する方法
  4. 環境変数 http_proxy および https_proxy で指定する方法 (root ユーザーで実行する場合)
  5. 環境変数 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
22
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
30