LoginSignup
1
0

CentOS初期設定

Last updated at Posted at 2024-03-06

OSの初期設定に関する備忘録です。

初めに

おおまかな流れは以下の通りである。

  1. OSインストール ※今回は記載略
  2. ネットワーク設定の有効化
  3. SSHの有効化
  4. パッケージの最新化
  5. ホスト名の設定
  6. SELINUXの無効化
  7. 不要なサービスの無効化 ※用途によって変わるため要確認
  8. suコマンド実行ユーザ管理
  9. ファイアウォールサービスの設定
  10. 公開鍵認証設定

上記の他、用途に応じで作業を個別に実施する。

OS インストール

OSのイメージはCentOS公式https://www.centos.org/からダウンロード可能。

※CentOS7は2024年6月末でサポートが終了するため、CentOS Stream等サポートされるバージョンを導入すること

今回はESXi上に仮想マシンとしてインストールを行う。
OSのインストール方法については本記載では省略する。

ネットワーク設定の有効化

初期状態ではネットワークの設定が有効化されていない。そのためSSHやTelnetで接続するためにはまずネットワーク設定を有効化する必要がある。

まずはターミナルを開く。ターミナルは左上の「アクティビティ」から「端末」を選択する。

※画像だと赤枠部分
CentOSデスクトップ画面1.png

この画面からでもネットワークの設定はおこなえるが今回はCLIから設定をおこなう。
画面左下の端末アイコンをクリックする。

CentOSデスクトップ画面2.png

クリック後、ターミナルが起動する。

次にネットワークの有効化をおこなう。ネットワーク設定は以下のコマンドを入力することで設定できる。
ネットワーク設定1.JPG

入力後、Ethernetアダプタとブリッジが表示されるが今回はEthernetの項目の「ens192」を編集する。
ネットワーク設定2.JPG

編集画面が表示されたら、下部の「自動的に接続する」を選択し、Enterキーを入力することでXマークが付与される。その後「OK」を選択する。

※DHCPサーバからIPを付与されていない場合については別途ここでIPアドレスを付与するがここでは省略する。

ネットワーク設定3.JPG

「戻る」を選択する。

ネットワーク設定4.JPG

次に設定したらネットワーク接続を有効化するため、「接続をアクティベートする」を選択する。

ネットワーク設定5.JPG

選択後、先ほど設定した「ens192」を選択し、「アクティベート」を選択する。

ネットワーク設定6.JPG

これでネットワーク接続が有効化され外部との接続が可能となる。

※試しにPINGとか打ってもよいかも

ネットワーク設定7.JPG

最後に戻るを選択することでターミナル画面に戻ることができる。

SSH有効化

※既に起動している場合はスルー
このままESXi上で作業してもよいが効率が悪いため、SSHを有効化しTeraterm経由で構築作業がおこなえるようにする。

様々な方法があるが手っ取り早くターミナルからSSHサービスを起動する。

bash
systemctl start sshd

SSHサービスが起動しているか確認するには、

bash
systemctl status sshd

でActive: active (running)と表示されていればサービスは正常に起動している。

このままでもSSH接続可能だが、OS再起動した際にサービスが停止してしまいその度にESXi経由でサービスを起動しなければならないため、サービスの自動起動を有効化しよう。

bash
systemctl enable sshd

これでOS再起動後もSSHで接続が可能となる。

パッケージの最新化

パッケージの最新化は以下のコマンドでおこなう。

bash
yum update -y

CentOS8以降ではdnfが標準のパッケージ管理システムとなるため、
パッケージの最新化は以下のコマンドでおこなう。

bash
dnf update -y

で同様にパッケージのアップデートが可能。
(yumコマンドもあるが実際はdnfのaliasらしい)

ホスト名の設定

bash
hostnamectl set-hostname <HOSTNAME>

コマンド実行後、再起動することで適用される。※もしかしたらしなくても反映されるかも。

SELinuxの無効化

SELinuxはLinuxのセキュリティ関連のサービスを提供してくれるがこいつ起因でつながらない、動かないがとても多いため本来は有効化するものだが無効化する

SELinuxの状態については以下のコマンドで確認できる

bash
getenforce

このとき、「Enforcing」と表示されれば有効化されている。

SELinuxの無効化はviコマンドから以下のファイルを編集する。

bash
vi /etc/selinux/config

ファイルを開くと下記の画面が表示されるため、下記「SELINUX=enforcing」の部分を「 SELINUX=Disabled」に書き換える。

■書き換え前

/etc/selinux/config
# 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

■書き換え後

/etc/selinux/config
# 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

書き換え後、OSを再起動し、再度getenforceコマンドで有効化状態を確認する。

bash
getenforce

「Disabled」と表示されれば設定変更完了となる。

不要なサービスな無効化

