Running ssh on Amazon EC2 instance on port other than 22

前端 未结 3 1627
孤城傲影
孤城傲影 2020-12-14 20:33

I am not able to access Amazon EC2 instance via ssh as i am behind a firewall. So, i thought of running ssh on port other than 22, like 80 or 443.

I tried starting A

3条回答
  •  孤城傲影
    2020-12-14 21:13

    To connect to an AWS instance through ssh from a port different than default 22:

    1. Open the security group of your instance so that it allows connections to that port from the source that you choose (0.0.0.0/0 for any source).
    2. In your instance:

      • It is a new instance you could use an user-data script like this one:

      #!/bin/bash -ex perl -pi -e 's/^#?Port 22$/Port 443/' /etc/ssh/sshd_config service sshd restart || service ssh restart

    Please note that this only works if you are launching a new instance:

    User data scripts and cloud-init directives only run during the first boot cycle when an instance is launched.

    • If it is not a new Instance, edit the /etc/ssh/sshd_config file adding/changing Port 22 to the port that you want (i.e: Port 443) to connect through ssh and then do service ssh restart and you should be done.

    Note: I did this with an Ubuntu instance, with another Linux instances may be slightly different.

提交回复
热议问题