Linux EC2インスタンスのポート番号変更でセキュリティを強化する方法

2025/01/20に公開

こんにちは!
SSH接続のポート番号といえば「22」ですよね。
と言えば全員がそうだよねと頷くぐらいにはSSHの接続ポートは22番ポートというのは浸透している話だと思います。
これが何を指しているかと言うと、「悪い人たち」もそのことを知っているわけです。

と言うことで、本記事ではAWSのLinuxインスタンスにSSH接続に使用するポート番号を変更する手順を紹介します。

簡潔に記載すると、①AWSのセキュリティグループの設定を変更。②インスタンスのsshd_configを編集して③sshdサービスを再起動後④実際にそのポート番号を使って接続できるかをテストする
と言うだけの簡単な手順になります。

今回は以下のAWSドキュメントを参考にし、「32768」のポートを使用するように設定したいと思います。
(本当はもっとわかりづらいポートがいいというツッコミは今回受け付けません笑)

https://docs.aws.amazon.com/ja_jp/vpc/latest/userguide/nacl-ephemeral-ports.html

0. 使用環境

・EC2インスタンス
 Amazon Linux 2023
・使用PC
 Mac

1.セキュリティグループのインバウンドルールを編集

まずは忘れないうちにAWSのマネージメントコンソールからセキュリティグループのインバウンドルールを編集し、「32768」のポートを解放します。

タイプ:カスタムTCP
ポート範囲:32768
ソース:Anywhere-IPv4(実際には接続する端末のIPのみに絞るなどしてください)

2.ルートユーザーでログイン

これはこれで甘え意欲ないですが、毎回sudoを入力するのは煩わしい大変だったので今回はルート権限での操作を行います。

$ sudo -s
# 

3.sshd_configのバックアップ取得

もしもの時のために、sshd_configのバックアップを取得しておきます。
また、sshd_configのあるディレクトリには随分と似た名前の「ssh_config」も存在しているので間違いのないように気をつけてください。

①sshd_configがあることを確認

# cd /etc/ssh
# ls -l
total 592
-rw-r--r--. 1 root root     578094 Oct 16 20:34 moduli
-rw-r--r--. 1 root root       1921 Oct 16 20:34 ssh_config←間違い
drwxr-xr-x. 2 root root         28 Jan 13 21:14 ssh_config.d
-rw-r-----. 1 root ssh_keys    557 Jan 14 22:05 ssh_host_ecdsa_key
-rw-r--r--. 1 root root        212 Jan 14 22:05 ssh_host_ecdsa_key.pub
-rw-r-----. 1 root ssh_keys    452 Jan 14 22:05 ssh_host_ed25519_key
-rw-r--r--. 1 root root        132 Jan 14 22:05 ssh_host_ed25519_key.pub
-rw-------. 1 root root       3854 Jan 13 21:14 sshd_config←正解
drwx------. 2 root root         28 Jan 13 21:14 sshd_config.d

②sshd_configのバックアップを作成します。

# cp sshd_config sshd_config.bk
# ls -l
total 596
-rw-r--r--. 1 root root     578094 Oct 16 20:34 moduli
-rw-r--r--. 1 root root       1921 Oct 16 20:34 ssh_config
drwxr-xr-x. 2 root root         28 Jan 13 21:14 ssh_config.d
-rw-r-----. 1 root ssh_keys    557 Jan 14 22:05 ssh_host_ecdsa_key
-rw-r--r--. 1 root root        212 Jan 14 22:05 ssh_host_ecdsa_key.pub
-rw-r-----. 1 root ssh_keys    452 Jan 14 22:05 ssh_host_ed25519_key
-rw-r--r--. 1 root root        132 Jan 14 22:05 ssh_host_ed25519_key.pub
-rw-------. 1 root root       3854 Jan 13 21:14 sshd_config
-rw-------. 1 root root       3854 Jan 19 00:29 sshd_config.bk←正常にバックアップできている
drwx------. 2 root root         28 Jan 13 21:14 sshd_config.d

4.sshd_configの編集

sshd_configの内容を編集していきます。

# vi sshd_config

編集前(一部抜粋)

# 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
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

編集後(一部抜粋)

# 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 32768 ←追記
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

編集内容が保存されていることを確認します。

# cat sshd_config | grep -i port
# If you want to change the port on a SELinux system, you have to tell
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
Port 22
Port 32768
# WARNING: 'UsePAM no' is not supported in Fedora and may cause several
#GatewayPorts no

