LoginSignup
13
8

More than 3 years have passed since last update.

コマンドラインで ubuntu の apt の取得先をミラーに変更する (take2)

Last updated at Posted at 2020-04-12

コマンドラインで ubuntu の apt の取得先をミラーに変更する (take2)

前回の記事 の記事でコマンドラインでミラーサイトの URL を指定する方法を記載した。

Dockerfile に適用しようと思ったが、DockerHub で自動ビルドするときにはおそらくアメリカのサーバーでビルドされるので、日本のサイトを明示的に指定すると効率が悪い。

改良版

http://mirrors.ubuntu.com/mirrors.txt のリストを使う場合

sudo sed -i.bak -r 's!(deb|deb-src) \S+!\1 mirror://mirrors.ubuntu.com/mirrors.txt!' /etc/apt/sources.list

※ (apt 1.6 以降なら) ローカル版と統一性を合わせて mirror://mirror+http:// とすることもできる。

sudo sed -i.bak -r 's!(deb|deb-src) \S+!\1 mirror+http://mirrors.ubuntu.com/mirrors.txt!' /etc/apt/sources.list

ローカルの /etc/apt/mirrorlist.txt のリストを使う場合

sudo sed -i.bak -r 's!(deb|deb-src) \S+!\1 mirror+file:/etc/apt/mirrorlist.txt!' /etc/apt/sources.list

前回の記事 の記事のように固定で書き換える場合

※ 正規表現を改善

sudo sed -i.bak -r 's!(deb|deb-src) \S+!\1 http://ftp.riken.jp/Linux/ubuntu/!' /etc/apt/sources.list

解説

https://blog.amedama.jp/entry/2019/09/11/234050 とかを見ると明示的にミラーサーバーの URL を指定するのではなく mirror://mirrors.ubuntu.com/mirrors.txt を指定するといいと書いてある。

APT-TRANSPORT-MIRR(1) を見るとmirror://mirrors.ubuntu.com/mirrors.txt も含めて何を指定すればいいのかの解説がある。

ローカルのミラー一覧の場合

例えば以下のような内容のファイルが /etc/apt/mirrorlist.txt にあったとする。

http://ftp.de.debian.org/debian/
http://ftp.us.debian.org/debian/
http://deb.debian.org/debian/

この場合、以下のように指定すると /etc/apt/mirrorlist.txt のファイルに書かれている内容をもとにミラーサイトを見つけてくれる。

deb mirror+file:/etc/apt/mirrorlist.txt buster main

http で取得できる場所にあるミラー一覧の場合

例えば http://mirrors.ubuntu.com/mirrors.txt に同様にミラーサイトのリストが書かれていたとする。
以下のように指定すると、http://mirrors.ubuntu.com/mirrors.txt の内容をもとにミラーサイトを見つけてくれる。

deb mirror://mirrors.ubuntu.com/mirrors.txt buster main

実例 (ローカルの /etc/apt/mirrorlist.txt を使う場合)

/etc/apt/mirrorlist.txt の中身

test@ubuntu:~$ cat /etc/apt/mirrorlist.txt
http://ubuntu-ashisuto.ubuntulinux.jp/ubuntu/
http://linux.yz.yamagata-u.ac.jp/ubuntu/
http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu/
http://ftp.jaist.ac.jp/pub/Linux/ubuntu/
http://www.ftp.ne.jp/Linux/packages/ubuntu/archive/
http://ubuntutym.u-toyama.ac.jp/ubuntu/
http://ftp.riken.jp/Linux/ubuntu/

http://mirrors.ubuntu.com/mirrors.txt の最後の行の http://archive.ubuntu.com/ubuntu/ を削除したもの。

変換コマンド

/etc/apt/sources.listsed で書き換える

sudo sed -i.bak -r 's!(deb|deb-src) \S+!\1 mirror+file:/etc/apt/mirrorlist.txt!' /etc/apt/sources.list

apt update の実行例

※ ログで http://archive.ubuntu.com/ubuntu/ が含まれない。

