Contents
1.SSHによるリモート接続の設定
SSHはサーバーへリモートで接続するためのサービスで、基本的にはOSインストール直後に動作していますが、デフォルトの設定ではややセキュリティに難があります。
ここではデフォルトの設定を変更してssh接続のセキュリティを高める設定を行います。
1.1 SSHサービスの設定ファイル変更
SSHサービスの設定を変更するために設定ファイルを変更します。
SSHサービスの設定ファイルは"/etc/ssh/sshd_config"になります。
# $OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj 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.
# 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
# This system is following system-wide crypto policy. The changes to
# crypto properties (Ciphers, MACs, ...) will not have any effect here.
# They will be overridden by command-line options passed to the server
# on command line.
# Please, check manual pages for update-crypto-policies(8) and sshd_config(5).
# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
17行目「Port 22」今回は「Port 2244」に変更して進めていきます
20行目「#ListenAddress 0.0.0.0」の「#」を削除します
44行目「PermitRootLogin yes」を「PermitRootLogin no」に変更
rootユーザーはユーザー名が既に判明しているためパスワードが判明すると、管理者権限でサーバーへログイン出来るため、これを拒否する設定を行います。
SSH の再起動
2.ファイアウォール(firewalld)の設定方法
Rockyではファイアウォールはfirewalldがデフォルトに設定されており、OSインストール時に有効になっています。
「firewalld」について簡単に説明すると、通信制御のポリシーを設定する場合、事前に定義されたゾーンに対して通信の許可・遮断ルールを適用し、そのゾーンを各NIC(ネットワークアダプタ)に割り当てていくという方式になっています。
2.1 「firewalld」を制御するための「firewall-cmd」コマンド使用方法
1)「fierwalld」の状態や設定内容を確認するためのコマンド
①firewalld稼働状況確認
running
「firewalld」が動作している場合は「running」、停止している場合は「not running」と表示されます
または
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2022-06-07 22:09:30 JST; 4min 27s ago
Docs: man:firewalld(1)
Main PID: 993 (firewalld)
Tasks: 2 (limit: 11170)
Memory: 34.1M
CGroup: /system.slice/firewalld.service
mq993 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid
※停止している場合
Active: inactive (dead)と表示され、firewallが停止していることが分かります
➁defaultゾーン設定を表示
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」で設定を反映させる必要があります
# firewall-cmd --add-service=http --permanent
# firewall-cmd --reload
④起動・停止方法
「firewalld」は「systemd」で制御されているので、起動と停止には「systemctl」コマンドを使用
# systemctl start firewalld
firewalldの停止
# systemctl stop firewalld
2.2 変更したSSHポート2244番を解放する
# 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」Passphrasee」を入力し、「OK」をクリック
情報が正しければ下図のように正常にログインできるはずです。
4.NTP サーバーの設定
サーバの時刻を日本標準時に同期させるNTP サーバーを構築します
①Chrony のインストールと設定
3行目 : コメントアウトにして、その下に追加
#pool 2.pool.ntp.org iburst
pool ntp.nict.jp iburst
# systemctl restart chronyd.service
# firewall-cmd --reload
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分ぐらいかかります。)