sudo apt updateが失敗する
Err:1 http://archive.ubuntu.com/ubuntu focal InRelease
Temporary failure resolving 'archive.ubuntu.com'
Err:2 http://security.ubuntu.com/ubuntu focal-security InRelease
Temporary failure resolving 'security.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Temporary failure resolving 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Temporary failure resolving 'archive.ubuntu.com'
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-updates/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-backports/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/focal-security/InRelease Temporary failure resolving 'security.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.
これは通常、DNSの解決に問題があることを示しています。つまり、ネームサーバーが正しく機能していない可能性があります。この問題を解決するためには、ネームサーバーの設定を見直す必要があります。
解決法(ネームサーバーの設定を変える)
UbuntuのDNS設定は、/etc/resolv.conf ファイルに保存されています。このファイルの内容を確認するには、cat /etc/resolv.conf コマンドを実行します。
問題が現在のDNSサーバーにある場合、別のDNSサーバーに切り替えると解決することがあります。ここでは、Google Public DNS(8.8.8.8と8.8.4.4)に切り替える方法を示します。
ただし、直接 /etc/resolv.conf を編集するのは推奨されていません。なぜなら、このファイルはシステムやネットワークの状態に応じて自動的に生成・更新されるため、直接編集した設定は上書きされてしまう可能性があるからです。
そこで、以下のように /etc/systemd/resolved.conf ファイルを編集してDNS設定を変更します。
まず、sudo nano /etc/systemd/resolved.conf コマンドでファイルを開きます。次に、DNS= の行を見つけ、Google Public DNSのIPアドレスを設定します。
/etc/systemd/resolved.conf
DNS=8.8.8.8 8.8.4.4
最後に、設定を保存し、ファイルを閉じます。
次に、systemd-resolvedサービスを再起動して設定を反映します。それには、sudo systemctl restart systemd-resolved コマンドを実行します。
これで、UbuntuのDNS設定がGoogle Public DNSに切り替わり、sudo apt update の実行が成功するはずです。なお、これらの手順はシステムや環境によって異なる場合がありますので、詳細はシステムのドキュメンテーションを参照してください。