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
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1337350
Accepted
Bob Reed
Bob Reed
Asked: 2021-05-11 07:13:04 +0800 CST2021-05-11 07:13:04 +0800 CST 2021-05-11 07:13:04 +0800 CST

使用 iptables 阻止除特定端口之外的所有互联网流量

  • 772

我有一个在 Ubuntu Server 20.04 LTS 上运行的 Emby 服务器,并且想配置 iptables 以阻止来自 Internet 的所有传入连接(端口 8920 除外),但允许来自本地网络节点的正常传入连接(ssh 等)。这可能吗?(我这样做是因为我的 Zyxel 路由器 EMG3425-Q10A 没有正确进行端口转发。仍在努力解决这个问题。)

networking
  • 1 1 个回答
  • 631 Views

1 个回答

  • Voted
  1. Best Answer
    Doug Smythies
    2021-05-11T12:42:14+08:002021-05-11T12:42:14+08:00

    这是一个 iptables 脚本。LAN(局域网)和接口定义适用于我的测试计算机,必须根据用户要求进行更改:

    doug@s19:~/iptables/misc$ cat ask1337350
    #!/bin/sh
    FWVER=0.01
    #
    # ask1337350 Smythies 2021.05.10 Ver:0.01
    #       See here:
    #       https://askubuntu.com/questions/1337350/using-iptables-to-block-all-internet-originating-traffic-except-for-a-specific-p
    #       run as sudo on s19.
    #
    #       Note: These rules likely need to be merged with
    #       any existing iptables rules set.
    
    echo "Loading ask1337350 rule set version $FWVER..\n"
    
    # The location of the iptables program
    #
    IPTABLES=/sbin/iptables
    
    #Setting the EXTERNAL and INTERNAL interfaces and addresses for the network
    #
    # Set for Smythies s19 computer (for testing). Edit for ask1337350's computer.
    # EXTIF="enp3s0" no,no,no use the bridge br0, or everything breaks, big time.
    EXTIF="br0"
    EXTIP="192.168.111.136"
    NETWORK="192.168.111.0/24"
    UNIVERSE="0.0.0.0/0"
    
    # Clearing any previous configuration
    # Be careful here. I can do this on s18, but do not know
    # about vxsa4's computer.
    #
    echo "  Clearing any existing rules and setting default policies.."
    $IPTABLES -P INPUT DROP
    $IPTABLES -F INPUT
    $IPTABLES -P OUTPUT ACCEPT
    $IPTABLES -F OUTPUT
    $IPTABLES -P FORWARD ACCEPT
    $IPTABLES -F FORWARD
    $IPTABLES -t nat -F
    
    # Delete user defined chains
    $IPTABLES -X
    # Reset all IPTABLES counters
    $IPTABLES -Z
    # Smythies: While my references do not have it, I think this is needed.
    $IPTABLES -t nat -Z
    
    # loopback interfaces are valid.
    #
    $IPTABLES -A INPUT -i lo -s $UNIVERSE -d $UNIVERSE -j ACCEPT
    
    # Allow any related traffic coming back to the server in.
    #
    $IPTABLES -A INPUT -i $EXTIF -s $UNIVERSE -d $EXTIP -m state --state ESTABLISHED,RELATED -j ACCEPT
    
    # Allow in any LAN traffic
    #
    $IPTABLES -A INPUT -s $NETWORK -i $EXTIF -j ACCEPT
    
    # Also allow in port 8920 traffic from anywhere
    # The question does not specify a protocol. Do both.
    #
    $IPTABLES -A INPUT --protocol udp --destination-port 8920 -i $EXTIF -j ACCEPT
    $IPTABLES -A INPUT --protocol tcp --destination-port 8920 -i $EXTIF -j ACCEPT
    
    # Do not allow in anything else
    # Could also just fall through to default policy here, but sometimes a logging rule is also desired.
    #
    $IPTABLES -A INPUT -i $EXTIF -j DROP
    
    # At this point carry on. This might need to be merged into whatever existing iptables rule set.
    #
    echo ask1337350 rule set version $FWVER done.
    

    给这个,在很短的时间后:

    doug@s19:~/iptables/misc$ sudo iptables -xvnL
    Chain INPUT (policy DROP 0 packets, 0 bytes)
        pkts      bytes target     prot opt in     out     source               destination
           8      616 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
         133     7916 ACCEPT     all  --  br0    *       0.0.0.0/0            192.168.111.136      state RELATED,ESTABLISHED
          51     3355 ACCEPT     all  --  br0    *       192.168.111.0/24     0.0.0.0/0
           0        0 ACCEPT     udp  --  br0    *       0.0.0.0/0            0.0.0.0/0            udp dpt:8920
           0        0 ACCEPT     tcp  --  br0    *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8920
          19     5880 DROP       all  --  br0    *       0.0.0.0/0            0.0.0.0/0
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
        pkts      bytes target     prot opt in     out     source               destination
    
    Chain OUTPUT (policy ACCEPT 90 packets, 12122 bytes)
        pkts      bytes target     prot opt in     out     source               destination
    
    • 1

相关问题

  • 如何设置 VLAN 转发?

  • 如何将主机 Ubuntu 上的 VPN (tun0) 网络适配器映射到 VirtualBox 来宾 Windows?

  • 如何限制下载/上传带宽?

  • 如何通过 Windows 网络共享文件?

  • 面板小程序以文本形式显示当前网络流量?

Sidebar

Stats

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

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve