V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
通过以下 Referral 链接购买 DigitalOcean 主机,你将可以帮助 V2EX 持续发展
DigitalOcean - SSD Cloud Servers
MrLonely
V2EX  ›  VPS

VPS 上的 Docker 如何用 iptables 保护网络安全呢?

  •  
  •   MrLonely · Mar 19, 2023 · 1956 views
    This topic created in 1148 days ago, the information mentioned may be changed or developed.

    看到过一些朋友的私人服务,比如 nextcloud ,bitwarden 这种放在 VPS 上。 但是 Docker 开多了以后各个端口很难管理,有些 service 本身并不自带 user/password 认证。 有些 container 还会影射 host 的 volume 进去。如果被他人访问了很麻烦。 想要实现一个像家里局域网一样,先用代理服务回家,然后再访问各个服务的设定。

    就是先用 wiregurad 连上 VPS , 然后再把访问各个端口的流量转发到本机端口。 而从网络上直接来的流量全部 drop 掉,只允许通过 wireguard 的流量访问。 不太清楚这个要怎么样通过 iptables 实现。

    9 replies    2023-03-19 23:18:17 +08:00
    oovveeaarr
        1
    oovveeaarr  
       Mar 19, 2023
    Jakarta
        2
    Jakarta  
       Mar 19, 2023 via Android
    alexwu
        3
    alexwu  
       Mar 19, 2023 via Android
    我的做法是所有服务监听 tailscale 的 IP ,然后
    alexwu
        4
    alexwu  
       Mar 19, 2023 via Android
    @alexwu 然后在 tailscale 上还能设置不同的权限
    MrLonely
        5
    MrLonely  
    OP
       Mar 19, 2023
    @oovveeaarr 看描述我觉得应该是这个。我的需求主要就是 Restrict connections to the Docker host 这一条。
    the following rule restricts external access from all IP addresses except 192.168.1.1:
    iptables -I DOCKER-USER -i ext_if ! -s 192.168.1.1 -j DROP

    但是我把它改成
    iptables -I DOCKER-USER -i 1.2.3.4 ! -s 127.0.0.1 -j DROP
    之后,所有 docker 的网页依然可以继续访问。不需要挂代理。


    @Jakarta 我也跟着这个操作了一遍,也重启了。似乎没有达到效果。
    MrLonely
        6
    MrLonely  
    OP
       Mar 19, 2023
    @Jakarta 然后又突然成功了。多谢了。
    leaflxh
        7
    leaflxh  
       Mar 19, 2023
    启动的时候指定 -p 127.0.0.1:本机端口:容器内服务端口
    kiritoyui
        8
    kiritoyui  
       Mar 19, 2023
    禁止所有端口,开启 80.443.22 ,需要 web 访问的容器全部用 caddy 反代,没有认证的就用 caddy 的 basic auth
    kiritoyui
        9
    kiritoyui  
       Mar 19, 2023
    @kiritoyui 还忘了说容器只暴露到 127.0.0.1
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   908 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 20:25 · PVG 04:25 · LAX 13:25 · JFK 16:25
    ♥ Do have faith in what you're doing.