2017年5月11日木曜日

Raspberry Pi 3上のCentOS7の初期設定

Raspberry Pi 3にCentOS7が入ったので、最低限の初期設定を行う。



■本日の目標

・必要最低限の設定を入れる


■キーボード&文字コード設定

・基本はSSH接続だけど、直接操作するときのために
 109キー日本語レイアウトのキーボードの設定を行う。
 【変更前】
$ localectl
   System Locale: n/a

       VC Keymap: n/a
      X11 Layout: n/a

 【コマンド】
$ localectl set-keymap jp106
$ localectl set-keymap jp-OADG109A
$ localectl set-locale LANG=ja_JP.utf8

 【変更後】
$ localectl
   System Locale: LANG=ja_JP.utf8
       VC Keymap: jp-OADG109A
      X11 Layout: jp
       X11 Model: jp106
     X11 Options: terminate:ctrl_alt_bksp


■rootパスワード変更

・念のため、パスワードを変えておく
$ passwd
Changing password for user root.
New password:            # ←新しいパスワードを入力(表示されないので注意)
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:     # ←もう一度入力
passwd: all authentication tokens updated successfully.

■パーティション拡張

・16GBのmicroSDカードを使ってるのでそれ相応のサイズを認識しているようだが、
 実際に使える領域が少なすぎるのでパーティションの拡張も行う。
 【変更前】
$ fdisk -l

Disk /dev/mmcblk0: 15.9 GB, 15931539456 bytes, 31116288 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0006dc70

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            2048     1026047      512000    c  W95 FAT32 (LBA)
/dev/mmcblk0p2         1026048     2074623      524288   82  Linux swap / Solaris
/dev/mmcblk0p3         2074624     6268927     2097152   83  Linux
$
$ df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/root      ext4      2.0G  748M  1.1G  41% /
devtmpfs       devtmpfs  459M     0  459M   0% /dev
tmpfs          tmpfs     463M     0  463M   0% /dev/shm
tmpfs          tmpfs     463M   12M  451M   3% /run
tmpfs          tmpfs     463M     0  463M   0% /sys/fs/cgroup
/dev/mmcblk0p1 vfat      500M   43M  457M   9% /boot
tmpfs          tmpfs      93M     0   93M   0% /run/user/0

 【コマンド】
$ touch /.rootfs-repartition
$ reboot

 【変更後】 ついでにさっき変更したSystemLocaleも反映されたようで・・・
$ fdisk -l

Disk /dev/mmcblk0: 15.9 GB, 15931539456 bytes, 31116288 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x0006dc70

  デバイス ブート      始点        終点     ブロック   Id  システム
/dev/mmcblk0p1            2048     1026047      512000    c  W95 FAT32 (LBA)
/dev/mmcblk0p2         1026048     2074623      524288   82  Linux swap / Solaris
/dev/mmcblk0p3         2074624     6268927     2097152   83  Linux

$ df -hT
ファイルシス   タイプ   サイズ  使用  残り 使用% マウント位置
/dev/root      ext4       2.0G  748M  1.1G   41% /
devtmpfs       devtmpfs   459M     0  459M    0% /dev
tmpfs          tmpfs      463M     0  463M    0% /dev/shm
tmpfs          tmpfs      463M   12M  451M    3% /run
tmpfs          tmpfs      463M     0  463M    0% /sys/fs/cgroup
/dev/mmcblk0p1 vfat       500M   43M  457M    9% /boot
tmpfs          tmpfs       93M     0   93M    0% /run/user/0

 ・・・ん?・・・変更されない・・・??

 ちょっと他の方法で試してみる。
 ⇒こちらのサイトを参考にさせていただいた。

 【再変更後】
$ fdisk -l

Disk /dev/mmcblk0: 15.9 GB, 15931539456 bytes, 31116288 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x0006dc70

  デバイス ブート      始点        終点     ブロック   Id  システム
/dev/mmcblk0p1            2048     1026047      512000    c  W95 FAT32 (LBA)
/dev/mmcblk0p2         1026048     2074623      524288   82  Linux swap / Solaris
/dev/mmcblk0p3         2074624    29218815    13572096   83  Linux

$ df -hT
ファイルシス   タイプ   サイズ  使用  残り 使用% マウント位置
/dev/root      ext4        13G  752M   12G    7% /
devtmpfs       devtmpfs   459M     0  459M    0% /dev
tmpfs          tmpfs      463M     0  463M    0% /dev/shm
tmpfs          tmpfs      463M   12M  451M    3% /run
tmpfs          tmpfs      463M     0  463M    0% /sys/fs/cgroup
/dev/mmcblk0p1 vfat       500M   43M  457M    9% /boot
tmpfs          tmpfs       93M     0   93M    0% /run/user/0 

 パーティションサイズの拡張に成功!


■タイムゾーン設定

・時間がおかしかったのでタイムゾーンも見直す
 【変更前】
$ timedatectl
      Local time: 日 2017-04-30 13:01:28 UTC
  Universal time: 日 2017-04-30 13:01:28 UTC
        RTC time: n/a
       Time zone: UTC (UTC, +0000)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

 【コマンド】
$ timedatectl set-timezone Asia/Tokyo

 【変更後】
$ timedatectl
      Local time: 日 2017-04-30 22:01:45 JST
  Universal time: 日 2017-04-30 13:01:45 UTC
        RTC time: n/a
       Time zone: Asia/Tokyo (JST, +0900)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

■NTP設定

・NTPと言えばNtpdだろうと調べてみたが、CentOS7の標準ではChronyというもので同期しているようだ。
 特にこだわりはないし、そのままChronyを使って、日本の標準時を扱っているNICTで同期する。
 【変更前】
$ cat /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
(以降、省略)

 【コマンド】
 viコマンドで編集
 「server ntp.nict.jp iburst」を追加

 【変更後】
$ cat /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
server ntp.nict.jp iburst      # ←追加
(以降、省略)

 設定反映のため、サービスを再起動しておく
$ systemctl restart chronyd

$ systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since 日 2017-04-30 22:13:29 JST; 3s ago
  Process: 1069 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 1063 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1067 (chronyd)
   CGroup: /system.slice/chronyd.service
           mq1067 /usr/sbin/chronyd

 4月 30 22:13:29 centos-rpi3 systemd[1]: Starting NTP client/server...
 4月 30 22:13:29 centos-rpi3 chronyd[1067]: chronyd version 2.1.1 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +DEBUG +ASYNCDNS  +IPV6 +SECHASH)
 4月 30 22:13:29 centos-rpi3 chronyd[1067]: Frequency 7.325 +/- 8.156 ppm read from /var/lib/chrony/drift
 4月 30 22:13:29 centos-rpi3 systemd[1]: Started NTP client/server.


■IPv6無効化

・内部ネットワークでIPv6を使ってないし、
 また割り当てミスが発生しても困るので、IPv6を無効化しておく。
 【変更前】
$ cat /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).

 【コマンド】
$ echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> /etc/sysctl.conf
$ echo 'net.ipv6.conf.default.disable_ipv6 = 1' >> /etc/sysctl.conf

 【変更後】
$ cat /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1 

 仕上げに
$ sysctl -p
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
で設定反映完了。

 ifconfigでIPアドレスを確認してみると、IPv6の項目が無くなった。
$ ifconfig
eth0: flags=XXXX  mtu 1500
        inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255
        ether XX:XX:XX:XX:XX:XX  txqueuelen 1000  (Ethernet)
    (以降、省略)


■ホスト名を変更

・デフォルトの「centos-rpi3」でもいいんだけど、味気ないので変更しておく。
 【変更前】
$ cat /etc/hostname
centos-rpi3

 【コマンド】
 viコマンドで編集
 ⇒適宜書き換えて再起動
  


他には「SELinux」とか「firewalld」の設定もしようと思ったけど、
必要になったら(問題が発生したら)対応することにする。

Previous Post
Next Post

post written by: