LoginSignup
8
10

More than 3 years have passed since last update.

ネットワーク設定後のCentOS7の基本設定

Last updated at Posted at 2020-03-12

はじめに

CentOS7をインストールし
ネットワーク設定をした後に何を設定すべきか
個人的ベストプラクティスを明記します。

全て設定する必要性はなく、必要な設定をピックアップしてください。

条件

条件:CentOS7.7を既にインストール済みであること
   インストール時のソフトウェアはインフラストラクチャーサーバを選択
   以下のCentOSネットワーク設定を構築済み
   インストール後に於けるCentOS7の初期ネットワーク設定
   

手順

ログイン

まずは管理者権限を持つアカウントでログインします。

CentOS7.7
localhost login:

プロンプトが「#」になっていることを確認します。
「$」であれば管理者権限を持っていないユーザでログイン
しているのでrootでログインし直します。

CentOS7.7
[root@localhost ~]#

SELINUXの無効化

SELINUXの設定を無効化します。
vi /etc/selinux/configコマンドを入力します。

CentOS7.7
[root@localhost ~]#vi /etc/selinux/config

設定ファイルを以下へ変更します。

#SELINUX=enforcing
SELINUX=disabled

設定を反映させるためにサーバを再起動します。

CentOS7.7
[root@localhost ~]#reboot

サーバ再起動後、以下コマンドを入力します。

CentOS7.7
#Disabledと表示されればOKです。
[root@localhost ~]#getenforce

SSHの接続ポート変更

SSHの接続ポート確認をします。

CentOS7.7
[root@localhost ~]#vi /etc/ssh/sshd_config

設定ファイルを以下へ変更します。

#ポート番号は予約されていない番号であればなんでもOKです。
Port 22→Port 22222

サービスを再起動します。

CentOS7.7
#注:再起動するとSSHで接続できなくなりますので複数のウインドウで開いてください。
[root@localhost ~]#systemctl restart sshd

Firewallの設定変更が必要になります(firewalldを無効にしていたら飛ばしてOK)。
以下コマンドを入力し、設定ファイルをコピーします。

CentOS7.7
[root@localhost ~]#cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ssh-22222.xml

編集します。

CentOS7.7
vi /etc/firewalld/services/ssh-22222.xml

設定ファイルを以下へ変更します。

/etc/firewalld/services/ssh-22222.xml
#ここでは22222とします。
#<port protocol="tcp" port="22"/>
<port protocol="tcp" port="22222"/>

設定ファイルをFirewallに反映させます。

CentOS7.7
#successが表示されてたらOKです。
[root@localhost ~]#firewall-cmd --reload

ファイアウォールに設定を入れます。

CentOS7.7
#successが表示されてたらOKです。
#再起動してもFirewallに設定が反映された状態になります。
firewall-cmd --permanent --add-service=ssh-22222

ファイアウォールのポートが開いているか以下コマンドで確認します。

CentOS7.7
#設定した[ssh-22222]が存在していたらOKです。
[root@localhost ~]#firewall-cmd --list-all

ユーザの作成

ユーザの作成をします。

CentOS7.7
#USERNMEは任意の名前を入力します。
#例: useradd hogehoge
[root@localhost ~]#useradd {USERNAME}

ユーザのパスワード設定

ユーザのパスワードを決めます。

CentOS7.7
#{USERNAME}は上記で作成したユーザをしようします。
#例: passed hogehoge
[root@localhost ~]#passwd {USERNAME}

sudoの設定

sudoの使用許可設定をします。
以下コマンドを入力します。
wheelグループにユーザを追加します。

CentOS7.7
#{USERNAME}は先ほど作成したユーザです。
#例: usermod -G wheel hogehoge
[root@localhost ~]#usermod -G wheel {USERNAME}

以下コマンドを入力し、wheelグループに追加したユーザが所属していることを確認します。

CentOS7.7
#例: cat /etc/group | grep hogeghoge
[root@localhost ~]#cat /etc/group | grep {USERNAME}

以下コマンドを入力します。
sudoで接続できるユーザおよびグループを管理します。

CentOS7.7
[root@localhost ~]#visudo 

