LoginSignup
0
1

More than 3 years have passed since last update.

RHEL7以降の初期設定について

Last updated at Posted at 2020-11-23

はじめに

RHEL7系以降の必要最低限の初期設定をしていきます。

またNW設定やサブスクリプション登録は別の記事で投稿しているのでご参照の程、宜しくお願い致します。
今回はセキュリティ関連を全て無効化する設定にしています。

現場によってはセキュリティ製品関連はサードパーティ製を利用しているのではないでしょうか。

個人的にはサードパーティ製品に頼らなくもRed Hat社が提供している
ドキュメントで十分にSecurity対策ができるのではと思っています。

時間がある時にセキュリティ関連の設定記事を書きたいと思っています。
今後セキュリティ関連を扱う際は以下のドキュメントを参考にします。

Product Documentation for Red Hat Enterprise Linux 8
https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/8/

TOMOYO Linux
https://tomoyo.osdn.jp/

bash補完機能の強化

参考にしたサイトは以下になります。

【 complete 】コマンド――補完の方法を指定する(bash)
https://www.atmarkit.co.jp/ait/articles/1907/12/news015.html

dnf install -y bash-completion

ホスト名設定

手順は以下のドキュメントに参考にしています。

第6章 ホスト名の設定
https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/7/html/networking_guide/ch-configure_host_names

現在のホスト名を確認するために以下のコマンドを実行します。

hostnamectl status

Static hostname欄を確認し"localhost.localdomain"であることが確認できます。
今回はこのホスト名を変更します。

実行結果

   Static hostname: localhost.localdomain ★
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 732bd8d5144d4845ad0dc5738997dae0
           Boot ID: abc80636176744d0a0dd74eeabb22288
    Virtualization: vmware
  Operating System: Red Hat Enterprise Linux 8.2 (Ootpa)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:8.2:GA
            Kernel: Linux 4.18.0-193.el8.x86_64
      Architecture: x86-64

以下のコマンドを実行しホスト名を"rhel82.localdomain"に変更します。

hostnamectl set-hostname rhel82.localdomain

以下のコマンドを実行しStatic hostname欄を確認し"rhel82.localdomain"に変更されたことを確認します。

hostnamectl status

実行結果

   Static hostname: rhel82.localdomain ★
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 732bd8d5144d4845ad0dc5738997dae0
           Boot ID: abc80636176744d0a0dd74eeabb22288
    Virtualization: vmware
  Operating System: Red Hat Enterprise Linux 8.2 (Ootpa)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:8.2:GA
            Kernel: Linux 4.18.0-193.el8.x86_64
      Architecture: x86-64

基本的な環境設定確認

システムロケール確認

以下のコマンドを実行しシステムロケールを確認します。

localectl status

実行結果

   System Locale: LANG=ja_JP.UTF-8
       VC Keymap: jp
      X11 Layout: jp

言語およびキーマップは日本語に設定されていることが確認できているので今回の変更作業は割愛します。
基本的な環境設定を変更したい場合は、以下のサイトをご参照の程、宜しくお願い致します

1.6. 基本的な環境設定の変更

Firewall 無効化

次にFirewall の無効化設定をしていきます。
RHEL8のリリースノートにfirewalld デーモンとバックエンドとして nftablesが
使用されていることが記載されているので両方とも無効化します。

Red Hat Enterprise Linux 8.0 リリースノート
https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/8/html/8.0_release_notes/index

以下のコマンドを実行しfirewalldサービスの状態を確認します。

systemctl status firewalld.service

★印のところを確認し、状態が"active (running)"であるを確認します。

実行結果

● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-11-22 21:06:09 EST; 1min 0s ago ★
     Docs: man:firewalld(1)
 Main PID: 1037 (firewalld)
    Tasks: 2 (limit: 23789)
   Memory: 37.4M
   CGroup: /system.slice/firewalld.service
           mq1037 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid

以下のコマンドを実行しfirewalldサービスを停止します。

systemctl stop firewalld.service

以下のコマンドを実行しfirewalldサービスの状態を確認します。

systemctl status firewalld.service

★印のところを確認し、状態が"inactive (dead)"であるを確認します。

実行結果

● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Sun 2020-11-22 21:07:26 EST; 3s ago ★
     Docs: man:firewalld(1)
  Process: 1037 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 1037 (code=exited, status=0/SUCCESS)

以下のコマンドを実行しfirewalldサービスの自動起動設定を確認します。
今回の場合、自動起動設定が有効になっているため無効化します。

systemctl list-unit-files --type service firewalld.service

実行結果

UNIT FILE         STATE
firewalld.service enabled

1 unit files listed.

以下のコマンドを実行しnftablesサービスの自動起動設定を確認します。
今回の場合、自動起動設定が無効化されているため、このままにします。

systemctl list-unit-files --type service nftables.service

実行結果

UNIT FILE        STATE
nftables.service disabled

1 unit files listed.

