AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 606494
Accepted
Andres SK
Andres SK
Asked: 2014-06-20 08:18:51 +0800 CST2014-06-20 08:18:51 +0800 CST 2014-06-20 08:18:51 +0800 CST

如何让 iptables 允许新端口(用于网络服务器套接字处理程序)

  • 772

在本地开发服务器中,我没有任何 iptables 规则(在 Mac 上运行)。然而,生产服务器以某些规则运行 CentOS 6。

我需要添加一个规则,允许客户端连接到1337端口。

这是我当前的 iptables 文件。是否有我必须插入新规则的特定订单位置?

# Generated by iptables-save v1.4.7 on Tue Jun  4 17:42:56 2013
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [3:412]
-A INPUT -p tcp -m tcp --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 11211 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
-A OUTPUT -p tcp -m tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT
COMMIT
# Completed on Tue Jun  4 17:42:56 2013
iptables
  • 3 3 个回答
  • 14400 Views

3 个回答

  • Voted
  1. Vinícius Ferrão
    2014-06-20T08:23:47+08:002014-06-20T08:23:47+08:00

    这只是一个命令。考虑到您正在使用 TCP 请求,只需执行以下操作:

    iptables -I INPUT -p tcp -m tcp --dport 1337 -j ACCEPT
    

    正如@iain 在评论中指出的那样,在命令行上使用它可以保证在 REJECT 规则之前评估该规则。如果您直接编辑文件,只需将其放在 REJECT 行之前:

    -A INPUT -p tcp -m tcp --dport 1337 -j ACCEPT
    
    • 3
  2. John Auld
    2014-06-20T08:31:16+08:002014-06-20T08:31:16+08:00

    假设连接使用 tcp 而不是 udp,将第二行添加到来自任何地方的所有访问。

    -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 1337 -j ACCEPT
    

    或者运行以下命令,再次假设协议为 tcp。这将添加与上面显示的手动编辑相同的行并将规则保存在 /etc/sysconfig/iptables 中。

    lokkit -p 1337:tcp
    

    所以这两个选项是等效的,但 lokkit 会立即应用更改。

    • 1
  3. Best Answer
    user9517
    2014-06-20T08:46:37+08:002014-06-20T08:46:37+08:00

    该规则应添加到 INPUT 链之后

    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    

    规则和之前

    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    

    规则。

    您可以通过直接编辑 /etc/sysconfig/iptables 或使用 -I 参数插入规则来完成此操作。我个人会保存防火墙的状态,然后编辑 /etc/sysconfig/iptables 然后重新启动服务

    service iptables save
    edit the file and add -A INPUT -p tcp -m state --state NEW -m tcp --dport 1337 -j ACCEPT
    service iptables restart
    

    如果您想从命令行完成所有操作,那么您可以使用 --line-number 来决定在哪里插入新规则

    iptables -L INPUT --line-numberss
    Chain INPUT (policy ACCEPT)
    num  target     prot opt source               destination
    1    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
    2    ACCEPT     icmp --  anywhere             anywhere
    3    ACCEPT     all  --  anywhere             anywhere
    4    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
    5    REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
    

    所以现在在位置 2 插入规则

    iptables -I INPUT 2 -p tcp -m state --state NEW -m tcp --dport 1337 -j ACCEPT
    

    如果可行,请不要忘记保存防火墙的状态

    service iptables save
    
    • 1

相关问题

  • OpenVPN 的 Linux IP 转发 - 正确的防火墙设置?

  • iptables 单个规则中的多个源 IP

  • 存储 iptables 规则的规范方法是什么

  • 使用 iptables 和 dhcpd 进行端口转发

  • 根据 Apache 日志数据自动修改 iptables 以阻止行为不良的客户端

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve