キーボードを打っていれば幸せ

パソコンのインストールや設定で困ったことやセキュリティについての備忘録

Ubuntu 18.04LTSで # apt update 失敗 → 設定変更により、修正完了

まとめ

Ubuntu 18.04LTSで# apt updateすると時刻関係のエラーが出て途中から進まなくなった。
timezoneとntpを設定することでエラーが消え、正しく動作するようになった。 2018/10/14事象発生→同日解決

環境

VMWare Workstation 14 Pro
Ubuntu 18.04LTS (VMWare上のVM上で動作)
MISP 2.4.96 (今回の事象とは関係ないけど、MISPが配布しているVMイメージを利用したので書いておく)

エラーメッセージ

OSをアップデートするために# apt updateを実行すると、次のエラーが出た。
「Release fileが6時間56分ずれており、無効である」のような意味。システム時刻が問題のようである。

root@misp:/var/www/MISP# apt update
Hit:1 http://us.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Reading package lists... Done      
E: Release file for http://security.ubuntu.com/ubuntu/dists/bionic-security/InRelease is not valid yet (invalid for another 6h 56min 10s). Updates for this repository will not be applied.
E: Release file for http://us.archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelease is not valid yet (invalid for another 6h 56min 22s). Updates for this repository will not be applied.
E: Release file for http://us.archive.ubuntu.com/ubuntu/dists/bionic-backports/InRelease is not valid yet (invalid for another 6h 56min 48s). Updates for this repository will not be applied.

調べた結果

このエラーメッセージ(E:で始まる行)でググると、そのPCの時刻を正しく合わせろ、のようなことが書かれていた。
$ dateで確認すると、確かに7時間近く遅れている。
timezoneもCEST(ヨーロッパ)時間になっている。
# apt updateで取得する情報が正しくないと、正しくupdateできないので文句を言うのは納得がいく。
ということで、timezoneとntpを正しく設定することにする。

timezoneの設定

まず timedatectl コマンドを使ってtimezoneを「Asia/Tokyo」(JST)に設定する。

# timezone一覧の確認
root@misp:/var/www/MISP# timedatectl list-timezones 
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
:中略

# timezoneをAsia/Tokyoに設定する
root@misp:/var/www/MISP# timedatectl set-timezone Asia/Tokyo 

# 設定結果の確認。ちゃんとJSTになっている。
# が、まだ時刻が不正確(この時のリアル時刻は2018-10-14 6:20ころだった)
root@misp:/var/www/MISP# timedatectl
                      Local time: Sat 2018-10-13 22:12:06 JST
                  Universal time: Sat 2018-10-13 13:12:06 UTC
                        RTC time: Sat 2018-10-13 13:12:07
                       Time zone: Asia/Tokyo (JST, +0900)
       System clock synchronized: yes
systemd-timesyncd.service active: yes
                 RTC in local TZ: no

ntpサーバの設定

/etc/ntp.conf ファイルを編集し、ntpサーバを設定する。

# /etc/ntp.conf ファイルを編集する。念のため修正の前にバックアップを取った方がよい。
root@misp:/var/www/MISP# vi /etc/ntp.conf

# 編集部分のみ抜粋する。行頭の数字は行番号。
# 21~24、27行目のubuntuのntpサーバをコメントアウトする。
# 28~30行目のntpサーバを追加。(他のサーバを追加してもOK)
# 54行目の時刻同期を許可する設定を追加
-- /etc/ntp.conf抜粋 --
18 # Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
19 # on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
20 # more information.
21 #pool 0.ubuntu.pool.ntp.org iburst
22 #pool 1.ubuntu.pool.ntp.org iburst
23 #pool 2.ubuntu.pool.ntp.org iburst
24 #pool 3.ubuntu.pool.ntp.org iburst
25 #
26 # Use Ubuntu's ntp server as a fallback.
27 #pool ntp.ubuntu.com
28 server ntp.nict.jp iburst
29 server ntp1.jst.mfeed.ad.jp iburst
30 server ntp2.jst.mfeed.ad.jp iburst
:中略
51 # Clients from this (example!) subnet have unlimited access, but only if
52 # cryptographically authenticated.
53 #restrict 192.168.123.0 mask 255.255.255.0 notrust
54 restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap
-- /etc/ntp.conf抜粋 --

# ntpサービスを再起動する
root@misp:/var/www/MISP# systemctl restart ntp

# 現在の時刻を確認。まだずれている。
root@misp:/var/www/MISP# date
Sat Oct 13 22:18:13 JST 2018

# 時刻同期を実行する。
root@misp:/var/www/MISP# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 ntp-a2.nict.go. .NICT.           1 u    1   64    1    6.551   -1.620   0.000
 ntp1.jst.mfeed. 133.243.236.17   2 u    1   64    1    4.785   -0.250   0.000
 ntp2.jst.mfeed. .STEP.          16 u  581   64    0    0.000    0.000   0.000

# 現在時刻を確認する。正しくなった。
root@misp:/var/www/MISP# date
Sun Oct 14 06:30:02 JST 2018

# apt updateも正しく動作するようになった。
root@misp:/var/www/MISP# apt update
Hit:2 http://us.archive.ubuntu.com/ubuntu bionic InRelease
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [401 kB]
Get:6 http://us.archive.ubuntu.com/ubuntu bionic-updates/main i386 Packages [362 kB]
Get:7 http://us.archive.ubuntu.com/ubuntu bionic-updates/universe i386 Packages [555 kB]
Get:8 http://us.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [560 kB]     
Fetched 2,124 kB in 11s (202 kB/s)                                                            
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.

参考

/etc/ntp.confの設定方法や、サービスの再起動等は次のサイトを参考にさせていただきました。
このサイトには、Ubuntu以外のOSや、サーバ用途に関する多くの設定方法がわかりやすく書かれています。
server world https://www.server-world.info/query?os=Ubuntu_18.04&p=ntp&f=1