5.sshdサービスの再起動

sshdサービスの再起動と再起動後の確認を行います。
Active: active (running)を確認でき、since以降の時間が直近のものであれば正常に再起動が完了しています。

# systemctl restart sshd
# systemctl status sshd
● sshd.service - OpenSSH server daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; preset: enabled)
     Active: active (running) since Sun 2025-01-19 00:35:57 UTC; 8s ago
       Docs: man:sshd(8)
             man:sshd_config(5)
   Main PID: 3207 (sshd)
      Tasks: 1 (limit: 1111)
     Memory: 1.2M
        CPU: 8ms
     CGroup: /system.slice/sshd.service
             └─3207 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

Jan 19 00:35:57 ip-10-0-12-99.ap-northeast-1.compute.internal sshd[3207]: Server listening >
Jan 19 00:35:57 ip-10-0-12-99.ap-northeast-1.compute.internal systemd[1]: Starting sshd.ser>
Jan 19 00:35:57 ip-10-0-12-99.ap-northeast-1.compute.internal sshd[3207]: Server listening >
Jan 19 00:35:57 ip-10-0-12-99.ap-northeast-1.compute.internal systemd[1]: Started sshd.serv>
Jan 19 00:35:57 ip-10-0-12-99.ap-northeast-1.compute.internal sshd[3207]: Server listening >
Jan 19 00:35:57 ip-10-0-12-99.ap-northeast-1.compute.internal sshd[3207]: Server listening

6.設定したポートでの接続確認

設定したポートでのSSH接続を試します。
今回はMacのターミナルからの接続です。他の環境(Teratermとか)からの接続を試したい方はそちらの接続方を調べてみてください。

% ssh -p [ポート番号] -i "[キーペア名]" ec2-user@[DNS名orIPアドレス]
A newer release of "Amazon Linux" is available.
  Version 2023.6.20250115:
Run "/usr/bin/dnf check-release-update" for full release and version update info
   ,     #_
   ~\_  ####_        Amazon Linux 2023
  ~~  \_#####\
  ~~     \###|
  ~~       \#/ ___   https://aws.amazon.com/linux/amazon-linux-2023
   ~~       V~' '->
    ~~~         /
      ~~._.   _/
         _/ _/
       _/m/'
Last login: Sun Jan 19 00:29:18 2025 from 49.109.109.215

7. 22番ポート設定の無効化

32768番のポートでの接続確認ができたので、22番ポートの設定は削除してしまいましょう。

# vi sshd_config
# cat sshd_config | grep -i port
# If you want to change the port on a SELinux system, you have to tell
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#Port 22 ←#をつけ無効化
Port 32768
# WARNING: 'UsePAM no' is not supported in Fedora and may cause several
#GatewayPorts no
# systemctl restart sshd
# systemctl status sshd
● sshd.service - OpenSSH server daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; preset: enabled)
     Active: active (running) since Sun 2025-01-19 00:45:20 UTC; 2s ago
       Docs: man:sshd(8)
             man:sshd_config(5)
   Main PID: 3688 (sshd)
      Tasks: 1 (limit: 1111)
     Memory: 1.2M
        CPU: 8ms
     CGroup: /system.slice/sshd.service
             └─3688 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

Jan 19 00:45:20 ip-10-0-12-99.ap-northeast-1.compute.internal sshd[3688]: Server listening >
Jan 19 00:45:20 ip-10-0-12-99.ap-northeast-1.compute.internal systemd[1]: Starting sshd.ser>
Jan 19 00:45:20 ip-10-0-12-99.ap-northeast-1.compute.internal sshd[3688]: Server listening >
Jan 19 00:45:20 ip-10-0-12-99.ap-northeast-1.compute.internal systemd[1]: Started sshd.ser

8.22番ポートでの接続確認

接続不可のメッセージが出力されました。

% ssh -i "[キーペア名]" ec2-user@[DNS名orIPアドレス]         
ssh: connect to host ec2-54-238-152-98.ap-northeast-1.compute.amazonaws.com port 22: Connection refused

また、念には念を入れてセキュリティグループの22番ポートの設定も削除しておきましょう。

EC2インスタンスのポート変更の手順は以上となります。
もちろん、これだけでセキュリティの全てが担保できるわけではありませんが、セキュリティ強化の第一歩目となったのではないでしょうか。

おしまい。

Discussion