不要なサービスの常時起動はセキュリティ的な抜け道になりやすいため、要件がなければ塞ぐに限る。

その際に何が不要なサービスになるかは個人や提供システムの形態にもよるが、ここではとりあえず下記に表示されているサービスを停止する。

bash
systemctl disable auditd.service
systemctl disable abrt-ccpp.service
systemctl disable abrt-oops.service
systemctl disable abrt-xorg.service
systemctl disable abrt-vmcore.service
systemctl disable abrtd.service
systemctl disable libstoragemgmt.service
systemctl disable smartd.service
systemctl disable libvirtd.service
systemctl disable qemu-guest-agent.service
systemctl disable dmraid-activation.service
systemctl disable mdmonitor.service
systemctl disable avahi-daemon.service
systemctl disable kdump

時刻同期

端末やサーバ内にあるハードウェアクロックは時間の誤差がでやすいため、基本的には外部のNTPサーバから時刻をもらって合わせるのが一般的である。

今回は特に要件がなければNICT(国立研究開発法人 情報通信研究機構)の日本標準時グループ(https://jjy.nict.go.jp/tsp/PubNtp/index.html)が提供しているNTPサービスを利用する。

まず、NTPサービスを利用するためにchronyをインストールする。
(既に入っている場合もある)

bash
dnf install chrony

インストールが完了したら、viコマンドで下記のファイルを開く。

bash
vi /etc/chrony.conf

※修正箇所は1つ、最初のNTPサーバの向き先をNICTののNTPサーバに向ける。

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

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#allow 192.168.0.0/16

# Serve time even if not synchronized to a time source.
#local stratum 10

# Specify file containing keys for NTP authentication.
keyfile /etc/chrony.keys

# Get TAI-UTC offset and leap seconds from the system tz database.
leapsectz right/UTC

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

設定変更後、サービスの再起動を実施する。

bash
systemctl restart chronyd

向き先が正常に設定されたかどうかは下記コマンドで確認できる。

bash
chronyc sources

設定反映した向き先が記載されていれば設定は完了となる。

suコマンド実行ユーザ管理

suやsudoコマンドはかなり端折ってに説明するとスーパーユーザと同じ権限(もしくはスーパーユーザそのもの)でコマンドを実行させることができる修飾子である。

そのため、使えるユーザは限定的であればあるほど良いため一部のユーザのみsuコマンドの実行権限を付与させる。実行権限を付与するためには、対象のユーザをwheelユーザグループに登録することで可能となる。

※もちろんこのwheelグループもsuを有効化する手順が必要である。

まずは対象ユーザをwheelグループに登録する。

bash
usermod -g wheel <username>

次にwheelグループの設定を変更するため以下のファイルを開く

bash
vi /etc/pam.d/su

そして以下の項目を書き換える

■変更前

bash
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid

■変更後

bash
# Uncomment the following line to require a user to be in the "wheel" group.
auth required pam_wheel.so use_uid

以上で設定は完了となる。

FWの設定

FWは外部と内部で各ポートに対して不必要な接続を行わせないためのサービスである。セキュリティ的にとても重要な設定のためここは正しくおこなうように。

基本的には使うポートだけ開放し、それ以外は閉塞が基本となる。開放するポートはそのサーバが提供するサービスによるがWebサーバなら80番、443番、SSH接続なら22番などがある。

今回はSSH用に22番ポートを開放する手順について記載する。

まずはFWサービスを起動させる。

bash
systemctl start firewalld.service

サービスが起動しているかはsystemctl statusで確認する。

bash
systemctl status firewalld.service

次にSSH用のポートを解放する。

bash
firewall-cmd --zone=public --add-port=22/tcp --permanent

最後にFWサービスを自動起動設定をおこなう。

bash
systemctl is-enabled firewalld.service

以上で設定は完了となる。

公開鍵認証設定

通常、初期設定状態でSSH接続するとパスワード認証で接続することになるが、パスワード認証はセキュリティ的にあまり推奨されていない。一般的に公開鍵認証を利用することが推奨されているためその設定をおこなう。

まず公開鍵を生成する。

bash
ssh-keygen -t rsa

次に公開鍵を公開鍵認証用に設定変更する。
まず、対象ユーザのsshフォルダに遷移する。

bash
cd .ssh/

公開鍵の名前を変更する。

bash
mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys

名前を変更した公開鍵のファイル権限を変更する。

bash
chmod 600 ~/.ssh/authorized_keys

該当ディレクトリ内になる「id_rsa」をクライアント側にSCPを利用して転送する。

※WinSCPやFFFTPなどのソフトで転送してもよい。

以上で設定変更は完了となる。

おわりに

以上でOS初期設定は完了となる。
※過不足があるかもしれないのでその時は加筆修正します。

1
0
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
1
0