1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Squidを使用して閉域環境からYUM/DNFパッケージを取得する

Last updated at Posted at 2025-08-15

今回のやりたいこと

  1. 閉域環境(直接インターネットに出られない)に配置したサーバでパッケージをインストールしたい。
  2. インターネット接続可能なサーバ上の Squid(プロキシサーバ) を経由して外部のリポジトリにアクセスしたい。
  3. DNF/YUM のリポジトリ設定にプロキシを指定して、パッケージ取得(ここではhttpd例に取得)を可能にする。

環境

  • OS:Oracle Linux 9
  • CPU:1 OCPU(2vCPU)
  • メモリ:8GB
  • ディスク:50GB
    ※プロキシとクライアントは同じ環境・スペックで検証しています。

今回はOracle Cloud Infrastructure(OCI)上で検証しましたが、設定自体は他のクラウド環境やオンプレミス環境でも同様に適用可能です。

プロキシ側の設定

Squidとは、Webアクセスを中継およびキャッシュするプロキシサーバ用のミドルウェアです。
以下コマンドでSquidのインストールを行います。

sudo dnf install -y squid
実行結果
[opc@proxy ~]$ sudo dnf install squid
Oracle Linux 9 KVM Utilities (x86_64)                                                                            1.1 MB/s | 126 kB     00:00    
Latest RHCK with fixes from Oracle for Oracle Linux 9 (x86_64)                                                    60 MB/s |  73 MB     00:01    
Ksplice for Oracle Linux 9 (x86_64)                                                                              251 kB/s | 3.5 kB     00:00    
Ksplice for Oracle Linux 9 (x86_64)                                                                               57 MB/s | 7.8 MB     00:00    
Ksplice aware userspace packages for Oracle Linux 9 (x86_64)                                                      13 MB/s | 2.5 MB     00:00    
Oracle Linux 9 OCI Included Packages (x86_64)                                                                    264 kB/s | 3.5 kB     00:00    
Oracle Linux 9 EPEL Packages for Development (x86_64)                                                             48 MB/s |  30 MB     00:00    
Oracle Linux 9 BaseOS Latest (x86_64)                                                                            184 kB/s | 4.2 kB     00:00    
Oracle Linux 9 Application Stream Packages (x86_64)                                                              200 kB/s | 4.5 kB     00:00    
Oracle Linux 9 BaseOS GA (x86_64)                                                                                 30 MB/s | 2.1 MB     00:00    
Oracle Linux 9.1 BaseOS (x86_64)                                                                                  36 MB/s | 2.8 MB     00:00    
Oracle Linux 9.2 BaseOS (x86_64)                                                                                  41 MB/s | 3.1 MB     00:00    
Oracle Linux 9.3 BaseOS (x86_64)                                                                                  40 MB/s | 3.6 MB     00:00    
Oracle Linux 9.4 BaseOS (x86_64)                                                                                  45 MB/s | 4.3 MB     00:00    
Oracle Linux 9.5 BaseOS (x86_64)                                                                                  13 MB/s | 4.5 MB     00:00    
Oracle Linux 9.6 BaseOS (x86_64)                                                                                  42 MB/s |  10 MB     00:00    
Oracle Linux 9 Addons (x86_64)                                                                                   224 kB/s | 3.5 kB     00:00    
Oracle Linux 9 CodeReady Builder (x86_64) - (Unsupported)                                                         66 MB/s |  14 MB     00:00    
Oracle Linux 9 Distro Builder (x86_64) - (Unsupported)                                                            13 MB/s | 1.7 MB     00:00    
Oracle Linux 9 Update 3 (x86_64) Security Validations                                                             36 MB/s | 4.0 MB     00:00    
Oracle Linux 9 Development Packages (x86_64)                                                                      59 MB/s | 169 MB     00:02    
Developer Preview of UEK Release 7 (x86_64)                                                                       74 MB/s | 117 MB     00:01    
Oracle Linux 9 KVM Utilities for Development and test (x86_64)                                                   114 kB/s | 6.9 kB     00:00    
Oracle Linux 9 UEK Release 7 (x86_64)                                                                             78 MB/s |  76 MB     00:00    
Oracle Linux 9 (x86_64) RDMA                                                                                     641 kB/s |  73 kB     00:00    
Oracle Linux 9 UEK Release 8 (x86_64)                                                                            225 kB/s | 3.5 kB     00:00    
依存関係が解決しました。
=================================================================================================================================================
 パッケージ                     アーキテクチャー              バージョン                              リポジトリー                         サイズ
