homebrewserver.club/content/fundamentals/port_forwarding.md

142 lines
7.6 KiB
Markdown
Raw Normal View History

2019-06-24 10:54:50 +02:00
Title: Port Forwarding configuration for your home router
2019-06-12 15:04:47 +02:00
Date: 2019-01-14
Tags: port forwarding, router, introduction, lan, wan
2019-08-16 12:20:14 +02:00
url: fundamentals-port-forwarding.html
save_as: fundamentals-port-forwarding.html
Summary: How to make your homebrew server reachable over the internet using your home router.
2019-06-12 15:04:47 +02:00
Author: hbsc & friends
Image: images/port_forwarding.svg
2019-09-17 18:52:57 +02:00
Status: draft
slug: 005
2019-06-12 15:04:47 +02:00
2019-08-16 18:30:00 +02:00
[TOC]
2019-06-12 15:04:47 +02:00
##Introduction
The whole premise of the homebrewserver.club is the simple — yet often overlooked — fact that your home internet subscription theoretically also allows you to host services. The internet is in its essence a bi-directional medium. Anyone with an internet connection can not only look up on-line content but also host it!
2019-06-12 15:04:47 +02:00
2019-06-23 12:32:22 +02:00
In times of 'cloud providers' and 'virtual private servers' it is an easy thing to forget, and internet service providers don't make it easy on you either, but a homebrew server can be as simple as an old laptop connected directly to your home router. However, you do need to change some settings on the router to make that happen!
2019-06-12 15:04:47 +02:00
##Requirements
2019-06-22 23:37:16 +02:00
To begin serving from home you need the following:
2019-06-12 15:04:47 +02:00
- Make sure you have physical access to your home router.
- Get to know the password of the admin user (this is usually provided in the box or written on the label on the underside of the router).
- Have an available power socket next to your router.
- Have a homeserver with a web server and open SSH server running on it.
2019-06-12 15:04:47 +02:00
- An ethernet cable to connect your server to the router.
## Port forwarding theory
![A schematic representation depicting network address translation between a local area network and a wide area network, where ports are being forwarded from the WAN to home server on the LAN]({attach}images/port_forwarding.svg)
2019-06-12 15:04:47 +02:00
By default home routers have configured the firewall so that the devices behind your router are inaccessible to the internet. This is to prevent your private network from being public.
2019-06-12 15:04:47 +02:00
2019-06-23 12:32:22 +02:00
Machines behind your router (called your local area network or `LAN`) can make connections to the wider internet (known as `WAN`), but not the other way around.
2019-06-12 15:04:47 +02:00
However, when hosting a server at home, we do want that server to be reachable from the internet. In order to do that we need to open so-called *network ports*.
2019-06-12 15:04:47 +02:00
Ports are logical 'gates' that are open or closed to connections. These ports have numbers and are [standardized](https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Well-known_ports) for specific protocols or applications.
2019-06-22 23:37:16 +02:00
For example, HTTP traffic from a website would default to port `80`. HTTPS defaults to `443` and SSH defaults to port `22`.
2019-06-12 15:04:47 +02:00
2019-06-23 12:32:22 +02:00
To make our server accessible over the internet we need to open the ports on the router and forward them to our server. This is called port-forwarding.
2019-06-12 15:04:47 +02:00
The exact method (and terminology) of port-forwarding differs from router to router. However, it always follows a similar scheme where you designate inbound traffic on a certain port to be forwarded to the your server's IP-address and port on the local area network.
2019-06-12 15:04:47 +02:00
For this you need to have access to the administrative panel of your router.
### Find your router
To access the administrative panel of your router you need to find it's IP-address. You can do this by connecting to that router via Ethernet or Wi-Fi and then finding out what your own IP-address is.
On Debian based systems this is done like this in the terminal:
2019-06-12 15:04:47 +02:00
`$ ifconfig`
If you get a command not found warning try this:
`$ ip address`
2019-06-22 23:37:16 +02:00
This will return information on your network connection. Look for the line saying `inet`
2019-06-12 15:04:47 +02:00
3: wlp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether ac:ab:00:00:ac:ab brd ff:ff:ff:ff:ff:ff
inet 192.168.1.11/24 brd 192.168.1.255 scope global wlp3s0
valid_lft forever preferred_lft forever
2019-06-22 23:37:16 +02:00
inet6 fe80::eab1:fcff:acab:374e/64 scope link
2019-06-12 15:04:47 +02:00
valid_lft forever preferred_lft forever
In this case the IP-address is `192.168.1.11` as a rule of thumb you can then change the last digit to either `1` or `254` to find the router.
2019-06-12 15:04:47 +02:00
### Log in to your home router and get to know your LAN
2019-06-23 12:32:22 +02:00
Using a web browser navigate to the IP-address you found above to reveal the router's admin panel. It should provide you with a log in field where you can enter the router's admin details to get access to the control panel.
2019-06-12 15:04:47 +02:00
There you will see a lot of possible settings. Look at the options "LAN", "DHCP Leases" or "Network" to get an overview of all the devices.
2019-06-23 12:32:22 +02:00
### Connect your home server
2019-06-12 15:04:47 +02:00
2019-06-23 12:32:22 +02:00
Use an ethernet cable to connect your home server to your router. In case that it has ethernet ports in different colors/markings make sure you take something that says either `LAN` or `INET`.
2019-06-12 15:04:47 +02:00
2019-06-23 12:32:22 +02:00
Have a look at your router's interface again and look for the IP-address that your server was assigned. In this guide I'll assume it was 192.168.1.10.
2019-06-12 15:04:47 +02:00
Next try to find an option called "Static (DHCP) Lease" or "DHCP Binding" or something similar in your LAN view. Then make sure to assign your server a static DHCP lease. This will make sure that the server is always reachable under the same IP-addres.
![]({attach}images/ethernetcable.svg)
2019-06-12 15:04:47 +02:00
## Forward the ports
2019-06-23 12:32:22 +02:00
Once you've set up a static lease to your home server you can start port forwarding. Depending on the make of the router it can be also be called *Port Sharing* or *Traffic Forwarding*. Again, depending on the make of the router, it can be found under the tabs 'security', 'access control' or 'internet'.[^1]
2019-06-23 12:32:22 +02:00
The basic process is to determine which external port to open and to which IP address on the LAN and which port to forward it to.
2019-06-12 15:04:47 +02:00
2019-06-23 12:32:22 +02:00
You might be asked a few things, including the name of the rule, the protocol (TCP, UDP or both), the external port and the internal port. Sometimes you are given the option to open a range of ports.
2019-06-12 15:04:47 +02:00
To open the ports for the web server, we're opening two separate ports, one for plain HTTP and one for secure HTTP.
2019-06-23 12:32:22 +02:00
Open the external port `80` for plain HTTP and redirect it to the local IP-address of the homeserver:
```
Name: "HTTP Homeserver"
Protocol: TCP
Device: 192.168.1.10
External Port: 80
Port to device: 80
```
Open the external port `443` for HTTPS and redirect it to the local IP-address of the homeserver:
```
Name: "HTTPS Homeserver"
Protocol: TCP
Device: 192.168.1.10
External Port: 443
Port to device: 443
```
Lastly we will open a port for `SSH`. The change here is that we open the external port `9999` and map that to `22` internally. Setting SSH on a non-standard port is a low-level way to prevent automated scripts gaining access to your homeserver. TODO add link to basic security
```
Name: "SSH Homeserver"
Protocol: TCP
Device: 192.168.1.10
External Port: 9999
Port to device: 22
```
## Concluding
Now that you have opened the corresponding ports you should be able to type your external IP-address in your browser and should be automatically redirected to the website on your home server.
2019-06-23 12:32:22 +02:00
### How to find out which ports to open?
While a majority of applications will work on `80` and `443` you might need to open different port for different applications. For example in the series describing [self-hosted chat over XMPP]({filename}../instant-messaging/configuring_an_xmpp_server_prosody_0.11.md#set-up-firewall-and-dns) ports `5000`, `5222`, `5269` and `5281` are opened and forwarded.
Most installation guides for software will tell you whether you need to open ports. However it is also possible to see what applications are listening to what port using:
2019-06-23 12:32:22 +02:00
`$ netstat -tulp`.
2019-06-23 12:32:22 +02:00
[^1]: [https://portforward.com/](https://portforward.com/router.htm) has a large list of routers and visual instructions on how to set up port forwarding on them.