以下の行のコマンドがコメントアウト(#)がされていないことを確認
%wheel =ALL(ALL) ALL

#%wheel =ALL(ALL) ALL
%wheel =ALL(ALL) ALL

wheelグループに追加したユーザでログインします。

CentOS7.7
localhost login: hogehoge

sudo を使用してファイルを編集してみます。

CentOS7.7
#ファイルの編集が許可されて、編集できれば完了です。
[hogehoge@localhost ~]#sudo vi /etc/hostname

NTPの設定

NTPの設定をします。
以下のコマンドを入力します。

CentOS7.7
[root@localhost ~]#vi /etc/chrony.conf

User public servers from the pool.ntp.org projectの項目の配下に
設定が投入されていたらすべてコメントアウトします。

/etc/chrony.conf
例:
#User public servers from the pool.ntp.org project
#server 0.centos.pool.ntp.org iburst
#server 0.centos.pool.ntp.org iburst
#server 0.centos.pool.ntp.org iburst
#server 0.centos.pool.ntp.org iburst

その後、使用するNTPサーバの設定を投入します。
ここでは、パブリックNTPサーバの[ ntp.jst.mfeed.ad.jp ]を使用します。

/etc/chrony.conf
#User public servers from the pool.ntp.org project
#server 0.centos.pool.ntp.org iburst
#server 0.centos.pool.ntp.org iburst
#server 0.centos.pool.ntp.org iburst
#server 0.centos.pool.ntp.org iburst
server ntp.jst.mfeed.ad.jp

NTPサービスを再起動します。

CentOS7.7
[root@localhost ~]#systemctl restart chronyd.service

NTPサーバとの同期がとれているか確認します。

CentOS7.7
#[ntp.jst.mfeed.ad.jp]と同期が取れていればOKです。
[root@localhost ~]#chronyc sources 

RootユーザによるSSH接続禁止設定

rootユーザでのSSHでの接続禁止の設定をします。
SSHの設定ファイルを開きます。

CentOS7.7
[root@localhost ~]#vi /etc/ssh/sshd_config

以下の設定を変更します。

#PermitRootLogin yes
PermitRootLogin no

SSHのサービスを再起動します。

CentOS7.7
[root@localhost ~]#systemctl restart sshd

firewallサービスの無効化

firewallサービスの無効化をします。
firewallサービスが必要であればスキップしてください。
firewalld.serviceを停止します。

CentOS7.7
[root@localhost ~]#systemctl stop firewalld.service

firewalld.serviceを無効化します。

CentOS7.7
※再起動してもサービスが立ち上がりません。
[root@localhost ~]#systemctl disable firewalld.service

rebootします。
※再起動するのはサービス停止が永続化していることを確認するためで
それが不要であれば飛ばして構いません。

CentOS7.7
[root@localhost ~]#reboot

再起動後、firewallサービスが起動していないことを確認します。

CentOS7.7
[root@localhost ~]#systemctl status firewalld.service

suの制限設定

suできるユーザの制限をします。
以下のsu設定ファイルを開きます。

CentOS7.7
[root@localhost ~]#vi /etc/pam.d/su

以下のコメントアウトを消してください。
wheelグループに追加されているユーザのみsuできるようにします。

CentOS7.7
#auth required pam_wheel.so use_uid quiet
auth required pam_wheel.so use_uid quiet

wheelグループに追加されていないユーザでrootにsuをした場合に失敗することを確認します。
wheelグループに追加されているユーザでrootにsuをした場合に成功することを確認します。

CentOS7.7
rootユーザでログインできることを確認できたらOKです。
[hogehoge@localhost ~]#su hogehoge

ホスト名の設定

ホスト名の設定をします。

CentOS7.7
[hogehoge@localhost ~]#vi /etc/hostname

以下の設定を変更します。

#localhost.localdmains
hogehoge.localdomains

rebootします。

CentOS7.7
[root@localhost ~]#reboot

再起動後、以下コマンドを入力します。

CentOS7.7
#設定が変更されていることを確認します。
[root@hogehoge ~]#hostname

タイムゾーンの設定

タイムゾーンの設定(CentOS7.7ではデフォルトで東京)
以下コマンドを入力します。

CentOS7.7
[root@hogehoge ~]#timedatectl set-timezone Asia/Tokyo

以下コマンドで確認してください。

CentOS7.7
#Asia/TokyoになっていればOKです。
[root@hogehoge ~]#timedatectl status

終わりに

必要な設定をピックアップして使ってください。
また、その他必要だと思われる設定がありましたら
コメントに記載いただけますと幸いです。

以上となります。

8
10
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
8
10