LoginSignup
55
36

More than 1 year has passed since last update.

WSLにてapt update時の『Temporary failure resolving ~』を解決する方法

Posted at

問題

wslで sudo apt update するときに、名前解決ができない問題がある。

具体的には

sudo apt update

を実行すると

Err:1 http://archive.ubuntu.com/ubuntu focal InRelease
  Temporary failure resolving 'archive.ubuntu.com'

というエラーが出る。
この記事では、これを解決する。

解決方法指針

書いてあるとおりに、名前解決ができていないので自分の環境にあったDNSサーバを /etc/resolv.conf に指定する。
いろいろな記事に記載されているのはDNSサーバとして 8.8.8.8 を指定するように書いてある。
私の環境ではこれでは解決しなかった。

これを解決するために自分の環境にあったDNSサーバを調べ、それを /etc/resolv.conf に記載する手順を書いておく。

前提

wslのネットワークの自動で生成機能は切っておきましょう。
/etc/wsl.conf をこんな感じにしておく。

username@hostname:/etc$ cat /etc/wsl.conf
[network]
generateResolvConf = false

やり方

自分の環境のDNSサーバを調べる

コマンドプロンプトで DNSサーバを調べるために以下のコマンドを叩く。

PS C:\Users\username> ipconfig.exe /all

するとDNSサーバを含む全てのネットワークが表示される。

Wireless LAN adapter Wi-Fi:

   接続固有の DNS サフィックス . . . . .:
   説明. . . . . . . . . . . . . . . . .: Intel(R) Dual Band Wireless-AC 8265
   物理アドレス. . . . . . . . . . . . .: D0-AB-D5-E3-41-D0
   DHCP 有効 . . . . . . . . . . . . . .: はい
   自動構成有効. . . . . . . . . . . . .: はい
   IPv4 アドレス . . . . . . . . . . . .: 192.168.11.10(優先)
   サブネット マスク . . . . . . . . . .: 255.255.255.0
   リース取得. . . . . . . . . . . . . .: 2021年9月13日 0:39:37
   リースの有効期限. . . . . . . . . . .: 2021年9月15日 8:29:58
   デフォルト ゲートウェイ . . . . . . .: 192.168.11.1
   DHCP サーバー . . . . . . . . . . . .: 192.168.11.1
   DNS サーバー. . . . . . . . . . . . .: 192.168.11.1
   NetBIOS over TCP/IP . . . . . . . . .: 有効

この中でDNSサーバと書いてある箇所をメモする。
上記の表示例だと 192.168.11.1 がDNSサーバのIPアドレス。

/etc/resolv.conf に書き込む

sudo sh -c "echo 'nameserver 192.168.11.1' > /etc/resolv.conf"

apt updateを実行する

ここまでやると名前解決のエラーが無くなり、解決できるはず。
具体的には以下のような表示になり、解決する。

username@hostname:/etc$ sudo apt update
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:4 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [861 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [8628 kB]
Get:7 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [166 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 c-n-f Metadata [8692 B]
Get:9 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [405 kB]
(以下略)
55
36
1

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
55
36