以下のコマンドを実行しfirewalldサービスの自動起動設定を無効化します。

systemctl disable firewalld.service

実行結果

Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

以下のコマンドを実行しfirewalldサービスの自動起動設定が無効化されていることを確認します。

systemctl list-unit-files --type service firewalld.service

実行結果

UNIT FILE         STATE
firewalld.service disabled

1 unit files listed.

時刻同期設定

RHEL7以降からChronyの使用が推奨されましたのでChronyの設定をします。
今回参考にしたサイトは以下になります。

第11章 Chrony スイートを使用した NTP の設定
https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/8/html-single/configuring_basic_system_settings/index#using-chrony-to-configure-ntp

日本標準時グループ
https://jjy.nict.go.jp/index.html

Chronyインストール

以下のコマンドを実行しchronyをインストールします。

dnf install -y  chrony

Chrony自動起動設定確認

以下のコマンドを実行しchronydが自動起動する設定されているか確認します。

systemctl list-unit-files | grep chronyd

実行結果

chronyd.service                            enabled

自動起動の設定がされていることが確認できます。
もし"disabled"と表示されている場合は、"systemctl enable chronyd"を実行します。

Chronyステータス確認

以下のコマンドを実行しchronydのステータスを確認します。

systemctl status chronyd

実行結果

● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: inactive (dead) ★
     Docs: man:chronyd(8)
           man:chrony.conf(5)

chronydが起動していないことが★印のところで確認できます。

Chrony起動

以下のコマンドを実行しChronyを起動します。

systemctl start chronyd

chronydステータス確認

以下のコマンドを実行し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 Wed 2020-12-02 05:55:12 JST; 3s ago ★
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 2068 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 2064 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 2066 (chronyd)
    Tasks: 1 (limit: 23529)
   Memory: 1.5M
   CGroup: /system.slice/chronyd.service
           mq2066 /usr/sbin/chronyd

chronydが起動していることが★印のところで確認できます。

Chrony設定

今回は日本標準時グループが公開しているNTPサーバを指定します。
その他の設定はデフォルトのままにしています。
またchrony.confの各種設定項目の詳細はmanコマンドで確認できます。

Chrony設定変更

以下のコマンドを実行しchrony.confファイルを変更します。

vi /etc/chrony.conf

変更前

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
pool 2.rhel.pool.ntp.org iburst

変更後

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#pool 2.rhel.pool.ntp.org iburst
server ntp.nict.jp iburst

Chrony設定反映

以下のコマンドを実行し設定を反映します。

systemctl restart chronyd

NTPサーバと同期確認

以下のコマンドを実行しNTPサーバと時刻同期確認をします。

chronyc sources

実行結果

210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* ntp-b3.nict.go.jp             1   6     7     1    +30ms[ +173ms] +/-  174ms

設定したNTPサーバと同期できたことが確認できます。

SELinux無効

『SELINUX の使用』のSELinux無効化項目を要約すると以下の2点になります。

・SELinuxを永続的無効する代わりにPermissiveモードを推奨
・/etc/selinux/configでSELINUX=disabledへ変更、ブートオプションにselinux=0を追加しないとカーネルパニックが発生する可能性

今回はブート時のカーネルパラメータ変更の追加、削除を行うため2つ目の方法を実施します。

SELINUX の使用
https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/8/html-single/using_selinux/index

以下のコマンドを実行しSELinuxの状態を確認します。
今回の場合、有効化されているため無効化します。

getenforce

実行結果

Enforcing

以下のコマンドを実行しSELinuxのモードを確認します。
★の場所を書き換えて無効化の恒久化します。

cat /etc/sysconfig/selinux

実行結果

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing ★
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

以下のコマンドを実行し設定ファイルを書き換えします。

sed -ie 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

以下のコマンドを実行しSELinuxが設定が変更されていることを確認します。

cat /etc/sysconfig/selinux

実行結果

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled ★
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

ブート時のカーネルパラメータ変更

以下のドキュメントを参考に変更します。

第26章 GRUB 2 での作業
https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/7/html/system_administrators_guide/ch-working_with_the_grub_2_boot_loader

この /etc/default/grub ファイルは、インストールプロセスの際に grub.cfg を作成するときに
anaconda で使用される grub2-mkconfig ツールにより使用されます。
たとえばブートローダーの設定を再作成する必要がある場合は、システム障害時に使用することができます。
通常、最後の手段でない限り、手動で grub2-mkconfig を実行して grub.cfg ファイルを置き換えることは推奨されません。
/etc/default/grub への手動の変更には、grub.cfg ファイルの再ビルドが必要な点に留意してください。

/etc/default/grubを変更せずにgrubbyコマンドを使用して起動時のカーネルパラメータを変更します。

以下のコマンドを実行し起動時のカーネルパラメータを確認します。

cat /proc/cmdline

実行結果

