What changes when you turn a Linux box into a router

(patrickmccanna.net)

228 points | by 0o_MrPatrick_o0 7 days ago

13 comments

  • ValdikSS 4 days ago
    The Linux box instantly turns into a router as soon as you run `sysctl net.ipv4.ip_forward=1`, because the default policy for FORWARD table is ACCEPT.

    You need to explicitly reconfigure the iptables/nftables to prevent that from happening.

    Some software, say LXD/Incus, enable forwarding automatically upon installation/startup, and do not configure firewall to block non-their traffic, making the machine an open router. I've reported that, the developers said that's by design (despite other virtualization/containerization systems block forwarding if they happen to enable the sysctl).

    • 0o_MrPatrick_o0 4 days ago
      Respectfully- I don’t think this statement applies to the scenario I presented.

      “The Linux box instantly turns into a router as soon as you run `sysctl net.ipv4.ip_forward=1`, because the default policy for FORWARD table is ACCEPT.”

      In the setup I presented, we are bridging an Ethernet and a WiFi network. This would be desirable if you wanted to use an upstream dhcp server for your WiFi clients- or if you wanted to avoid double nat’ing.

      In 802.11 infrastructure mode, a station can only send frames with its own MAC address. The AP won’t accept or forward frames from unknown MACs. So you can’t transparently bridge Ethernet devices’ MAC addresses through a WiFi client interface. This is why we need hostapd.

      In every other circumstance- I think your statement holds.

      I tried to do some weird alerting on new MAC addresses and ran into this weirdness. Bridging WiFi and Ethernet gets weird.

      • iam-TJ 3 days ago
        "So you can’t transparently bridge Ethernet devices’ MAC addresses through a WiFi client interface. This is why we need hostapd."

        I think that is incorrect. hostapd handles the authentication side of things, but 4addr tuples are controlled by 'struct wireless_dev.use_4addr', and can be set by 'ip link set type bridge_slave ... proxy_arp_wifi on', `iw dev ... 4addr on', and if using systemd-networkd, with slave interface's

          [Bridge]
          ProxyARPWiFi=yes
        
        (and networkd doesn't need hostapd's bridge= option since networkd handles that aspect.)

        Kernel then uses NL80211_IFTYPE_AP_VLAN and handles the proxy operation.

        • 0o_MrPatrick_o0 3 days ago
          It may be possible that this has changed. Last year I built a device and crashed into the bridging weirdness when I wanted to use upstream dhcp. There was/is some funkiness lurking with bridging wifi to Ethernet- in particular with broadcasts that traverse the bridge.
      • tssva 3 days ago
        Respectfully the scenario you want to present seems to change. The title you submitted this under doesn’t have any mention of switching, firewalls, dhcp server or WiFi access point.

        Then the actual title of the article mentions routing and switching but not a firewall, dhcp server or WiFi access point. Then at the end you seem to change the goal to being a WiFi router but really you have presented more steps than required for that. You have also setup switching, a firewall and a dhcp server which are not required to be a router with WiFi access point.

        • 0o_MrPatrick_o0 3 days ago
          >> spectfully the scenario you want to present seems to change.

          Man that is totally a fair point.

          I feel like I’ve struggled with the tutorials on these configs so many times in my life that I’ve kind of munged several ideas together here. There’s so much subtlety to the iptables/nftables rules that I failed to understand for so long, that I forgot that some folks might not understand that WiFi has specific weirdness. You’re right- I open with routing as a topic, but I’m in a very specific nuance right away.

    • rahimnathwani 4 days ago
      When regular people say 'router', they assume this one box will be all three of these things:

      - router

      - NAT gateway

      - DHCP server

      In a typical scenario, turning IP forwarding on will do nothing unless:

      - DHCP has given the devices on the 'inside' IP addresses and told them the gateway address, and

      - the router is set up to do IP masquerading

    • wofo 3 days ago
      Could you share more details about this? Do you mean that e.g., if I run LXD/Incus on a machine with a public IP address, anyone on the internet could route traffic through it?
      • ValdikSS 2 days ago
        Anyone in "your lan" (L2 of the interface, sometimes L3 like VPN). Your containers/VMs will be fully accessible from other machines in "LAN" (hosting provider infrastructure, or ISP infrastructure), despite no port forwarding/publishing configured.

        Your IP could also be used to run DoS attacks on the internet, although in this case (compared to containers/VM access) the attacker won't be able to receive replies (one-way communication only, like the address spoofing). But if you also happen to configure NAT (MASQUERADE) without additional limits, anyone in "LAN" could use your machine as a router (use your IP address to access websites).

        Such misconfiguration is rarely told about in the how-tos and guides, and it's pretty common to have "additional free IPs" on your VPS/dedicated this way :D

        This is also true in case of manually crafted home routers (such as in this article, and misconfigured advanced routers like Mikrotik, OpenWrt, Cisco, etc): if you happen to unconditionally enable forwarding without firewall, and think that NAT will somehow block the access to your home LAN, that's not true. Your neighbor, connected to the same ISP switch, could just add the routing record to 192.168.0.0/24 via your router's MAC address and access your 192.168.0.0/24 LAN devices without restrictions (unless ISP specifically blocks such access).

        • wofo 20 hours ago
          Thanks! It's always scary when container technology fiddles with your network... I wonder how they can be at peace with such defaults. Another reason to stick with good old containerd units, I guess.
    • 3abiton 4 days ago
      A stupid question, what's the risk?
      • tremon 4 days ago
        The risk is minimal if you control or trust both networks. A network boundary is a natural choke point for access control, so that's where it's usually implemented. For an ipv4 boundary router (as is the topic of the post) you almost certainly need to configure Network Address Translation because your internal network addresses are non-routable on the Internet (at uni my dorm had public IP addresses for each student computer, fun times).

        As for the GP's example, running VM's or containers* on your own machine? I'd say the default ACCEPT policy is fine. However, silently changing such a setting on software installation is a problem because if the machine is multi-homed (i.e. has more than one network interface), you've now created a network route outside of the network admin's control.

        * The default for docker and podman is to use a private network, not a bridge anyway.

        • fc417fc802 4 days ago
          It's can also commonly be a problem if for example you are connected to multiple LANs via wireguard or similar.
      • Dibby053 4 days ago
        Basically you're introducing a hole. For example, if you have some devices in your network (like a dodgy TV box) that are not supposed to reach the internet or other parts of the network, the computer with net.ipv4.ip_forward=1 could be used as a pivot. Depending on the routing tables you probably would also need to enable IP masquerading (NAT) to allow bidirectional communication.
      • Joel_Mckay 4 days ago
        In almost all Linux based router setups: folks end up using 6to4 tunnels, packet marking, and interface routing priority.

        Setting that up with safe/fair bandwidth-sharing requires intermediate IT skill level. Still a great hobby project =3

      • ValdikSS 2 days ago
      • markhahn 3 days ago
        that you'll get it wrong, I suppose.

        after all, most routers/WAP/gateways that you buy today will have linux on the inside, configured similarly.

  • gxs 4 days ago
    My very first exposure to Linux was in 2000, my school was about to throw away an old gateway computer and I took it home and turned it into router

    As a kid with no AI, no google, it was quite a feat and I’m still very proud of it

    Was my introduction into how the internet works and I’ll never forget working with ipchains

    I remember enduring a lot of people in forums calling me a noob, but only after spending collective hours answering my dumb questions

    I credit a big part of my moderate success in tech, to being familiar with stuff at just a tad bit lower of a level than the average bear

    To my friend Sam who I haven’t talked to in 20 years, thanks for the idea

    • vaylian 4 days ago
      Stories like these make me sad when I think about chat control and age verification. Kids in the future may no longer be allowed to talk to random helpful strangers on the internet about computers and other technical topics, because apparently the internet is too dangerous for children.
      • pphysch 3 days ago
        Suppose the age verification checks stopped. Where exactly are these kids supposed to find "random helpful strangers" on the Internet that isn't also a major vector for predation or nonsensical AI spam?

        The open friendly ~safe Internet died long ago.

        • gxs 3 days ago
          Reddit? Discord? Other niche forums like the Subaru owner forums or the various audiophile forums? HN?

          There are so many places where no one even thinks to ask your age, they just help/troll/etc

          If age verification were in place, you may be forbidden from posting to those places which is mind boggling stupid to me

          I just don’t see the argument for age verification, it’s just yet another government overreach. It’s a well known thing to use children for any privacy reasons encroaching bills and they are always called “the save the children from online predators and other evil doers” so that you can be easily vilified if you oppose them

    • s0rce 3 days ago
      Around the same time I set up an old pentium 80 that booted linux off a floppy to be a router. It ran for a few years later until Linksys wifi routers got cheaper.
  • Havoc 4 days ago
    Used to run a virtualized firewall setup. And then one day discovered that somewhere along the lines I had made a change (or an update changed something) that meant proxmox admin interface was being served publicly. That's despite confirming during initial setup that it isn't.

    So now I do not do any funky stuff with firewalls anymore. Separate appliance with opnsense bare metal.

    • tarruda 4 days ago
      I currently do something similar.

      My router is a 16GB n150 mini PC with dual NICs. The actual router OS is within openwrt VM managed by Incus (VM/Container hypervisor) that has both NICs passed through.

      One of the NICs is connected to another OpenWrt wifi access point, and the other is connected to the ISP modem.

      The n150 also has a wifi card that I setup as an additional AP I can connect to if something goes wrong with the virtualization setup.

      Been running this for at least 6 months and has been working pretty well.

      • Joel_Mckay 4 days ago
        Both port specific firewall rules, and web-server IP permissions are important.

        For example, bandwidth rate-limiting may be inhibited for admin SSH or package updates, and LAN IPv4 private ranges for your host address pool are set.

        Finally, your internal DHCP should statically bind your admin computer MAC to a fixed LAN host IP to further reduce issues.

        Personally, I always build my NAS from scratch, as I have lost count of the number of problems web-GUI have caused over the years. =3

    • ziml77 4 days ago
      Dedicated appliances are the way to go for the most important parts of your setup. I've always had my router as an appliance because I don't like the idea of my network failing due to something going wrong with the server that runs a bunch of other things that are less important. I also have Home Assistant running on a dedicated machine because that's also important.

      Btw you do also need to be careful with opnsense. I was years behind on updates for mine because every time I updated I assumed that it would bring me up to date with the latest version. But opnsense has to install the upgrades in order. After you reboot you need to check again for updates and repeat until there's no more to install.

    • drnick1 4 days ago
      I don't bother with virtualization, and use the machine at the edge of my network as router, email server, Web server, DNS server, and countless or other things such as hostapd.

      An x86 mini PC can run all this without breaking a sweat; using separate appliances seems very wasteful. That being said, I configure everything in DIY mode, and don't rely on GUIs or other similar things that increase the attack surface considerably.

      • ziml77 4 days ago
        I used to try to combine everything, but now I don't. Separate appliances isolates issues to a subset of services. If everything is on a single PC and that one dies or even just needs a reboot, everything goes down.
      • GandalfHN 3 days ago
        [flagged]
    • gerdesj 4 days ago
      Fair enough and I think you have done the right thing - opnsense is pretty decent - and the clear delineation between collision domains helps avoid showing too much ankle to the internet 8)

      I think your initial setup was perfectly valid. Then you diagnosed a fault and fixed it with aplomb, in a way that you could verify. The key point is: "in a way you could verify" and you failed safe. Well played.

      Proxmox itself has a useful firewall implementation too, although it takes a bit of getting used to because you can set it at the cluster, host and VM levels. I personally love it because it is easier to manage than individual host based firewalls, which I also do, but I'm a masochist! For smaller systems I generally use the cluster level to keep all the rules in one place.

    • UltraSane 3 days ago
      Using VMs as routers and firewalls gets very confusing very fast.
  • freetime2 4 days ago
    Are there any preconfigured images/installers available for a major Linux distro to turn them a router with safe and sensible defaults?

    I know there is OpenWrt, but my experience is that is more geared toward running on embedded wifi hardware than an x86 machine. The x86 install comes with a tiny root partition that's actually pretty difficult to resize, for example, and upgrades are quite brittle compared to standard Linux distros.

    And there's also pfSense and OPNsense, but these run on FreeBSD which seems to lag behind Linux for hardware support. There's no support for the Aquantia AQC113 NIC, for example (although it looks like this may finally have been added in the last month or so).

    Something like an Ubuntu Appliance [1] would be quite nice.

    [1] https://ubuntu.com/appliance

    • Joel_Mckay 4 days ago
      Modified Ubuntu LTS server image will work, and a minimal Debian kernel will have far less bloat. Note pfSense/FreeBSD is fairly robust, and a mature project.

      Keep in mind most network appliances have dedicated hardware hand-off adapters, and so the CPU isn't involved in routing once the connection is setup. It is why people can use a $30 SoC, and still be able to saturate several 10Gb/100Gb ports. =3

    • erinnh 3 days ago
      The best is likely Vyos. It acts quite similarly to routers from the likes of Arista/Cisco/Juniper.

      https://vyos.io/

    • ask2sk 4 days ago
      Ipfire and Untangle seems suitable for your use case
      • Joel_Mckay 4 days ago
        Those look pretty cool too. =3
    • assimpleaspossi 3 days ago
      FreeBSD probably supports the hardware you have. If not, just buy the hardware that supports FreeBSD.
  • chatmasta 4 days ago
    This is an excellent post and great reference material. I’ve done this a few times before and the information was scattered all over the place. I appreciate the clear and concise writing here. I even added it to my HN favorites - a rare accolade!

    One thing I’d add, is that the best explanation I’ve ever seen for this, is the famous diagram [0] on Wikipedia of the netfilter API — I remember when I saw that, everything clicked into place. I’m not sure how up to date it is now, but it’s really good.

    [0] https://commons.wikimedia.org/wiki/File:Netfilter-packet-flo...

  • sorz 4 days ago
    I feels wrong to not mention IPv6 in 2026.

    - net.ipv6.conf.all.forwarding=1

    - nftables is default to `ip` family which only applies to IPv4. Setting it to `inet` will allow rules to apply to both IPv4 & 6; or `ip6` for IPv6 only. You can skip NAT rules, usually.

    - dnsmasq: in addition to DNS and DHCP, turns on router advertisement with SLAAC. Some devices can get IPv6 address from stateful DHCPv6 server, others (e.g. Android) only work with SLAAC.

  • binkHN 3 days ago
    While I run Linux on my production workstation, I use OpenBSD as my router and firewall at home. I find the configuration of OpenBSD for this a lot more simple and everything that's needed, even for IPv6, is in the base install.
  • rkagerer 3 days ago
    This is a great article.

    It explains steps I used to fumble through stabbing in the dark following piecemeal examples trying to bring up quick and dirty networking on an oddball Linux device (like a BPI-R4 or router VM).

  • evanjrowley 3 days ago

      Set Linux as router in one command. Support Internet sharing, redsocks, Wifi hotspot, IPv6. Can also be used for routing VM/containers
    
    https://github.com/garywill/linux-router
  • jcalvinowens 3 days ago
    People saying "the FOWARD chain defaults to ACCEPT" are missing the deeper point: with the kconfig most distros use, the filtering code doesn't even exist at all until you load the kernel modules!

    At the lowest level, it is impossible to have a default DROP for forwarding, because nftables is an optional piece of the kernel that often isn't loaded.

    • ValdikSS 2 days ago
      Well it's kind of possible to block it with routing tables+rules only, but you're right. This is also painful if iptables/nftables binaries are not installed.
  • dfir-lab 3 days ago
    [flagged]
  • ValveFan6969 4 days ago
    [dead]
  • eqvinox 4 days ago
    [flagged]
    • tremon 4 days ago
      Aside from the fact that "a CPE" is grammatically incorrect, you are also semantically wrong. A router is any device connected to multiple networks that can forward packets between them; and consumer-premises equipment includes everything that's directly connected or consumes a service from a telecom provider. Landline phones, set-top boxes and satellite decoders are also examples of CPE.

      It's like me stating "you're not a man, you're a human!" and then expecting you to be in awe of my profound wisdom.

    • oxygen_crisis 4 days ago
      Technically it's an IPv4 router once you enable net.ipv4.ip_forward in step 1, the rest is enabling a whole lot of supplementary services and operations not intrinsic to the definition of a router.
    • TacticalCoder 4 days ago
      I didn't see in TFA --although I may have missed it-- where it said it was replacing the ISP's router/CPE. Anything routing traffic is a router.

      At home I've got both a CPE given by my ISP and my own router that routes and bridges traffic between two LANs of mine (192. and 10.).

      Moreover the lack of IPv6 inside our own LANs is, for many of us, a feature. It doesn't mean we don't have an IPv6 address: it just means we have the choice and did choose to have our own LANs on IPv4 only. And, no, I don't care that it makes some programmers at some megacorp' lives more difficult to "reach" inside my networks.

      I'm the boss at my home and my router is IPv4 only.

      And I've got that in addition to my ISP's CPE.

      • 0o_MrPatrick_o0 4 days ago
        I wouldn’t call it a cpe unless it translates the connection from the clec/co into IP.

        IMO, it’s a plain old router/switch/bridge.

    • marssaxman 4 days ago
      Thank you for informing me that a novel definition of the term "router" has come along since the last time I turned a Linux box into a router. The world changes in strange ways sometimes!
    • gerdesj 4 days ago
      No need for "a" in "a CPE" (Customer Premise Equipment - its singular and plural inclusive already) - you wasted a character there 8)

      IPv6 support is not required for a router. You'll note they also fail to offer IPX/SPX or ATM and many more.

    • trelane 4 days ago
      What is "CPE" in this context? It's probably not "Common Platform Enumeration" (my top results for "cpe linux") or "Customer-Premises Equipment." ("cpe networking")
      • landdate 4 days ago
        Googling "cpe vs router" shows websites comparing "Customer Premises Equipment" with routers. I don't think it fits though.
      • 0o_MrPatrick_o0 4 days ago
        Customer premises equipment.

        People who use this term are in telco.

      • bombcar 4 days ago
        I think it's your second one (used to be called the "modem" in the modem→router→ pc setups of yore).
        • trelane 4 days ago
          > CPE generally refers to devices such as telephones, routers, network switches, residential gateways (RG), set-top boxes, fixed mobile convergence products, home networking adapters and Internet access gateways that enable consumers to access providers' communication services and distribute them in a residence or enterprise with a local area network (LAN).

          https://en.wikipedia.org/wiki/Customer-premises_equipment

          Given that the Wikipedia definition of CPE includes routers, I don't see how calling it CPE precludes it being a router, as the poster claimed:

          > That's not a router, that's a CPE, and one without IPv6 support

          • bombcar 4 days ago
            I think a CPE could (be/include) a router, but usually it refers to the demarc between the provider's network and the customer's (no matter who owns/manages it).

            For a Linux box to be a true CPE you'd likely need somewhat of a specialized card, one that can communicate directly to the next device up the line (e.g, take commercial fiber or cable in, ISDN modem, etc).

            If it just shoots out ethernet into some other box next to it, it's likely not a CPE.

            • Hikikomori 4 days ago
              Plenty of isps that provide internet over regular ethernet. But it's a ye olde telecom provider term that referred to the phone, that you also didnt own yourself. Doesn't always apply cleanly these days.
              • bombcar 4 days ago
                Usually it's "something else" that turns into RJ45 (as ethernet has a maximum length) - now if you're in a datacenter you likely can get raw RJ45 Internet).
                • Hikikomori 4 days ago
                  Fiber uses ethernet as well. Though ethernet fiber to the building and rj45 inside is common too.
                  • BenjiWiebe 3 days ago
                    Yes, but then you'd need a "somewhat specialized card" to turn a Linux computer into a "CPE" - a fiber transceiver.
                    • Hikikomori 3 days ago
                      And what if you just have rj45? Is a ethernet card also special? Transceivers aren't particularly special or hard to get either. Point is that's not what makes it a CPE, ownership does.

                      It's an old term used by telecom to refer to the phone they owned that's in the customers home. It has been used after by internet providers if they put a device in your home. If it's your own device it's not a CPE as seen from the isp perspective.

              • BenjiWiebe 4 days ago
                Really? They have buried Cat5/6 cable speaking Ethernet coming onto your property?

                I've never heard of that before. How does that work? Your ISP would always have to have some infrastructure within 100 meters of your router then.

                • dekhn 4 days ago
                  In my case I have AT&T Fiber that IIUC carries ethernet frames encoded optically.
                  • BenjiWiebe 3 days ago
                    But then you'd need the "somewhat specialized card" to turn a Linux box into a "CPE" - a fiber transceiver.
                • Hikikomori 4 days ago
                  No. But fiber to the building and rj45 inside is common here. Or fiber to each apartment. All regular ethernet.
          • landdate 4 days ago
            I mean the wikipedia literally states:

            > CPE generally refers to devices such as telephones, routers, network switches, residential gateways (RG), set-top boxes, fixed mobile convergence products, home networking adapters and Internet access gateways that enable consumers to access providers' communication services

            From my understanding any type of device that is used to extend or facilitate provider services is a CPE. So a router just acting as an extender would still be a cpe, as would a modem, as would anything that is on the customer side and facilitates provider services. Only situation a router wouldn't be a cpe is if it was just for a local lan network.

    • nullsanity 4 days ago
      [dead]