=================================================================================================================================================
インストール:
 squid                          x86_64                        7:5.5-19.el9_6.1                        ol9_appstream                        5.5 M
依存関係のインストール:
 libecap                        x86_64                        1.0.1-10.el9                            ol9_appstream                         30 k

トランザクションの概要
=================================================================================================================================================
インストール  2 パッケージ

ダウンロードサイズの合計: 5.5 M
インストール後のサイズ: 11 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(1/2): libecap-1.0.1-10.el9.x86_64.rpm                                                                           984 kB/s |  30 kB     00:00    
(2/2): squid-5.5-19.el9_6.1.x86_64.rpm                                                                            16 MB/s | 5.5 MB     00:00    
-------------------------------------------------------------------------------------------------------------------------------------------------
合計                                                                                                              16 MB/s | 5.5 MB     00:00     
トランザクションを確認しています
トランザクションの確認に成功しました。
トランザクションをテストしています
トランザクションのテストに成功しました。
トランザクションを実行しています
  scriptletの実行中: squid-7:5.5-19.el9_6.1.x86_64                                                                                           1/1 
  準備中           :                                                                                                                         1/1 
  インストール中   : libecap-1.0.1-10.el9.x86_64                                                                                             1/2 
  scriptletの実行中: squid-7:5.5-19.el9_6.1.x86_64                                                                                           2/2 
  インストール中   : squid-7:5.5-19.el9_6.1.x86_64                                                                                           2/2 
  scriptletの実行中: squid-7:5.5-19.el9_6.1.x86_64                                                                                           2/2 
  検証中           : libecap-1.0.1-10.el9.x86_64                                                                                             1/2 
  検証中           : squid-7:5.5-19.el9_6.1.x86_64                                                                                           2/2 

インストール済み:
  libecap-1.0.1-10.el9.x86_64                                            squid-7:5.5-19.el9_6.1.x86_64                                           

完了しました!

インストール完了後、squidの自動起動の有効化を行います。

sudo systemctl enable --now squid
実行結果
[opc@proxy ~]$ sudo systemctl enable --now squid
Created symlink /etc/systemd/system/multi-user.target.wants/squid.service → /usr/lib/systemd/system/squid.service.
[opc@proxy ~]$  

必要に応じてSquidが使用するポート(TCP/3128)のファイアウォールの許可を行います。

sudo firewall-cmd --permanent --add-port=3128/tcp
sudo firewall-cmd --reload

Squidの設定ファイル解説

ここでは、Squid の設定ファイルである/etc/squid/squid.confの内容と意味を解説します。
以下は実際の設定例です。

/etc/squid/squid.conf
#
# Recommended minimum configuration:
#

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed

acl localnet src 0.0.0.1-0.255.255.255  # RFC 1122 "this" network (LAN)
acl localnet src 10.0.0.0/8             # RFC 1918 local private network (LAN)
acl localnet src 100.64.0.0/10          # RFC 6598 shared address space (CGN)
acl localnet src 169.254.0.0/16         # RFC 3927 link-local (directly plugged) machines
acl localnet src 172.16.0.0/12          # RFC 1918 local private network (LAN)
acl localnet src 192.168.0.0/16         # RFC 1918 local private network (LAN)
acl localnet src fc00::/7               # RFC 4193 local private network range
acl localnet src fe80::/10              # RFC 4291 link-local (directly plugged) machines

acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http

#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports
http_access deny CONNECT !SSL_ports

# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet
http_access allow localhost

# And finally deny all other access to this proxy
http_access deny all

# Squid normally listens to port 3128
http_port 3128

# Uncomment and adjust the following to add a disk cache directory.
#cache_dir ufs /var/spool/squid 100 16 256

# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid

#
# Add any of your own refresh_pattern entries above these.
#

以下はSquidのアクセス制御リスト(ACL)の定義です。
localnetという名前で「社内・閉域ネットワーク」からのアクセスをまとめて識別する設定です。

