業務用エアコン関連の技術情報、エラーコード、環境問題対策に関する別サイト「エアコンの安全な修理・適切なフロン回収」

MiracleLinux9.0 : SSH , Firewalld , NTP サーバー

1.SSHによるリモート接続の設定

SSHはサーバーへリモートで接続するためのサービスで、基本的にはOSインストール直後に動作していますが、デフォルトの設定ではややセキュリティに難があります。
ここではデフォルトの設定を変更してssh接続のセキュリティを高める設定を行います。

1.1 SSHサービスの設定ファイル変更

SSHサービスの設定を変更するために設定ファイルを変更します。
SSHサービスの設定ファイルは"/etc/ssh/sshd_config"になります。

# vi /etc/ssh/sshd_config
#       $OpenBSD: sshd_config,v 1.104 2021/07/02 05:11:21 dtucker Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

# To modify the system-wide sshd configuration, create a  *.conf  file under
#  /etc/ssh/sshd_config.d/  which will be automatically included below
Include /etc/ssh/sshd_config.d/*.conf

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
Port 2244
#AddressFamily any
ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

21行目「Port 22」今回は「Port 2244」に変更して進めていきます
24行目「#ListenAddress 0.0.0.0」の「#」を削除します
41行目「#PermitRootLogin prohibit-password」「#」を削除します
rootユーザーはユーザー名が既に判明しているためパスワードが判明すると、管理者権限でサーバーへログイン出来るため、これを拒否する設定を行います。

SSH の再起動

# systemctl restart sshd.service

このままでは次に再起動したときSSHによるリモート接続ができなくなるので、次のファイアウォールの設定でSSHポート2244番を解放してください。

2.ファイアウォール(firewalld)の設定方法

MiracleLinuxではファイアウォールはfirewalldがデフォルトに設定されており、OSインストール時に有効になっています。

「firewalld」について簡単に説明すると、通信制御のポリシーを設定する場合、事前に定義されたゾーンに対して通信の許可・遮断ルールを適用し、そのゾーンを各NIC(ネットワークアダプタ)に割り当てていくという方式になっています。

2.1 「firewalld」を制御するための「firewall-cmd」コマンド使用方法

1)「fierwalld」の状態や設定内容を確認するためのコマンド

①firewalld稼働状況確認

# firewall-cmd --state
running
「firewalld」が動作している場合は「running」、停止している場合は「not running」と表示されます

または

➁defaultゾーン設定を表示

# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client ssh
ports:
protocols:
forward: no
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

上記の例ではサービス「 cockpit」「 dhcpv6-client」「 ssh」が許可されていること等がわかります

③「--permanent」オプションについて
サーバの再起動や「firewalld」サービスの再起動で設定が消えてしまわないようにするためには、
「--permanent」オプションを使用して設定を行う必要があります。その際、「--permanent」オプションを指定して設定を行った場合は、そのままでは「firewalld」に設定が反映されないため「fiewall-cmd  --reload」で設定を反映させる必要があります

例としてHTTPサービスをシステムを再起動しても初期化されず恒久的に利用するためには

# firewall-cmd --add-service=http --permanent
# firewall-cmd --reload

④起動・停止方法

「firewalld」は「systemd」で制御されているので、起動と停止には「systemctl」コマンドを使用

firewalldの起動
# systemctl start firewalld
firewalldの停止
# systemctl stop firewalld

2.2  変更したSSHポート2244番を解放する

# firewall-cmd --add-port=2244/tcp --permanent
# firewall-cmd --reload
# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client ssh
ports: 2244/tcp
protocols:
forward: no
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

2244 port が追加されている

3.Windowsからリモート接続

Windowsでの設定

Windowsからリモート接続するための設定を始めます、ターミナルエミュレーターは「Tera Term」を使用します。
Tera Termを起動し、起動画面のキャンセルをした後、Tera Termメニューの「File」から「New Connection」を選択する。

「サーバーのIPアドレス」「TCPポート番号」の箇所は各自で設定したものを入力します。最後に「OK」をクリック

「OK」をクリックすると、セキュリティの確認の画面になるので「Continue」をクリック

次の画面になるので、「User name」Passphrase」を入力し、「OK」をクリック

情報が正しければ正常にログインできるはずです。

4.NTP サーバーの設定

サーバの時刻を日本標準時に同期させるNTP サーバーを構築します

①Chrony のインストールと設定

# dnf -y install chrony
# vi /etc/chrony.conf
3行目 : コメントアウトにして、その下に追加
#pool 2.cloudlinux.pool.ntp.org iburst
pool ntp.nict.jp iburst

②chrony を再起動しシステム再起動後も chrony を有効にします

# systemctl enable chronyd.service
# systemctl restart chronyd.service

③firewallでNTPポート解放

# firewall-cmd --add-service=ntp --permanent
# firewall-cmd --reload

④chronyd のステータス( 動作 )の確認

# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
==============================================================
^+ ntp-b2.nict.go.jp 1 6 77 27 -50us[ -342us] +/- 5432us
^+ ntp-b3.nict.go.jp 1 6 77 26 +103us[ +103us] +/- 5681us
^* ntp-k1.nict.jp 1 6 77 26 -250us[ -544us] +/- 3445us
^+ ntp-a2.nict.go.jp 1 6 77 27 -211us[ -503us] +/- 5513us

*印がついていたら同期ができています。 (起動してから同期までに10分ぐらいかかります。)

タイトルとURLをコピーしました