sshd は標準で 22/TCP を Listen(待ち受け)します。しかし、標準設定のままにしておくとあちこちからアクセスされ、セキュリティ強度が高いとは言えません。そこで、今回は CentOS7 で sshd が Listen するポートを 22222/TCP に変更してみます。
サービス(22222/TCP)の定義
一般的なサービスが Listen するポートは /usr/lib/firewalld/services ディレクトリ配下で定義されています。今回はポート変更に際して新たに以下のサービスを定義します。
項目 | 値 |
---|---|
サービス名(任意) | SSH-22222 |
ポート/プロトコル | 22222/TCP |
ベースにするファイル | /usr/lib/firewalld/services/ssh.xml |
新たに作成するファイル | /etc/firewalld/services/ssh-22222.xml |
ベースにする /usr/lib/firewalld/services/ssh.xml は以下のように定義されています。
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>SSH</short>
<description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
<port protocol="tcp" port="22"/>
</service>
/etc/firewalld/services/ssh-22222.xml というファイルを以下の内容で新規作成します。
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>SSH-22222</short>
<description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
<port protocol="tcp" port="22222"/>
</service>
firewalld へのサービス追加
新たに定義した「ssh-22222」サービスを firewalld に反映します。
firewall-cmd --reload
firewall-cmd --add-service=ssh-22222
firewall-cmd --permanent --add-service=ssh-22222
sshd の再起動
/etc/ssh/sshd_config ファイル中の port 設定を以下のように書き換えます。
Port 22
↓
Port 22222
sshd を再起動します。但し、SELinux が有効化された状態でポート番号を変更した sshd を再起動しようとしてもエラーになってしまい、設定変更が反映されません。よって、一時的に SELinux を無効化してから sshd を再起動します。まず、/etc/sysconfig/selinux を以下のように変更します。
SELINUX=enforcing
↓
SELINUX=disabled
SELinux の設定変更が完了したら一度、システムを再起動します。これで SELinux が無効化され、sshd も変更されたポートで起動してくるはずです。22222/TCP で ssh アクセス出来ることを確認します。今後は 22222/TCP で ssh アクセスしているターミナルで作業します。
不要になった ssh(22/TCP)の削除
22/TCP は不要になった為、firewalld から削除します。
firewall-cmd --remove-service=ssh
firewall-cmd --permanent --remove-service=ssh
コメント