如果我将SSH端口从22更改为23453,则无法再使用ssh。
更详细地讲,我在Amazon Web Services上使用Red Hat EC2实例。这是我全新安装的第二个更改(第一个更改是添加非root用户)。
我可以使用Git Bash和本地.ssh / config文件进行ssh操作,我在/ etc / ssh / sshd_config中编辑当前显示的行
#Port 23453
说
Port 23453
然后重新启动sshd
sudo service sshd restart
然后,在我的.ssh / config文件中添加一行“端口23453”
Host foo
Hostname my-ec2-public-DNS
Port 23453
IdentityFile my ssl key
如果我打开另一个Git Bash shell(不关闭我现有的连接)并尝试ssh进入我的实例(使用ssh foo),则会看到以下错误:
ssh: connect to host my-ec2-public-DNS port 23453: Bad file number
附加到此实例的安全组有两个条目,两个都
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
这看起来对我来说很开放。
更新
添加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
(作为根)的输出
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 -L
(ssh有效)和第二iptables -L
(ssh被阻止)之间差异的人。看一下INPUT链中规则的顺序(第一个“目标”下面的6行),它们是从上到下读取的,因此在第二组规则中,在“ ACCEPT tcp”之前命中了“ REJECT all” dpt:23453”。第三组规则的ACCEPT条目位于REJECT条目的上方,因此位于REJECT条目的前面。