test@ubuntu:~$ sudo apt update -y
Get:1 file:/etc/apt/mirrorlist.txt Mirrorlist [298 B]
Get:2 http://www.ftp.ne.jp/Linux/packages/ubuntu/archive focal InRelease [265 kB]
Get:3 http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu focal-updates InRelease [89.1 kB]                                               
Get:5 http://ftp.jaist.ac.jp/pub/Linux/ubuntu focal-security InRelease [97.9 kB]                                                              
Get:6 http://www.ftp.ne.jp/Linux/packages/ubuntu/archive focal/main i386 Packages [720 kB]                                                    
Get:9 http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu focal/main amd64 c-n-f Metadata [29.2 kB]                             
Get:10 http://ftp.jaist.ac.jp/pub/Linux/ubuntu focal/restricted amd64 Packages [22.8 kB]                                                        
Get:4 http://ubuntutym.u-toyama.ac.jp/ubuntu focal-backports InRelease [89.2 kB]                                                                
Get:7 http://linux.yz.yamagata-u.ac.jp/ubuntu focal/main amd64 Packages [978 kB]                                                                
Get:11 http://ftp.jaist.ac.jp/pub/Linux/ubuntu focal/restricted i386 Packages [8,044 B]                                                         
Get:12 http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu focal/restricted Translation-en [6,392 B]                                                     
Get:15 http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu focal/universe amd64 Packages [8,600 kB]                                                      
Get:13 http://ftp.jaist.ac.jp/pub/Linux/ubuntu focal/restricted amd64 c-n-f Metadata [380 B]                                                    
Get:25 http://ftp.riken.jp/Linux/ubuntu focal-updates/multiverse amd64 c-n-f Metadata [116 B]                                                   
Get:23 http://ftp.jaist.ac.jp/pub/Linux/ubuntu focal-updates/restricted amd64 c-n-f Metadata [116 B]                                            
Get:33 http://ftp.jaist.ac.jp/pub/Linux/ubuntu focal-backports/multiverse amd64 c-n-f Metadata [116 B]                                          
Get:32 http://ftp.riken.jp/Linux/ubuntu focal-backports/universe amd64 c-n-f Metadata [116 B]                                                   
Get:8 http://ubuntu-ashisuto.ubuntulinux.jp/ubuntu focal/main Translation-en [503 kB]                                                           
Get:20 http://www.ftp.ne.jp/Linux/packages/ubuntu/archive focal/multiverse Translation-en [105 kB]                                              
Get:30 http://www.ftp.ne.jp/Linux/packages/ubuntu/archive focal-backports/main amd64 c-n-f Metadata [112 B]                                     
Get:21 http://ubuntu-ashisuto.ubuntulinux.jp/ubuntu focal/multiverse amd64 c-n-f Metadata [9,164 B]                                             
Get:22 http://ubuntu-ashisuto.ubuntulinux.jp/ubuntu focal-updates/main amd64 c-n-f Metadata [108 B]       
Get:26 http://ubuntu-ashisuto.ubuntulinux.jp/ubuntu focal-security/main amd64 c-n-f Metadata [108 B]      
Get:28 http://ubuntu-ashisuto.ubuntulinux.jp/ubuntu focal-security/universe amd64 c-n-f Metadata [112 B]  
Get:14 http://linux.yz.yamagata-u.ac.jp/ubuntu focal/universe i386 Packages [4,625 kB]                    
Get:17 http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu focal/universe amd64 c-n-f Metadata [264 kB]                               
Get:29 http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu focal-security/multiverse amd64 c-n-f Metadata [116 B]            
Get:16 http://ubuntutym.u-toyama.ac.jp/ubuntu focal/universe Translation-en [5,131 kB]                              
Get:19 http://linux.yz.yamagata-u.ac.jp/ubuntu focal/multiverse i386 Packages [74.8 kB]                
Get:27 http://linux.yz.yamagata-u.ac.jp/ubuntu focal-security/restricted amd64 c-n-f Metadata [116 B]
Get:18 http://ubuntutym.u-toyama.ac.jp/ubuntu focal/multiverse amd64 Packages [144 kB]                 
Get:24 http://ubuntutym.u-toyama.ac.jp/ubuntu focal-updates/universe amd64 c-n-f Metadata [112 B]
Get:31 http://ubuntutym.u-toyama.ac.jp/ubuntu focal-backports/restricted amd64 c-n-f Metadata [116 B]
Fetched 21.8 MB in 3s (8,497 kB/s)               
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.

実例 (http で取得できる場所にあるミラー一覧の場合)

変換コマンド

/etc/apt/sources.listsed で書き換える

sudo sed -i.bak -r 's!(deb|deb-src) \S+!\1 mirror://mirrors.ubuntu.com/mirrors.txt!' /etc/apt/sources.list

参考の変換コマンド(apt 1.6 以降の場合)

http://manpages.ubuntu.com/manpages/eoan/en/man1/apt-transport-mirror.1.html に以下の注釈がある。

Note that since apt 1.6 the use of mirror+http should be preferred over mirror for uniformity. The functionality is the same.

※ (apt 1.6 以降なら) ローカル版と統一性を合わせて mirror://mirror+http:// とすることもできる。

sudo sed -i.bak -r 's!(deb|deb-src) \S+!\1 mirror+http://mirrors.ubuntu.com/mirrors.txt!' /etc/apt/sources.list

http://mirrors.ubuntu.com/mirrors.txt の中身

test@ubuntu:~$ wget http://mirrors.ubuntu.com/mirrors.txt
test@ubuntu:~$ cat mirrors.txt 
http://ubuntu-ashisuto.ubuntulinux.jp/ubuntu/
http://linux.yz.yamagata-u.ac.jp/ubuntu/
http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu/
http://ftp.jaist.ac.jp/pub/Linux/ubuntu/
http://www.ftp.ne.jp/Linux/packages/ubuntu/archive/
http://ubuntutym.u-toyama.ac.jp/ubuntu/
http://ftp.riken.jp/Linux/ubuntu/
http://archive.ubuntu.com/ubuntu/

