3
3

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.

EC2、SSHポート番号を変更するための手順。

Last updated at Posted at 2020-07-25

はじめに

EC2インスタンスにSSHで接続する際ですが、ポート番号をデフォルトの22番ではなく、その他の番号にすることで、少しでもハッキングの危険から逃れたりと、セキュリティ向上に寄与出来るのではないかと思われます。
ということで、今回は題名の通り、SSHポート番号を変更する手順について書きます。

手順

  • 一時的にセキュリティーグループのインバウンドで22番を認めます。

  • 公開鍵を使用して、EC2インスタンスにssh接続をします。

  • EC2インスタンス内でsshd_configファイルの記述を変更します。具体的には下記コマンドを打ってください。

    • cd /etc/ssh
    • sudo vi sshd_config (viエディターでファイルを開いています。)
    • #port22 のコメントアウトを外す
    • port2222に書き換える。(今回は例でポート番号を2222にしていますが、ここを自分の変更したいポート番号に設定してください。)
    • sudo service sshd restart
  • セキュリティグループのインバウンドで、カスタムTCPルール(今回の場合、TCP2222)を開放してください。

  • 一時的に認めていた22番はインバウンドルールから外しましょう。

  • ssh接続のコマンドで、末尾に -p 2222を付けて接続します。具体的には以下のようなコマンドになります。

    • ssh -i .ssh/秘密鍵ファイル ec2-user@パブリックIP -p 2222

まとめ

簡単に手順だけをまとめましたが、これで以上になります。
間違っている点などがありましたら、コメントで教えて頂けますと、幸いです。
ありがとうございました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?