Compass note

未踏の地へ踏み出すコンパス的エンジニアノート

Ubuntu 20.04 LTS のプロキシ設定まとめ

* 本ページはプロモーションが含まれています

"Ubuntu 20.04 LTS のプロキシ設定まとめ"

Ubuntu 20.04 LTS におけるプロキシ設定値まとめていきます。

Ubuntu 20.04 LTS の OS バージョンと kernel の確認

$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.1 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.1 LTS"
$ uname -a
Linux <hostname> 4.4.0-18362-Microsoft #1049-Microsoft Thu Aug 14 12:01:00 PST 2020 x86_64 x86_64 x86_64 GNU/Linux

この Ubuntu は Windows 10 の WSL2 で動かしています。
なので、kernel は 4.4 ベースの Microsft バージョンを利用しているのがわかります。

Ubuntu 20.04 LTS の Proxy 設定

ここでは Ubuntu 20.04 における各プロキシ設定の場所を解説していきます。

バージョンにより設定箇所が異なるアップデートが入るので 20.04 より新しいバージョンをお使いの場合でこの設定がうまく行かない場合は最新の情報を確認してみましょう。

.bashrc へ http_proxy, https_proxy を設定する

プロキシの環境設定として利用される http_proxy, https_proxy, ftp_proxy を export で入れていきます。

デフォルトのシェルは bash なので、.bashrc へ以下を設定していきます。

  1. ユーザの利用シェルを確認
    $ env |grep sh
    SHELL=/bin/bash
    
  2. ~/.bashrc に以下を追記
    $ vi ~/.bashrc:q
    export https_proxy="http://username:password@your.proxy.address:proxy.port/"
    export http_proxy="http://username:password@your.proxy.address:proxy.port/"
    export ftp_proxy="http://username:password@your.proxy.address:proxy.port/"
    
  3. .bashrc の読み込み source ~/.bashrc
  4. セットしたプロキシ環境変数の確認
    $ env |grep proxy
    ftp_proxy=http://username:password@your.proxy.address:proxy.port/
    https_proxy=http://username:password@your.proxy.address:proxy.port/
    http_proxy=http://username:password@your.proxy.address:proxy.port/
    
  5. wget で動作確認
    $ wget https://yahoo.co.jp | more
    --2020-10-08 01:06:30--  https://yahoo.co.jp/
    Resolving your.proxy.address:proxy.port... 172.XX.XXX.1
    Connecting to your.proxy.address:proxy.port.. connected.
    Proxy request sent, awaiting response... 301 Redirect
    Location: https://www.yahoo.co.jp/ [following]
    --2020-10-08 01:06:30--  https://www.yahoo.co.jp/
    Connecting to your.proxy.address:proxy.port... connected.
    Proxy request sent, awaiting response... 200 OK
    Length: unspecified [text/html]
    Saving to: ‘index.html’

    index.html [ <=> ] 37.79K --.-KB/s in 0.05s

    2020-10-08 01:06:30 (725 KB/s) - ‘index.html’ saved [38698]

    1. yahoo.co.jp から wget できました。

      • username:プロキシ認証のユーザ名
      • password:プロキシ認証のパスワード
      • your.proxy.address:プロキシサーバの URL
      • proxy.port:プロキシのポート番号

      Ubuntu 20.04 LTS における apt-get コマンドのプロキシ設定

      1. /etc/apt/apt.conf.d/ 配下に設定値を記載した読み込みファイルを新規に作成します
        $ sudo vi /etc/apt/apt.conf.d/30proxy
        Acquire::http { Proxy "http://username:password@your.proxy.address:proxy.port/"; };
        Acquire::https { Proxy "http://username:password@your.proxy.address:proxy.port/"; };
        
      2. apt-get update でパッケージ管理DBを最新化する(パッケージ自体は更新されません)
        $ sudo apt-get update
        [sudo] password for user:
        Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
        Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]
        Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB]
        Get:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
        Ign:5 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages
        Get:6 http://archive.ubuntu.com/ubuntu focal/universe Translation-en [5124 kB]
        Get:7 http://archive.ubuntu.com/ubuntu focal/universe amd64 c-n-f Metadata [265 kB]
        Get:8 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [144 kB]
        Get:9 http://archive.ubuntu.com/ubuntu focal/multiverse Translation-en [104 kB]
        Get:10 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 c-n-f Metadata [9136 B]
        Ign:5 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages
        Ign:5 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages
        Ign:5 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages
        Ign:5 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages
        Err:5 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages
          Connection failed [IP: 172.19.246.1 86]
        Fetched 317 kB in 4min 4s (1298 B/s)
        Reading package lists... Done
        

      問題なく apt-get コマンドが proxy を通過することが確認できました。

      Ubuntu 20.04 LTS における curl コマンドのプロキシ設定

      curl コマンドは 3 通りのプロキシ設定があります。 が、上記で .bashrc にプロキシ設定をしておけば OK です。

      • おすすめ:~/.bashrcに設定

      export http_proxy=http://taro:password@192.168.1.1:8080
      export https_proxy=http://taro:password@192.168.1.1:8080
      
      ファイル修正後 source ~/.bashrc をお忘れなく。

      • コマンドに直接指定

      $ curl www.yahoo.co.jp -x http://taro:password@192.168.1.1:8080 
      

      • ~/.curlrcに設定 curl だけにプロキシを設定したい時に使います。

      proxy=http://taro:password@192.168.1.1:8080
      

      ファイル修正後 source ~/.curlrc をお忘れなく。