Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running ssh on Amazon EC2 instance on port other than 22

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 Amazon EC2 instance via Web Management Console with following 'user data':

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

The idea being that the above script would execute on instance startup and switch ssh from port 22 to port 80. (Ref: http://alestic.com/2010/12/ec2-ssh-port-80)

But ssh is still not accessible on port 80. Apparently 'user data' script is not being executed on start up?

I can 'only' start stop instances via Web Management Console, not from command-line (being behind firewall)

Any ideas?

like image 370
Jasper Avatar asked Nov 20 '12 14:11

Jasper


People also ask

How do I change the SSH port on AWS EC2 instance?

Launch and connect to EC2 instance running Amazon Linux 2. 2. Promote to root and edit /etc/ssh/sshd_config ## sudo vi /etc/ssh/sshd_config 3. Edit line 17 (usually 17) #PORT 22.

Can I SSH from AWS instance to AWS instance?

To connect from the Amazon EC2 console Open the Amazon EC2 console. In the left navigation pane, choose Instances and select the instance to which to connect. Choose Connect. On the Connect To Your Instance page, choose EC2 Instance Connect (browser-based SSH connection), Connect.

What are the 3 different methods that you connect to a EC2 instance?

AWS support many ways to let you connect to your servers(EC2), we will introduce three methods : SSH, Instance Connect, System Manager and deep dive in EC2 Instance Connect and System Manager – Session Manager.


3 Answers

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.

like image 98
Watchmaker Avatar answered Oct 17 '22 19:10

Watchmaker


The amazon firewall blocks all ports other than 22. You first have to enable port 80/443/whatever.

HOWTO: Go to "security groups" -> click on the group you chose for your instance, then on the "Inbound" tab.

There you can add your ports.

EDIT: If by chance you also installed apache or some other webserver, port 80 will be used and cannot be used by sshd. I do not know which operating system is installed on your server, but maybe some webserver is already included?

EDIT 2: As per the last comment, it seems nowadays all ports are blocked by default. So you will have to open port 22 if you need it. Wasn't the case eight years ago, but configurations change ;)

like image 30
Jens Avatar answered Oct 17 '22 19:10

Jens


Here is what I came up with to run sshd on 443 and 22 having rhel8 on ec2

  1. make sure your security groups allow connection from your network/ip to the desired ports (in my case 22 and 443)
tcp 443 1.2.3.4/32 #allow access to 443 from IP 1.2.3.4
tcp 22 1.2.3.4/32 #allow access to 22 from IP 1.2.3.4
  1. Login to the EC2 and
#install semanage with
sudo yum install -y policycoreutils-python-utils
#delete 443 from http ports
sudo semanage port -d -t http_port_t -p tcp 443
#add 443 to ssh ports
sudo semanage port -m -t ssh_port_t -p tcp 443
  1. Edit /etc/ssh/sshd_config
Port 22
Port 443
  1. Restart sshd
sudo service sshd restart
like image 1
Anton Avatar answered Oct 17 '22 20:10

Anton