在AWS上,我必须在EC2实例的防火墙以及安全组中打开端口吗?

如果我把我的SSH端口从22改为23453,我就不能再ssh了。

更详细地说,我在Amazon Web Services上使用了一个红帽EC2实例。 这是我全新安装的第二个更改(首先更改是添加一个非root用户)。

我可以很好地使用Git Bash和本地.ssh / config文件ssh,编辑当前说/ etc / ssh / sshd_config中的行

#Port 23453 

 Port 23453 

然后重新启动sshd

 sudo service sshd restart 

然后我添加一行“端口23453”我的.ssh / config文件

 Host foo Hostname my-ec2-public-DNS Port 23453 IdentityFile my ssl key 

如果我打开另一个Git Bash shell(不closures我现有的连接),并尝试ssh到我的实例(使用ssh foo),我看到以下错误:

 ssh: connect to host my-ec2-public-DNS port 23453: Bad file number 

连接到此实例的安全组有两个条目,都是TCP

22 (SSH) 0.0.0.0/0

23453 0.0.0.0/0

我最好的猜测是端口仍然被我的防火墙阻止。

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 tcp -- anywhere anywhere state NEW tcp dpt:ssh REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) target prot opt source destination REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) target prot opt source destination 

这对我来说很开放。

UPDATE

添加一个iptables规则后

 iptables -A INPUT -p tcp --dport 23453 -j ACCEPT 

再试一次,依然没有运气。

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 tcp -- anywhere anywhere state NEW tcp dpt:ssh REJECT all -- anywhere anywhere reject-with icmp-host-prohibited ACCEPT tcp -- anywhere anywhere tcp dpt:23453 Chain FORWARD (policy ACCEPT) target prot opt source destination REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) target prot opt source destination 

看起来足够开放。 我不完全知道如何查找端口上的数据包或活动。 但netstat -ntlp的输出(以root身份)

 Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:56137 0.0.0.0:* LISTEN 948/rpc.statd tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 930/rpcbind tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1012/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1224/master tcp 0 0 0.0.0.0:23453 0.0.0.0:* LISTEN 32638/sshd tcp 0 0 :::36139 :::* LISTEN 948/rpc.statd tcp 0 0 :::111 :::* LISTEN 930/rpcbind tcp 0 0 ::1:631 :::* LISTEN 1012/cupsd tcp 0 0 :::23453 :::* LISTEN 32638/sshd 

我似乎在23453上显示sshd。

我再次检查实例端口是否在安全组中打开(端口:23453,协议:tcp,源:0.0.0.0/0)

还有什么可以导致失败通过SSH连接?

干杯

死后

我现在可以连接。 这是iptables中的一个缺失规则。 iptables -L的输出如下所示:

 Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT icmp -- anywhere anywhere ACCEPT tcp -- anywhere anywhere tcp dpt:23453 state NEW ACCEPT all -- anywhere anywhere ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) target prot opt source destination REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) target prot opt source destination 

您的实例防火墙没有打开此端口。 尝试以下命令:

 iptables -I INPUT 3 -s 0.0.0.0/0 -d 0.0.0.0/0 -p tcp --dport 23453 -m state --state New -j ACCEPT 

请注意,iptables规则需要保存在重新启动后保持。 在RHEL上:

 /sbin/service iptables save 

你确定安全组设置正确吗? 您是否点击“应用更改”? 许多人忘记实际应用他们的变化:)

“不好的文件号”通常表示连接超时,你的iptables设置看起来是正确的。

添加一个iptables规则

 iptables -I INPUT 1 -p tcp --dport 23435 -j ACCEPT 

它接受来自任何主机的stream量,通过端口23435,并尝试SSH,如果你看到任何数据包或活动,那么这意味着数据包到达您的服务器。

如果看不到数据包,则表示AWS安全组没有规则允许您的端口。

但是如果你看到这个规则的stream量(通过iptables -nvL ),那么你必须运行“netstat -ntlp”并且validationSSH守护进程是否在端口2435上运行,并且在0.0.0.0/0上运行。

希望这些步骤能够解决这个问题。 如果仍然没有,那么告诉我。

如果有人因为改变了ssh的默认端口而绊倒了这个话题,这里有一个解决scheme可以解决我的问题:

  1. 为了绕过企业防火墙,我在/etc/ssh/sshd_conf中将端口更改为80。
  2. 不幸的是,Apache已经安装在那个实例,所以我不能ssh了。
  3. 我从实例中分离出卷。
  4. 附加到另一个实例
  5. 挂载它,改变configuration文件中的端口
  6. 分离它,重新附加在旧的实例
  7. 重新启动:一切正常:D