[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

EOLした CentOS 6 で yum を使えるようにする【リポジトリ変更】

皆様こんにちは。
ネットワーク回線は 1Gbps 以上がデフォルトになってほしいと願う、システムソリューション部所属の なか です。

CentOS 6 環境といった古い環境は移行を強く推奨されていますが、様々な事情により残ってしまっている事があります。

そういった環境だと、更新や追加のために使おうとした「yum」が使えない(使うと「エラーが起きる」)という状況に遭遇しやすいです。

今回は、その状況を解決するための「CentOS 6 で yum が使えるようにする(リポジトリ変更)」手順の説明・解説をしていきます。

前置き

  • CentOS 6 での運用を推奨している訳ではございません。
    あくまでもやむを得ない理由で存在する CentO S6 への応急対応となります。
  • Baseリポジトリの利用ではなく、「yum」を使える状態にする事が目的です。
  • デフォルトのリポジトリである事が原因での対応手順となります。

実行環境

■ Windows 環境
OS : Windows 11 Pro(バージョン:23H2)
言語設定 : 日本語に変更

■ 検証 CentOS 6 環境(Vagrant+VirtualBox)
OS:CentOS 6.9(bento/centos-6.9)
Vagrant : 2.4.1
VirtualBox : 7.0.18 r162988(Qt5.15.2)
IP : 192.168.33.15
ホスト名 : targetnode
ユーザー名 : vagrant

リポジトリ原因 - Error:Cannot find a valid baseurl for repo: base

$ yum info
Loaded plugins: fastestmirror
Determining fastest mirrors
YumRepo Error: All mirror URLs are not using ftp, http[s] or file.
Eg. Invalid release/repo/arch combination/
removing mirrorlist with no valid mirrors: /var/cache/yum/x86_64/6/base/mirrorlist.txt
Error: Cannot find a valid baseurl for repo: base

「yum」を使用とした際に表示されるのは、上記「Error: Cannot find a valid baseurl for repo: base」のエラーが多いかと思います。

これは EOL に伴い、デフォルトで登録されていたリポジトリが全て閉鎖(終了)しました。
そのため、「利用できるリポジトリが存在しない」ために発生するエラーとなります。

解消するために利用できる、別の CentOS 6 用リポジトリを設定する必要があります。

作業手順

0. yum のエラー確認

問題がない状態なのに作業してしまわないように、念の為に確認しておきます。

$ yum info
Loaded plugins: fastestmirror
Determining fastest mirrors
YumRepo Error: All mirror URLs are not using ftp, http[s] or file.
Eg. Invalid release/repo/arch combination/
removing mirrorlist with no valid mirrors: /var/tmp/yum-vagrant-BcYfuY/x86_64/6/base/mirrorlist.txt
Error: Cannot find a valid baseurl for repo: base

Error: Cannot find a valid baseurl for repo: base

当該エラーが確認できたので、作業を始めていきましょう。

1. リポジトリのバックアップ

リポジトリの変更作業を加える前に、日付付きでバックアップをとります。

内容を全て削除した状態で保存してしまう可能性もあり得ますので、取っておきましょう。

$ cp -p /etc/yum.repos.d/CentOS-Base.repo /tmp/CentOS-Base.repo.`date +%Y%m%d`
$ ls -l /tmp | grep CentOS-Base.repo
-rw-r--r--. 1 vagrant vagrant 1991 Mar 28 2017 CentOS-Base.repo.2024xxxx

(一時的なバックアップなので /tmp に作成しています)

TIps : CentOS-Base.repo.`date +%Y%m%d`

`(バッククオート)を使い、コマンドの結果を文字列として扱います。
その中で、日付時刻の表示が行える data コマンドをフォーマットを指定(年・月・日)して使用しています。

これにより、カレンダーを見なくても、コマンド実行時の"年月日"を末尾に付けたファイル名で、バックアップが取れるので便利です。

2.リポジトリの変更

○ 2-1. デフォルト状態確認

リポジトリはデフォルトのままだと、下記の状態になっているはずです。

$ less /etc/yum.repos.d/CentOS-Base.repo
~省略~
[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
~省略~
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
~省略~
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
~省略~
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
~省略~
[contrib]
name=CentOS-$releasever - Contrib
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/contrib/$basearch/
~省略~

○ 2-2. ミラーリストは機能していないため無効化(コメントアウト)

「mirrorlist=http://mirrorlist.centos.org」 の部分をコメントアウトしますが、複数箇所の編集はミスしやすいです。

そのため sedコマンドで一括置換します。

$ sudo sed -i -e "s/^mirrorlist=http:\/\/mirrorlist.centos.org/#mirrorlist=http:\/\/mirrorlist.centos.org/g" /etc/yum.repos.d/CentOS-Base.repo

○ 2-3. baseurlを書き換えて有効化(コメントイン)

「#baseurl=」のコメントを外し、IIJ社が提供して下さっている CentOS 用リポジトリを使用するように指定します。
※ 2024/07 時点で利用可能なリポジトリを指定しています。今後、当該のリポジトリが終了した際は別の物を指定してください。

こちらも sedコマンドで一括置換します。

$ sudo sed -i -e "s/^#baseurl=http:\/\/mirror.centos.org\/centos\/\$releasever/baseurl=http:\/\/ftp.iij.ad.jp\/pub\/linux\/centos-vault\/\$releasever/g" /etc/yum.repos.d/CentOS-Base.repo
手動で書き換えたい場合は、「#baseurl=」の箇所を、下記のように変更してください。
#baseurl=http://mirror.centos.org/centos/$releasever
⇓
baseurl=http://ftp.iij.ad.jp/pub/linux/centos-vault/$releasever

○ 2-4. 変更 / 差分確認

編集した repo とバックアップしたrepoの差分を確認して、意図した変更になっているか確認しましょう。

$ diff /etc/yum.repos.d/CentOS-Base.repo /tmp/CentOS-Base.repo.`date +%Y%m%d`
15,16c15,16
< #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
< baseurl=http://ftp.iij.ad.jp/pub/linux/centos-vault/$releasever/os/$basearch/
---
> mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
> #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
~省略~

上記のような形で、変更後と変更前の差分が取れていれば問題ありません。

○ 2-5. 変数「$releasever」を指定する

マイナーリリースを固定するため、「releasever」変数にバージョンを記載します。

固定させるために変数ファイルを作成する形式で行います。
(既に指定 / 固定されている場合はこの対応は不要です)

$ ls -l /etc/yum/vars/ | grep releasever ※作成されていない事を確認
$ sudo sh -c 'echo "6.9" > /etc/yum/vars/releasever' ※作成&追記
 ※ rootで実行する場合は、echo "6.9" > /etc/yum/vars/releasever で問題ありません。

$ cat /etc/yum/vars/releasever ※記載されている事を確認
6.9

記述した 6.9 が表示されているので問題ありません。

3. yumの動作確認とキャッシュ削除

○ 3-1. yum info で yum が利用できる事を確認

php のパッケージ情報を表示してみましょう。

$ yum info php
Loaded plugins: fastestmirror
Determining fastest mirrors
Available Packages
Name : php
Arch : x86_64
Version : 5.3.3
Release : 49.el6
Size : 1.1 M
Repo : base
Summary : PHP scripting language for creating dynamic web sites
URL : http://www.php.net/
License : PHP
Description : PHP is an HTML-embedded scripting language. PHP attempts to make it
~省略~

上記のようにバージョン等々が表示されていれば、問題なく yum が動作しています。

○ 3-2. yumのキャッシュ削除

必須ではありませんが、yum を変更したのでキャッシュを消しておきます。

$ sudo yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up Everything
Cleaning up list of fastest mirrors

完了

以上で「yum」エラー解消のためのリポジトリ変更作業は完了となります。

最後に

この作業そのものは難しくはありません。
ただ「yum」を使いたい時にこのエラーで足止めをくらいやすいので、CentOS 6 環境では事前に対応しておくと楽です。

この記事を読んだ方に、多少でも役に立つ知識・情報となれれば幸いです。
ここまで読んで頂きありがとうございました。

参考資料

CentOS 6 リポジトリ アドレスを変更するにはどうすればよいですか? | Alibaba Cloud
https://www.alibabacloud.com/help/en/ecs/user-guide/change-the-centos-6-source-address

8.2.3. パッケージ情報の表示 |  RHEL6
https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/6/html/deployment_guide/sec-displaying_package_information

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
1
読み込み中...
1 票, 平均: 1.00 / 11
225
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

inside

Beyond mid-career in 2022 Belongs to
the System Solutions Department
LPIC-3 I have a 304 and AWS SAA I only
have three choices for regular drinks: milk, cola, and black tea.