linux - 允许 ssh 传入/传出并阻止除特定端口外的所有传出

标签 linux bash networking iptables

我正在尝试创建允许传入和传出 ssh 连接的 iptable 规则,然后允许到特定端口的出站连接,最后丢弃任何不匹配的内容。

这些是我想出的规则,SSH 规则有效,但是当我隧道进入盒子时,我似乎无法访问 http(端口 80),即使我已经允许它。谁能找出错误?

#!/bin/bash
#clear iptables
iptables -F
iptables -X

#set default policy to drop
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

#accept everything no matter port on localhost
iptables -A INPUT -i lo -j ACCEPT

#allow established connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#allow input on port 22, (established connections auto accepted)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

#allow traffic going to specific outbound ports
iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 6667 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 6697 -j ACCEPT
#...

#drop anything that doesnt match the rules above
iptables -A INPUT -j DROP
iptables -A OUTPUT -j DROP

感谢您的宝贵时间。

最佳答案

您可能需要添加 DNS 端口,否则您可能无法解析任何主机名。

允许 TCP 和 UDP 端口 53 的 OUTPUT 应该会有所帮助。

关于linux - 允许 ssh 传入/传出并阻止除特定端口外的所有传出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19776681/

相关文章:

json - bash:JSON 文件中转义引号的精确再现

php - shell_exec问题

linux - 如何从 yum 生成可用更新列表并导出为 CSV,包括每个包的当前版本和更新版本?

linux - 如何使用 Linux shell 脚本在文件中找到此模式?

Linux tcp 服务器无法绑定(bind)到 close_wait 端口

windows - 如何找到当前的 DNS 服务器?

security - 使用代理拦截 SSL 流量的 Web 保护产品如何与实现 SSL pinning 的网站配合使用?

linux - 如何从包含 content/u02/app/oracle-1/product/12.2.0/db_1 :N to/u01/app/oracle/product/12. 2.0/db_1:Y 的文件中替换整行

linux - 在 UNIX 中使用 date 命令以特定格式设置日期和时间

bash 脚本 : echo >> returns error "No such file or directory"