IPTables – 更改传出和传入数据包的端口

是否有可能configurationIPTables的方式,所有传出的数据包到一个特定的IP和端口被改变了一个不同的端口,并为传入的数据包做同样的事情?

我必须经常使用服务器,Hoster决定只能在端口222上使用SSH访问,而不是默认的22。

当ssh,scp或rsyncing时,总是会导致头痛。 你总是要记住添加端口参数。

我想用IPTables来绕开这个问题。

任何帮助不胜感激。

是的,应该可以将iptable规则设置为NAT输出stream量。 你真的只需要创build一个处理输出stream量的规则。 你不应该需要一个规则来做任何事情返回数据包。 netfilter的国家性质将为您处理。

你可能需要使用一个像这样的规则。

# if you want to redirect requests from the local machine iptables -t nat -A OUTPUT--destination remote.host.ip \ -p tcp --dport 22 -j DNAT --to-destination remote.host.ip:222 # if you want to redirect requests on a device inline iptables -t nat -A PREROUTING --destination remote.host.ip \ -p tcp --dport 22 -j DNAT --to-destination remote.host.ip:222 

另一个简单的解决scheme是简单地为服务器设置SSHconfiguration文件,并在你的configuration中指定端口。

 # list of all names, you might commonly use for this host. Host foo foo.example.org foo.example # real hostname Hostname real.example.org Port 222 

我会build议阅读这个链接 。 如果您决定这样做,我也强烈build议您使用以下安全措施来处理SSH和面向公众的IP。

  1. 不要允许rootlogin。
  2. 只允许该端口从某些IP打开。
  3. 有基于密钥的authentication。
  4. 使用诸如denyhosts之类的工具来阻止任何types的蛮力尝试。