This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| docu:tutos:net:rpi_to_router [2020/02/08 12:49] – admin | docu:tutos:net:rpi_to_router [2020/02/09 22:24] (current) – admin | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| Today we are going to **convert our raspberry pi** into an effective **router** to create a new local subnet where we can safely host our servers and their services.\\ | Today we are going to **convert our raspberry pi** into an effective **router** to create a new local subnet where we can safely host our servers and their services.\\ | ||
| - | The device we are going to use is a **Raspberry Pi 3b model**, but **any device** with *nix, an **ethernet adapter** and 1 free **usb slot** (either 2.0, 3.0, usb c, micro usb) with **usb-to-ethernet support** compiled, should work.\\ | + | The device we are going to use is a **Raspberry Pi 3b model** |
| \\ | \\ | ||
| === THINGS YOU WILL NEED: === | === THINGS YOU WILL NEED: === | ||
| a **small device** (in this case a Pi), a **usb-to-ethernet** cable (to create a new physical address), a **switch** (in case you want to extend your connections), | a **small device** (in this case a Pi), a **usb-to-ethernet** cable (to create a new physical address), a **switch** (in case you want to extend your connections), | ||
| + | |||
| + | === THINGS YOU COULD NEED: === | ||
| + | a **usb hub** just in case your device only has 1 usb slot. | ||
| \\ | \\ | ||
| - | === Let' | + | === Let' |
| - **Connect** the your small device to a **working router** (any router with internet access would do the work), connect it using the **onboard ethernet adapter**. In this case the gateway **interface will be eth0** | - **Connect** the your small device to a **working router** (any router with internet access would do the work), connect it using the **onboard ethernet adapter**. In this case the gateway **interface will be eth0** | ||
| - Connect your **usb-to-ethernet** dongle to any free usb slot on your device. | - Connect your **usb-to-ethernet** dongle to any free usb slot on your device. | ||
| - | - Make sure your system supports usb-to-ethernet. | + | - You should see a **new interface** (`ip addr`), our subnet **will be on eth1**. If you don't see the new interface, make sure your **system supports usb-to-ethernet**) |
| + | - Connect a **small ethernet cable** from the usb-to-ethernet to a **switch** (for providing networking to other devices) | ||
| + | <code txt> | ||
| + | I highly recommend you do this operations on a physical screen | ||
| + | connected to your device, and a usb keyboard. | ||
| + | Some operations over SSH could make you lose access. | ||
| + | </ | ||
| + | I also recommend you to **remove** network-manager, | ||
| + | |||
| + | \\ | ||
| + | Start by installing `isc-dhcp-server` package, which is a **dhcp server** so we can automatically **assign ips to our local sub-network** on eth1. | ||
| + | <code bash> | ||
| + | apt-get install isc-dhcp-server | ||
| + | </ | ||
| + | |||
| + | \\ | ||
| + | Modify **/ | ||
| + | |||
| + | <code bash>vim / | ||
| + | <file bash isc-dhcp-server> | ||
| + | # Defaults for isc-dhcp-server (sourced by / | ||
| + | |||
| + | # Path to dhcpd' | ||
| + | # | ||
| + | # | ||
| + | |||
| + | # Path to dhcpd' | ||
| + | # | ||
| + | # | ||
| + | |||
| + | # Additional options to start dhcpd with. | ||
| + | # | ||
| + | # | ||
| + | |||
| + | # On what interfaces should the DHCP server (dhcpd) serve DHCP requests? | ||
| + | # | ||
| + | INTERFACESv4=" | ||
| + | # | ||
| + | </ | ||
| + | |||
| + | We will be using the **192.168.2.0/ | ||
| + | Modify our dhcpd configuration on **/ | ||
| + | <code bash>vim / | ||
| + | <file bash dhcpd.conf> | ||
| + | # time in seconds, setup to your needs | ||
| + | default-lease-time 600; | ||
| + | max-lease-time 7200; | ||
| + | |||
| + | # 192.168.2.0/ | ||
| + | subnet 192.168.2.0 netmask 255.255.255.0 { | ||
| + | authoritative; | ||
| + | | ||
| + | # the range of ips to give to clients | ||
| + | # set at your own needs | ||
| + | range 192.168.2.2 192.168.2.254; | ||
| + | | ||
| + | # i guess this can be omitted | ||
| + | option subnet-mask 255.255.255.0; | ||
| + | | ||
| + | # this is important i guess | ||
| + | option broadcast-address 192.168.2.255; | ||
| + | | ||
| + | # we are going to be 192.168.2.1, | ||
| + | option routers 192.168.2.1; | ||
| + | | ||
| + | # you will be the dns server too | ||
| + | option domain-name-servers 192.168.2.1; | ||
| + | # otherwise, you can simply not configure a dns server and use any other | ||
| + | #option domain-name-servers 1.1.1.1; | ||
| + | } | ||
| + | |||
| + | # an example of dhcp reservation by mac address | ||
| + | host adevicename { | ||
| + | hardware ethernet 01: | ||
| + | fixed-address 192.168.2.130; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Set yourself (the router) a **static ip address**: | ||
| + | <code bash> | ||
| + | ifconfig eth1 up | ||
| + | ip addr add 192.168.2.1/ | ||
| + | </ | ||
| + | |||
| + | Configure iptables to **route traffic** from **eth1 to eth0 and viceversa**: | ||
| + | <code bash> | ||
| + | # postrouting to our gateway interface eth0 | ||
| + | iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE | ||
| + | |||
| + | # this should in theory, block incoming packets that were not established first | ||
| + | iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED, | ||
| + | iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT | ||
| + | |||
| + | # enable ip forwarding if you haven' | ||
| + | echo 1 > / | ||
| + | </ | ||
| + | |||
| + | To finish the setup, **restart the isc-dhcp-server** | ||
| + | <code bash> | ||
| + | |||
| + | \\ | ||
| + | === Setup a DNS Server (Highly recommended) === | ||
| + | |||
| + | We will be using **dnsmasq** as our DNS Server, because it is very easy to install and configure. | ||
| + | <code bash> | ||
| + | |||
| + | Configure the dns server: | ||
| + | <code bash>vim / | ||
| + | <file bash dnsmasq.conf> | ||
| + | |||
| + | # bind on custom interface or ip | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | |||
| + | # log dns queries and dhcp requests | ||
| + | # | ||
| + | #log-dhcp | ||
| + | |||
| + | # expand /etc/hosts hosts to your dns | ||
| + | expand-hosts | ||
| + | |||
| + | # forward dns request to this ip | ||
| + | # when you can't resolve an address | ||
| + | server=1.1.1.1 | ||
| + | # if omitted, dnsmasq will use resolvconf to return | ||
| + | # the dns configuration inherited by the dhcp server | ||
| + | </ | ||
| + | |||
| + | Restart the **dnsmasq** server | ||
| + | <code bash> | ||
| + | |||
| + | \\ | ||
| + | === Connect your server or laptop to the switch you've configured earlier, and wollah! === | ||
| + | ==== Enjoy! ==== | ||