WSL2 Ubuntu20.04でapt-get updateが失敗する

WSL2 Ubuntu20.04でapt-get updateが失敗する

WSL2 Ubuntu20.04でapt-get updateが失敗してしまいました。

どうやらWSL2時刻ずれが原因だったようで、対処方法を紹介します。

エラー内容と解決方法

早速ですが、エラー内容と解決方法です。

エラー内容

$ sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Reading package lists... Done
E: Release file for http://security.ubuntu.com/ubuntu/dists/focal-security/InRelease is not valid yet (invalid for another 7h 52min 6s). Updates for this repository will not be applied.
E: Release file for http://archive.ubuntu.com/ubuntu/dists/focal-updates/InRelease is not valid yet (invalid for another 7h 52min 28s). Updates for this repository will not be applied.
E: Release file for http://archive.ubuntu.com/ubuntu/dists/focal-backports/InRelease is not valid yet (invalid for another 7h 53min 20s). Updates for this repository will not be applied.

apt-get updateコマンドを実行すると、~~~ is not valid yet (invalid for ...)というエラーが発生してしまいます。

修正方法

WSL2の時刻を修正します。

$ sudo hwclock -s

エラーの詳細

上記で解決した方はこれ以降読む必要がないかもしれませんが、一応原因等について記録しておきます。

WSLで困りごとがあったときは、GitHubのissueで検索するのが鉄板かと思います。

案の定、同じ症状に関してissueが作成されていました。(GitHub issue: https://github.com/microsoft/WSL/issues/4245

エラー原因

issue内で言及されていますが、詳しい原因はわかっていないようです。。。

ですが、この問題が起きる状況はわかっているようです。

エラーが起きる状況
  1. WSL2の起動
  2. パソコンをスリープ状態へ
  3. パソコンのスリープを解除
  4. WSL2上のシステム時間が現実時間とずれる
  5. apt-getでエラーが発生

つまり、パソコンを一度スリープするとWSL2上のシステム時間と現実時間にずれが発生し、エラーが発生してしまうようです。

確かに自分もスリープしたらエラーが発生していました。。。

解決策1:hwclockコマンドの実行

解決策の一つ目は、hwclockコマンドを実行して、システム時間と現実時間を同期することです。

# 時刻がずれている
$ date
Sat Jul 24 05:20:06 JST 2021

# 時刻の修正
$ sudo hwclock -s

# 時刻が修正されたことを確認
$ date
Sat Jul 24 14:56:25 JST 2021

解決策2:WSLの再起動

もう一つの解決策として紹介されているのが、WSLの再起動です。

PowerShellを開いて、wsl --shutdownを実行しましょう。

その後、WSLの起動すれば修正されているはずです。

まとめ

時刻ズレが原因でapt-get updateコマンドが失敗するときの対処方法を紹介しました。

個人的には再起動するのはめんどくさいので、解決策1のhwclockコマンドの利用がおすすめです。

2 COMMENTS

thatcarefulguy

thanks a lot !!

working on Windows 11 Insider Beta 22000.168 + WSL2 Ubuntu 20.04.3 LTS

現在コメントは受け付けておりません。