はしくれエンジニアもどきのメモ

情報系技術・哲学・デザインなどの勉強メモ・備忘録です。

WSL2でネットワークのドメインを解決できない場合の対処

WSL2でネットワークのドメインを解決できない場合の対処

WSL2上のLinux(ex.Ubuntu)で, ping google.comapt updateがうまく行かない場合(Temporary failure)の対処である.

どうやらDNSサーバの設定がよくなく?ドメインの解決ができなくなっているぽい.

参考:

現状(2020/07/16),原因は不明ぽいです. (/etc/resolv.confがどっかのタイミングで勝手に書き換わる?)

環境

  • Windows10 ver2004(OSビルド 19041.330)
    • WSL2
    • Docker Desktop for Windows 2.3.0.3(45519)
      • Docker Engine: 19.03.8
    • VSCode 1.47.0

エラー内容

wsl上のLinuxpingapt updateできなくなる.

pingができない

例えばgoogleへのpingが返ってこない.

> ping google.com
ping: google.com: Temporary failure in name resolution

apt updateができない

apt updateするとTemporary failureが起きる.

> sudo apt update
Err:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Temporary failure resolving 'archive.ubuntu.com'
Err:2 http://security.ubuntu.com/ubuntu bionic-security InRelease
Temporary failure resolving 'security.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Temporary failure resolving 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Temporary failure resolving 'archive.ubuntu.com' Reading package lists... Done
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic/InRelease
Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelease
Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-backports/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/bionic-security/InRelease
Temporary failure resolving 'security.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.

対処

wsl2ではDNSサーバの設定であるresolv.confが自動的に生成される. なのでこの設定を変更する.

  1. powershellからwslコマンドでログインする.
  2. `/etc/wsl.conf`で制御できるので,このファイルを作る.

    sudo vim /etc/wsl.conf
  3. 中身を次のようにする.`:wq`で保存.

    [network]
    generateResolvConf = false
  4. 別のpowershellを立ち上げてwslをシャットダウンさせる.

    wsl --shutdown
  5. WSL2を再起動して/etc/resolv.confを書き換える.

    > wsl
    > sudo vim /etc/resolv.conf
    
  6. 次に書き換える.

    nameserver 8.8.8.8

これでDNSサーバの設定ができドメイン名が解決できるようになる.