3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

OCIのUbuntuインスタンスのSSHポートを変更する

Posted at

OCIのインスタンスは、デフォルトのSSHポートが22番となっており、そのままでは危険。
ここでは、ポート番号を20000番(お好みで)に変更することを目標とします。

SSHサーバの設定ファイルの変更

/etc/ssh/sshd_config を適切に書き換えます。(VimやEmacsを使って)

/etc/ssh/sshd_config
︙
#Port 22
︙

のコメントアウトを外し、さらに新しいポート番号の記述を追加します。
注)ポート番号の設定失敗を想定して、とりあえず22と20000の両方を開けています。

/etc/ssh/sshd_config
︙
Port 22
Port 20000
︙

iptablesの設定変更

OCIのUbuntuインスタンスではiptablesによるファイアウォールがデフォルトで有効になっています。

20000番のポートを許可します。

$ sudo iptables -I INPUT 5 -p tcp --dport 20000 -j ACCEPT

このままではインスタンス(サーバ)を再起動したときに、設定がリセットされてしまうので、永続化します。

$ sudo /etc/init.d/netfilter-persistent save
$ sudo /etc/init.d/netfilter-persistent reload

sudo iptables -L で設定済の内容を確認できます。

$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     udp  --  anywhere             anywhere             udp spt:ntp
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:20000
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
︙

VCNのセキュリティ・リストの設定変更

該当のVCNのセキュリティ・リスト > イングレス・ルール を変更します。
スクリーンショット 2022-03-18 9.58.50.png
既に22番ポートが許可されているので、編集を押して、宛先ポート範囲を20000に書き換えて保存します。

(再)SSHサーバの設定ファイルの変更

一度exitして、新しい20000ポートでログインし直します。
ログインできたら、sshd_configを再度編集して古い22ポートは閉じます。

/etc/ssh/sshd_config
︙
#Port 22
Port 20000
︙

参考

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?