apt update の実行例

test@ubuntu:~$ sudo apt update -y
Get:1 http://mirrors.ubuntu.com/mirrors.txt Mirrorlist [331 B]
Get:4 http://ftp.jaist.ac.jp/pub/Linux/ubuntu focal-backports InRelease [89.2 kB]
Get:7 http://www.ftp.ne.jp/Linux/packages/ubuntu/archive focal-backports/restricted amd64 c-n-f Metadata [116 B]
Get:9 http://www.ftp.ne.jp/Linux/packages/ubuntu/archive focal-backports/multiverse amd64 c-n-f Metadata [116 B]                                
Get:3 http://ftp.riken.jp/Linux/ubuntu focal-updates InRelease [89.1 kB]                                                                        
Get:8 http://linux.yz.yamagata-u.ac.jp/ubuntu focal-backports/universe amd64 c-n-f Metadata [116 B]                                             
Get:11 http://linux.yz.yamagata-u.ac.jp/ubuntu focal-updates/restricted amd64 c-n-f Metadata [116 B]               
Get:10 http://ftp.jaist.ac.jp/pub/Linux/ubuntu focal-updates/main amd64 c-n-f Metadata [108 B]                                                  
Get:13 http://ftp.jaist.ac.jp/pub/Linux/ubuntu focal-updates/multiverse amd64 c-n-f Metadata [116 B]                                            
Get:2 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]                                                 
Get:5 http://ftp.riken.jp/Linux/ubuntu focal-security InRelease [97.9 kB]                           
Get:6 http://ubuntutym.u-toyama.ac.jp/ubuntu focal-backports/main amd64 c-n-f Metadata [112 B]              
Get:12 http://ubuntu-ashisuto.ubuntulinux.jp/ubuntu focal-updates/universe amd64 c-n-f Metadata [112 B]
Get:17 http://ubuntu-ashisuto.ubuntulinux.jp/ubuntu focal-security/multiverse amd64 c-n-f Metadata [116 B]
Get:19 http://www.ftp.ne.jp/Linux/packages/ubuntu/archive focal/main i386 Packages [720 kB]
Get:20 http://ubuntutym.u-toyama.ac.jp/ubuntu focal/main Translation-en [503 kB]                                                                
Get:30 http://ubuntu-ashisuto.ubuntulinux.jp/ubuntu focal/multiverse amd64 Packages [144 kB]                                                    
Get:31 http://ftp.jaist.ac.jp/pub/Linux/ubuntu focal/multiverse i386 Packages [74.8 kB]                                                         
Get:15 http://archive.ubuntu.com/ubuntu focal-security/restricted amd64 c-n-f Metadata [116 B]                                                  
Get:22 http://ubuntutym.u-toyama.ac.jp/ubuntu focal/restricted i386 Packages [8,044 B]                                                          
Get:23 http://ubuntutym.u-toyama.ac.jp/ubuntu focal/restricted amd64 Packages [22.8 kB]                                                         
Get:24 http://ubuntutym.u-toyama.ac.jp/ubuntu focal/restricted Translation-en [6,392 B]                                                         
Get:27 http://ubuntutym.u-toyama.ac.jp/ubuntu focal/universe amd64 Packages [8,600 kB]                                                          
Get:21 http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu focal/main amd64 c-n-f Metadata [29.2 kB]                                                     
Get:33 http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu focal/multiverse amd64 c-n-f Metadata [9,164 B]                                               
Get:18 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [978 kB]                                                                      
Get:25 http://www.ftp.ne.jp/Linux/packages/ubuntu/archive focal/restricted amd64 c-n-f Metadata [380 B]          
Get:32 http://www.ftp.ne.jp/Linux/packages/ubuntu/archive focal/multiverse Translation-en [105 kB]
Get:14 http://ftp.riken.jp/Linux/ubuntu focal-security/main amd64 c-n-f Metadata [108 B]                               
Get:16 http://ftp.riken.jp/Linux/ubuntu focal-security/universe amd64 c-n-f Metadata [112 B]                           
Ign:29 http://ftp.riken.jp/Linux/ubuntu focal/universe amd64 c-n-f Metadata
Get:29 http://ubuntutym.u-toyama.ac.jp/ubuntu focal/universe amd64 c-n-f Metadata [264 kB]
Get:26 http://archive.ubuntu.com/ubuntu focal/universe i386 Packages [4,625 kB]                                                                 
Get:28 http://archive.ubuntu.com/ubuntu focal/universe Translation-en [5,131 kB]                                                                
Fetched 21.8 MB in 44s (493 kB/s)                                                                                                               
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.
13
8
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
13
8