在非 22 端口的 Amazon EC2 实例上运行 ssh [英] Running ssh on Amazon EC2 instance on port other than 22
问题描述
我无法通过 ssh 访问 Amazon EC2 实例,因为我在防火墙后面.所以,我想在 22 以外的端口上运行 ssh,比如 80 或 443.
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.
我尝试使用以下用户数据"通过 Web 管理控制台启动 Amazon EC2 实例:
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
想法是上面的脚本将在实例启动时执行并将 ssh 从端口 22 切换到端口 80.(参考:http://alestic.com/2010/12/ec2-ssh-port-80)
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)
但是在端口 80 上仍然无法访问 ssh.显然在启动时没有执行用户数据"脚本?
But ssh is still not accessible on port 80. Apparently 'user data' script is not being executed on start up?
我只能通过 Web 管理控制台启动停止实例,而不是从命令行(在防火墙后面)
I can 'only' start stop instances via Web Management Console, not from command-line (being behind firewall)
有什么想法吗?
推荐答案
从不同于默认 22 的端口通过 ssh 连接到 AWS 实例:
To connect to an AWS instance through ssh from a port different than default 22:
- 打开您的实例的安全组,以便它允许从您选择的源(0.0.0.0/0 为任何源)连接到该端口.
在您的实例中:
- 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).
In your instance:
- 这是一个新实例,您可以使用像这样的用户数据脚本:
#!/bin/bash -experl -pi -e 's/^#?Port 22$/Port 443/'/etc/ssh/sshd_config服务 sshd 重启 ||服务 ssh 重启
请注意,这仅在您启动新实例时有效:
Please note that this only works if you are launching a new instance:
用户数据脚本和 cloud-init 指令仅在实例启动时的第一个引导周期内运行.
User data scripts and cloud-init directives only run during the first boot cycle when an instance is launched.
- 如果它不是新实例,请编辑
/etc/ssh/sshd_config
文件,将Port 22
添加/更改为您想要的端口(即:端口443
)通过ssh连接,然后执行service ssh restart
就可以了. - If it is not a new Instance, edit the
/etc/ssh/sshd_config
file adding/changingPort 22
to the port that you want (i.e:Port 443
) to connect through ssh and then doservice ssh restart
and you should be done.
注意:我是在一个 Ubuntu 实例上做的,另一个 Linux 实例可能略有不同.
Note: I did this with an Ubuntu instance, with another Linux instances may be slightly different.
这篇关于在非 22 端口的 Amazon EC2 实例上运行 ssh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!