BOOT_IMAGE=(hd0,msdos1)/vmlinuz-4.18.0-193.el8.x86_64 root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet

以下のコマンドを実行しデフォルトのカーネルの一覧表示します。

grubby --default-kernel

実行結果

/boot/vmlinuz-4.18.0-193.el8.x86_64

以下のコマンドを実行し、上記で確認したカーネルのメニューエントリーを表示します。(★印の行を確認)

grubby --info /boot/vmlinuz-4.18.0-193.el8.x86_64

実行結果

index=0
kernel="/boot/vmlinuz-4.18.0-193.el8.x86_64"
args="ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet $tuned_params" ★
root="/dev/mapper/rhel-root"
initrd="/boot/initramfs-4.18.0-193.el8.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (4.18.0-193.el8.x86_64) 8.2 (Ootpa)"
id="732bd8d5144d4845ad0dc5738997dae0-4.18.0-193.el8.x86_64"

今回はRed Hat グラフィカルブートの削除およびSELinuxの無効化パラメータを追加します。
グラフィカルブートを無効化することでOS起動時失敗時の調査資料になるためです。

以下のコマンドを実行し、カーネルパラメータを変更します。

※カーネルアップデート後は以下のコマンドを実行して全てのエントリーを
更新する必要があります。更新しなかった場合、ログインできなくなる可能性があります。
grubby --update-kernel=ALL --remove-args="rhgb quiet" --args=selinux=0

grubby --remove-args="rhgb quiet" --args=selinux=0 --update-kernel /boot/vmlinuz-4.18.0-193.el8.x86_64

以下のコマンドを実行し、カーネルパラメータを変更されたことを確認します。(★印の行を確認)

grubby --info /boot/vmlinuz-4.18.0-193.el8.x86_64

実行結果

index=0
kernel="/boot/vmlinuz-4.18.0-193.el8.x86_64"
args="ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap $tuned_params selinux=0" ★
root="/dev/mapper/rhel-root"
initrd="/boot/initramfs-4.18.0-193.el8.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (4.18.0-193.el8.x86_64) 8.2 (Ootpa)"
id="732bd8d5144d4845ad0dc5738997dae0-4.18.0-193.el8.x86_64"

以下のコマンドを実行し起動時のカーネルパラメータを再度、確認します。
この時点では、起動時のカーネルパラメータが変更されていないためrebootコマンドでOSを再起動します。

cat /proc/cmdline

実行結果

BOOT_IMAGE=(hd0,msdos1)/vmlinuz-4.18.0-193.el8.x86_64 root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet

OS起動後、再度以下のコマンドを実行し新しいカーネルパラメータが読み込まれたことを確認します。

cat /proc/cmdline

実行結果

BOOT_IMAGE=(hd0,msdos1)/vmlinuz-4.18.0-193.el8.x86_64 root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap selinux=0

追記 2020年11月26日

dnf update等でカーネルもバージョンアップした際は、以下のコマンドを実行し
ブート時のカーネルパラメータにも変更します。

grubby --update-kernel=ALL --remove-args="rhgb quiet" --args=selinux=0

カーネルパラメータが変更されたことを確認します。

grubby --info=ALL

実行結果

index=0
kernel="/boot/vmlinuz-4.18.0-240.1.1.el8_3.x86_64"
args="ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap selinux=0 $tuned_params"
root="/dev/mapper/rhel-root"
initrd="/boot/initramfs-4.18.0-240.1.1.el8_3.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (4.18.0-240.1.1.el8_3.x86_64) 8.3 (Ootpa)"
id="732bd8d5144d4845ad0dc5738997dae0-4.18.0-240.1.1.el8_3.x86_64"
index=1
kernel="/boot/vmlinuz-4.18.0-193.el8.x86_64"
args="ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap $tuned_params selinux=0"
root="/dev/mapper/rhel-root"
initrd="/boot/initramfs-4.18.0-193.el8.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (4.18.0-193.el8.x86_64) 8.2 (Ootpa)"
id="732bd8d5144d4845ad0dc5738997dae0-4.18.0-193.el8.x86_64"
index=2
kernel="/boot/vmlinuz-0-rescue-732bd8d5144d4845ad0dc5738997dae0"
args="ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap selinux=0"
root="/dev/mapper/rhel-root"
initrd="/boot/initramfs-0-rescue-732bd8d5144d4845ad0dc5738997dae0.img"
title="Red Hat Enterprise Linux (0-rescue-732bd8d5144d4845ad0dc5738997dae0) 8.2 (Ootpa)"
id="732bd8d5144d4845ad0dc5738997dae0-0-rescue"

まとめ

今回でRHELの初期設定を終わりにします。
カーネルバージョンアップ後は/etc/default/grubのパラメータ設定が引き継がれていました。grubbyで変更したものは引き継がれないみたいです。

運用を考えた際は、/etc/default/grubを変更してgrub2-mkconfigで再作成した方がメンテナンスしやすいです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1