既存の設定に必要なネットワークCIDRが含まれていない場合は、適宜新しく追加する必要があります。

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
## RFC 1122で定義される特殊な範囲。"this network" と呼ばれる古い予約領域。ほぼ使われないが一応記載。
acl localnet src 0.0.0.1-0.255.255.255  # RFC 1122 "this" network (LAN)
## RFC 1918で定義されたプライベートアドレス(10.0.0.0 ~ 10.255.255.255)。
acl localnet src 10.0.0.0/8             # RFC 1918 local private network (LAN)
## RFC 6598で定義されたキャリアグレードNAT(CGN)用の共有アドレス空間。
acl localnet src 100.64.0.0/10          # RFC 6598 shared address space (CGN)
## RFC 3927で定義されたリンクローカルアドレス(DHCP失敗時などに自動割当される)。
acl localnet src 169.254.0.0/16         # RFC 3927 link-local (directly plugged) machines
## RFC 1918で定義されたプライベートアドレス(172.16.0.0 ~ 172.31.255.255)。
acl localnet src 172.16.0.0/12          # RFC 1918 local private network (LAN)
## RFC 1918で定義されたプライベートアドレス(192.168.0.0 ~ 192.168.255.255)。
acl localnet src 192.168.0.0/16         # RFC 1918 local private network (LAN)
## RFC 4193で定義されたIPv6のユニークローカルアドレス(IPv4のプライベートIPに相当)。
acl localnet src fc00::/7               # RFC 4193 local private network range
## RFC 4291で定義されたIPv6リンクローカルアドレス(同一リンク上で自動割当される)。
acl localnet src fe80::/10              # RFC 4291 link-local (directly plugged) machines

以下はSquid のポートに関するアクセス制御リスト(ACL)の定義です。
「プロキシが受け付ける通信の種類やポート番号を制限するための設定」です。

acl SSL_ports port 443          # https
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http

acl SSL_ports port 443を例にして、

  • 名前:SSL_ports
  • 内容:ポート 443(HTTPS の標準ポート)
  • 後の設定(例:http_access deny CONNECT !SSL_ports)で使用され、CONNECTメソッドを使ったトンネリングは基本的に443番だけ許可するという制御に使われます。

以下はSquid プロキシのアクセス制御ルールと基本設定部分です。
「どの通信を許可するか・拒否するか」を順番に決めています。

Squid は上から順にルールを評価するので、順序が非常に重要となります。

# Deny requests to certain unsafe ports
## ユーザーが怪しいポートにアクセスするのを防ぐため、ACL に含まれないポートへの通信をすべて拒否。
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports
## ユーザーが Squid を経由して任意のポートにトンネルを張るのを防ぐため、CONNECT メソッド(HTTPSトンネル作成)を許可するのは SSL_ports(例:443番)だけ使用。
http_access deny CONNECT !SSL_ports

# Only allow cachemgr access from localhost
## Squid の管理機能を外部から使われないように保護するため、管理インターフェースはローカルマシンからのみ許可、それ以外のアクセスは拒否。
http_access allow localhost manager
http_access deny manager

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
## コメントアウトされているが、有効にすると「外部からプロキシ経由で 127.0.0.1 などへのアクセスを禁止」できる。
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
## 内部LAN・ローカルだけが使えるプロキシにする設定で、localnet ACL に一致する内部ネットワークおよびプロキシ自身(localhost)からのアクセスを許可
http_access allow localnet
http_access allow localhost

# And finally deny all other access to this proxy
## その他のアクセスはすべて拒否
http_access deny all

# Squid normally listens to port 3128
## Squid はデフォルトでポート3128で待ち受ける。
http_port 3128

# Uncomment and adjust the following to add a disk cache directory.
## ディスクキャッシュ設定(コメントアウトされているので無効)
## 有効にすると /var/spool/squid に100MBのキャッシュ領域を作成し、ディレクトリを階層化して管理。
#cache_dir ufs /var/spool/squid 100 16 256

# Leave coredumps in the first cache dir
## 異常終了時のコアダンプファイルを保存する場所を指定。
coredump_dir /var/spool/squid

#
# Add any of your own refresh_pattern entries above these.
#

クライアント側の設定

プロキシ設定が完了したら、次にクライアント側の設定を行います。
必要に応じて、デフォルトで有効になっているすべてのリポジトリを無効化します。

sudo dnf config-manager --disable \*

ここではクライアントの/etc/yum.repos.dの配下に、ol9_proxy.repoを作成し、必要な設定を追加します。
今回はhttpdをインストールするため、利用するリポジトリとしてAppStreamBaseOSを指定します。

/etc/yum.repos.d/ol9_proxy.repo
[ol9_appstream_proxy]
name=ol9_appstream through proxy
baseurl=https://yum.ap-tokyo-1.oraclecloud.com/repo/OracleLinux/OL9/appstream/x86_64/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1
proxy=http://10.0.0.2:3128

[ol9_baseos_proxy]
name=ol9_baseos through proxy
baseurl=https://yum.ap-tokyo-1.oraclecloud.com/repo/OracleLinux/OL9/appstream/x86_64/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1
proxy=http://10.0.0.2:3128

以下では、作成したリポジトリ設定ファイルの各行について、それぞれの意味と役割を解説します。

[ol9_appstream_proxy]
  • リポジトリのID(内部識別名)。
  • リポジトリを有効化する場合、dnf --enablerepo=ol9_appstream_proxy のように指定できます。
name=ol9_appstream through proxy
  • リポジトリの説明。
  • dnf repolist などで表示される「人間が読む用の名前」です。
baseurl=https://yum.ap-tokyo-1.oraclecloud.com/repo/OracleLinux/OL9/appstream/x86_64/
  • 実際のパッケージを取得するURL。

このURLはOracle Linux向けの公式リポジトリを指しています。
別のOSやバージョンを利用する場合は、対応するリポジトリURLを必ず確認・指定する必要があります。

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
  • ダウンロードしたRPMパッケージの署名を検証するための GPG鍵の場所を示します。
gpgcheck=1
  • ダウンロードしたパッケージが正規のものか確認するため、パッケージ検証を有効化(1=有効、0=無効)します。
enabled=1
  • リポジトリを有効化(1=有効、0=無効)します。
proxy=http://10.0.0.2:3128
  • このリポジトリにアクセスする際、指定のプロキシを経由する設定します。
  • ここでは10.0.0.2:3128のHTTPプロキシサーバを使用します。

10.0.0.2は今回の検証で使用したプロキシサーバのIPアドレスです。
実際の環境に合わせて適切な IP アドレスに置き換えてください。


リポジトリ設定ファイル(今回は/etc/yum.repos.d/ol9_proxy.repo)を作成した後、httpdをインストールして動作確認を行います。

sudo dnf install httpd

インストール時に表示されるリポジトリー欄に設定したリポジトリID(例:ol9_appstream_proxy)が表示されていれば、プロキシ経由でパッケージ取得が正しく行われていることを示します。

[opc@client ~]$ sudo dnf install httpd
メタデータの期限切れの最終確認: 0:52:19 前の 2025年08月15日 08時53分43秒 に実施しました。
依存関係が解決しました。
=================================================================================================================================================
 パッケージ                           アーキテクチャー         バージョン                            リポジトリー                          サイズ
=================================================================================================================================================
インストール:
 httpd                                x86_64                   2.4.62-4.0.1.el9                      ol9_appstream_proxy                    64 k
依存関係のインストール:
 apr                                  x86_64                   1.7.0-12.el9_3                        ol9_appstream_proxy                   131 k
 apr-util                             x86_64                   1.6.1-23.el9                          ol9_appstream_proxy                    99 k
 apr-util-bdb                         x86_64                   1.6.1-23.el9                          ol9_appstream_proxy                    12 k
 httpd-core                           x86_64                   2.4.62-4.0.1.el9                      ol9_appstream_proxy                   1.8 M
 httpd-filesystem                     noarch                   2.4.62-4.0.1.el9                      ol9_appstream_proxy                    11 k
 httpd-tools                          x86_64                   2.4.62-4.0.1.el9                      ol9_appstream_proxy                    93 k
 oracle-logos-httpd                   noarch                   90.4-1.0.1.el9                        ol9_baseos_proxy                       37 k
弱い依存関係のインストール:
 apr-util-openssl                     x86_64                   1.6.1-23.el9                          ol9_appstream_proxy                    14 k
 mod_http2                            x86_64                   2.0.26-4.el9                          ol9_appstream_proxy                   171 k
 mod_lua                              x86_64                   2.4.62-4.0.1.el9                      ol9_appstream_proxy                    58 k

トランザクションの概要
=================================================================================================================================================
インストール  11 パッケージ

これで、プロキシサーバ経由でのパッケージ取得環境構築に関する全手順は完了です。
設定したSquidプロキシおよびリポジトリが正常に機能していれば、閉域環境のクライアントでもインターネット上のパッケージを問題なく取得できます。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?