How to use Linux as a router.

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,525
Reaction score
3,287
Credits
31,524
Let's say you have two TCP/IP subnet in your house, and you want to route network traffic between them.

You probably wouldn't do this on your main gaming/internet browsing computer. So, let's say you have another
computer with Linux on it, and this computer has two ethernet ports ( it doesn't matter if they are USB adapters ).

  1. Assign IP Addresses: Ensure each Ethernet port on your Linux computer has an IP address from each subnet. For example:
    • eth0: 10.0.0.1
    • eth1: 192.168.0.1
  2. Enable IP Forwarding: This allows the Linux kernel to forward packets between interfaces. Edit the /etc/sysctl.conf file and set:
    Code:
     net.ipv4.ip_forward = 1
    Then apply the changes:
    Code:
     sudo sysctl -p
  3. Configure Routing: Add routing rules to ensure packets are directed correctly. You can use iptables for this:
    Code:
     sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE sudo iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT sudo iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
  4. Persist the Configuration: Save the iptables rules so they persist after a reboot. This can be done using iptables-save:
    Code:
     sudo iptables-save > /etc/iptables/rules.v4
  5. Set Up Default Gateways: Ensure devices on each subnet use the Linux computer as their default gateway. For example:
    • Devices on the 10.0.x.x subnet should use 10.0.0.1.
    • Devices on the 192.168.x.x subnet should use 192.168.0.1.
This setup will allow your Linux computer to route traffic between the two subnets.
 


I built a pfSense router out of an old computer for that purpose, but this is MUCH simpler. pfSense works on multiple devices. But I don't want to hijack your thread. :)
 
Might have to set up a VM on the homelab to experiment with this. Intriguing. Would be gnarly to be able to get rid of my TP-Link
 
I'm on my way out for the night. I wanted to leave a comment in here.

The last time I ran my own router on an older desktop I used IPFire. It has been a while, but it's a router and firewall.

I grabbed a link for the curious:


G'night!
 
You could also implement quagga if you want to play with routing protocols. To do anything meaning practice with protocols you will need to stand up multiple systems. Easily done if you are using VM's. If you need a router I would suggest pfsense. You can implement routing protocols and more complex filtering. Of course most of the packages offered on that platform can be stood up on a linux machine.
 
You can do this in VMs but remember, you need two dedicated physical interfaces.
That depends. If you are setting up communication between VM's you can do it with virtual interfaces. If your playing with VLANS you can create a router on a stick using a single interface. Lots of possibilities....
 


Members online


Top