前回は、rootでSSH接続できない設定を行った。
今回は、SSHの接続ポートを変更して、セキュリティを高めていく。
XServer VPSのパケットフィルター設定変更
まず、VPS パネルの「パケットフィルター設定」で、変更後のポート番号を通信可能にする。

変更後の適当なポート番号(例:2222)を追加する。

追加したポート番号で、マシンとの通信は可能となった。

SSH接続ポートの設定変更
SSHの設定ファイルを編集して、SSH接続のポート番号を変更する。
[nori@hostname ~]$ sudo vi /etc/ssh/sshd_config
「Port」の設定を変更後のポート番号に変更。
# 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 2222
SSHサービスを再起動。
[nori@hostname ~]$ sudo systemctl restart sshd
Job for sshd.service failed because the control process exited with error code.
See "systemctl status sshd.service" and "journalctl -xeu sshd.service" for details.
だが、エラーとなり起動しない。
原因はSELinuxのセキュリティによるもの。
設定ファイルにも親切に書いてあった。
# 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
SELinuxの許可ポートを追加
まず、sshとして、どのポートが開いているのかを確認する。
[nori@hostname ~]$ semanage port --list | grep ssh
-bash: semanage: command not found
が、ツールがインストールされていないため、まずはインストール。
[nori@hostname ~]$ sudo dnf -y install policycoreutils-python-utils
:
Installed:
policycoreutils-python-utils-3.6-2.1.el9.noarch
Complete!
SELinuxのSSH用ポート番号を追加。
[nori@hostname ~]$ sudo semanage port --list | grep ssh
ssh_port_t tcp 22
[nori@hostname ~]$ sudo semanage port -a -t ssh_port_t -p tcp 2222
[nori@hostname ~]$ sudo semanage port --list | grep ssh
ssh_port_t tcp 2222, 22
改めてSSHのサービスを再起動。
[nori@hostname ~]$ sudo systemctl restart sshd
変更したポートでSSH接続ができる状態になったので、接続テスト。
user@MacBookPro ~ % ssh -i ~/.ssh/id_rsa nori@servername.example.com -p 2222
Last login: Sat Mar 15 15:48:15 2025 from 999.999.999.999
[nori@hostname ~]$
XServer VPSのパケットフィルター設定変更
VPS パネルの「パケットフィルター設定」で、変更前のポートを閉鎖する。


変更後のポートのみが設定された状態となったことを確認。

次回は、メールの設定を行う。
お友達紹介プログラムを利用して契約すると10%OFFになるそうなので、
もしよろしければ次の画像をクリックしてみてください。

投稿一覧はこちら→「VPSでAlmaLinux」