LoginSignup
68
72

More than 5 years have passed since last update.

CentOS7のfirewalldでsshのポート番号を変更する方法

Last updated at Posted at 2015-04-06

sshのポート番号を変更するには、

  • sshdの設定変更(/etc/ssh/sshd_config を変数してListenするポート番号を変更する)
  • パケットフィルタリング(firewalldやiptables)のポート番号を変更する

の2つを行う必要があります。
本記事では、sshdの設定変更が済んでいるものとして、firewalldでsshサービスのポート番号を変更する方法を解説します。

firewalldを起動する(ついでに自動起動もonに)

何はなくとも起動します。
レンタルサーバやクラウドサービスだと最初から起動してると思いますが、vagrant環境だと起動していませんでした。
(まあローカル環境だと起動する必要もないわけですが、そこは勉強のためということで)

[root@localhost ~]# systemctl enable firewalld
ln -s '/usr/lib/systemd/system/firewalld.service' '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
ln -s '/usr/lib/systemd/system/firewalld.service' '/etc/systemd/system/basic.target.wants/firewalld.service'
[root@localhost ~]# systemctl start firewalld

確認します。

[root@localhost ~]# systemctl status firewalld
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: active (running) since Mon 2015-04-06 14:33:20 UTC; 1s ago
 Main PID: 10921 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─10921 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Apr 06 14:33:20 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.

/usr/lib/firewalld/ と /etc/firewalld/ の使い分けを知る

/usr/lib/firewalld/ 以下にはシステムデフォルトの設定ファイルが置かれています。ここは基本的にいじってはいけません。

自分用にカスタマイズしたい場合は、/usr/lib/firewalld/にあるファイルを /etc/firewalld/にコピーしてから編集します。

/etc/firewalld/下にファイルが存在する場合は、それによってシステム設定が上書きされます。

/usr/lib/firewalld/services/ssh.xml を見る

サービスごとの設定ファイルは/usr/lib/firewalld/services/の下にあります。
確認してみましょう。

[root@localhost ~]# cat /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>

sshのポート番号が記述されていますね。
ではポート番号を変更したいのでこのファイルを編集し・・・おっと/usr/lib/の下をいじってはいけないルールでした。

/etc/firewalld/services/ssh.xml ファイルを作る

上で説明したとおり、/etc/firewalld/ の下にファイルを作ればこの設定でシステムデフォルトを上書きできます。
作るといってもコピーして編集するだけです。

cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ssh.xml

ポート番号を書き換えます。

/etc/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"/>
+  <port protocol="tcp" port="1234"/>
</service>

設定を反映する

[root@localhost ~]# firewall-cmd --reload
success

これでfirewalldのsshポート番号を変更することができました。

おまけ:ポート番号を複数指定することもできる

上のport番号を記述するところで、

  <port protocol="tcp" port="22"/>
  <port protocol="tcp" port="1234"/>

のように書くと複数指定できます。

SSHだと複数ポート空ける必要はないですが、HTTPポートを80と8080を使いたいときなんかにこの方法を使うとよいでしょう。

68
72
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
68
72