I want to use my Ubuntu server to be a router for two private networks on my network.
I also want this server to serve DHCP requests for the private networks.
How do I set up an Ubuntu 16.04 server to do this?
I want to use my Ubuntu server to be a router for two private networks on my network.
I also want this server to serve DHCP requests for the private networks.
How do I set up an Ubuntu 16.04 server to do this?
You'll need an Ubuntu server with 3 network interfaces, of course, one for the Internet-connected interface, and two private network interfaces.
After that, just follow this guide to setup this server as a router:
(1) Edit
/etc/sysctl.conf
. Find and uncomment this line:Once you've done this, execute the command
sudo sysctl -p
to reload the system kernel settings. This allows the Ubuntu box to now serve traffic across subnets and VLANs via IPv4.(2) Edit your router box's
/etc/network/interfaces
to set up the static IP address on the interfaces which serve the private networks. In this example, I know the interfaces areens37
andens38
, whileens33
is the primary Internet connected interface on my box. I'm leavingens33
alone, but addingens37
andens38
configuration stanzas:Adjust the network addresses and interface names accordingly for your setup.
**Note that if you are using VMware Workstation or similar, and you have
vmnet#
devices selected in the VM for this, make sure the host system either does NOT have a host-attached device for thisvmnet
, or that if it does, you use a different address than.1
in the last octet on your router box.(3) Install the DHCP server software. We'll configure this in later steps.
This will permit the router box to serve DHCP requests for your private subnets.
(4) First, copy the DHCP Server config that was installed automatically to a backup file.
(5) Now, we are making that original file blank so we can apply our config.
(6) Now let's use our config:
Adjust this config accordingly to your needs, and make sure to update the 'routers' option based on whatever IP address you set above in your network configuration settings.
Once you're done adjusting the file as needed, save the file.
(7) We need to tell the system what interfaces to care about. Edit
/etc/default/isc-dhcp-server
, and specify your private network interfaces (in my case,ens37
and38
) in theINTERFACES=""
line, so that it'd look like this:Save the file.
(8) And now, the firewall rules. We need to tell the system to permit the thing to work as a router, and set up proper controls and rulesets for this to be the case. I'm assuming you haven't configured the firewall here, because I'm describing a from-scratch setup.
If you have already set up
ufw
on this machine, runsudo ufw disable
, and then uninstallufw
withsudo apt-get remove ufw
. UFW does not work for what we need, we need the advanced power ofiptables
directly. For most routers we should not be using UFW at all.Make sure we know the interface name for your Internet-connected network interface. On my example test system, it was
ens33
, but it may be different on your system. Make sure we also know the network interfaces for our private networks we're going to be the router for; we need them in here too. Setup youriptables
as follows with the following commands. Pay attention to my comments as well:(9) Install the
iptables-persistent
package which will allow us to actually remember ouriptables
rules and load them at reboot.When it asks to save the existing rules, select "Yes" for both IPv4 and IPv6.
(10) Test it! Set up another system on one of the private networks you've configured, and make sure once it's set up that it can communicate to the Internet and has a configured DHCP address within the private subnets set up above!