Network addresses
What is a netword address ?
IP means Internet Protocol. But first, let's talk about Santa 🎅.
To send a letter asking for all the presents you want, you write to the famous address "Santa Claus, North Pole, Rainbow road.". But how does Santa knows how where to answer? Of course you wrote your own address on the back of the envelope.
That's how works computers and servers : you know where is the video you want to watch, and the server hosting the video knows where to send data.
Every device has its own IP on the internet. There are numbers like 192.0.2.2, not as good looking as Santa's pretty address isn't it?
How devices knows what is the IP of domain name like wikipedia.org ? It there a directory?
Indeed there is something like that : DNS. We'll talk about it later.
There are differences between local (or private) IP ans public IP. Most of the time, to reach the internet, your ISP lend you a router so your network look like this :
+-----------------------+ | | | Local Network (home) | | PRIVATE IP | | | | | | laptop <---+------+ | 192.168.1.20 | | | | | | | | | Phone <---+--+ ++---------------+ | 192.169.1.21 | | | | | | +--+ Router (box) | +----------+ | | | |<---+ INTERNET | | Computer <---+-----+ PUBLIC IP: | +----------+ | 192.168.1.22 | | 192.0.2.2 | | | +--+ | | * * * * * * * * * | | ++---------------+ | * Your server <---+--+ | | * 192.168.1.23 * | | | * * * * * * * * * | | | | | | Game console <--+------+ | 192.168.1.24 | | | +-----------------------+
- Private IP is used between devices inside local network.
- Public IP is used outside the local network, mostly on the Internet.
To host a server, it is useful to have a satic IP delivered by your ISP. Some don't, so you may want to look for a VPN or dynamic DNS or change ISP :)
Reminder
To run your own server, you must know what are its IP, public, local, and its gateway (router). At first, one may want to configure its network using dhcp then configure a static network when more confident.
Let's see a few words to do so.
Public or local/private address
First, understand your server is probably behind a router given by your ISP. The router received a public IP. People "on the internet" can reach this IP.
The router allocate private addresses to devices connected in your local network. Each device has its own private IP : one for your phone, another for a printer, a computer, a console... They all share one public address.
Private addresses can be in three ranges :
- 10.0.0.0/8 : from 10.0.0.0 to 10.255.255.255.
- 172.16.0.0/12 : from 172.16.0.0 to 172.31.255.255
- 192.168.0.0/16 : from 192.168.0.0 to 192.168.255.255
Your private or local IP is the one your computer use, the one it knows.
To find your local IP, run "ifconfig" and filter for "inet". You'll see something like that :
# ifconfig |grep inet inet6 ::1 prefixlen 128 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x3 inet 127.0.0.1 netmask 0xff000000 inet 192.168.1.2 netmask 0xffffff00 broadcast 192.168.1.255 inet6 fe80::feaa:14ff:fe65:5f86%re0 prefixlen 64 scopeid 0x1
inet means IPV4, inet6 means IPv6.
- ::1 and 127.0.0.1 are localhost, that's not what we are looking for.
- addresses starting with fe80: and 192.168 are local IP. The first is IPv6, the latter is IPv4.
Your public address is the one "used" when you browse the web. Websites you're used to read see this IP. If you don't know it, you can check one of these links (amongst many others):
Router
The router is the box your ISP gave you. We also call it a gateway because on a side it is the "public" network, on the other your "private network". The router deals with redirections : if someone wants to reach your website, the router will lead him to your server's private address.
To find your gateway IP, use "route" command:
# route -n show Routing tables Internet: Destination Gateway Flags Refs Use Mtu Prio Iface default 192.168.1.1 UGS 35 4827 - 8 re0 224/4 127.0.0.1 URS 0 13 32768 8 lo0 127/8 127.0.0.1 UGRS 0 0 32768 8 lo0 127.0.0.1 127.0.0.1 UHhl 9 969 32768 1 lo0 ...
The first line is what you're looking for : 192.168.1.1 in this example.
Hostname
Every device in your network has a name to identify it. It is the hostname.
On a computer, you can change this name. To do so on OpenBSD, edit the /etc/myname file and write the complete hostname:
server.athome.tld
DHCP
To configure a network interface, you can use DHCP to autmatically get a local address. Be careful though, the "D" means "Dynamic", so this addres might change in the future depending on your router.
To do so, edit /etc/hostname.if file, and replace ".if" by the name of your inteface (run "ifconfig" to find it) and just write "dhcp" inside.
dhcp
Static configuration
A bit more complicated, static configuration gives you more control.
In /etc/hostname.if (replace "if" with your interface name) :
inet 192.168.1.2 255.255.255.0 192.168.1.255 inet alias 192.168.1.9 255.255.255.0 192.168.1.255 ## and as many as you want # inet (alias) local_ip network_mask broadcast_add up
If you're not sure, at first configure using dhcp and look at the output of "ifconfig".
You must specify the route to your gateway. To do so, you can edit "/etc/mygate" file or add a "!route" command to "/etc/hostname.if". I prefer the latter as it avoid to split configuration in multiple places.
The gateway is you router's IP : 192.168.1.1 here.
inet 192.168.1.2 255.255.255.0 192.168.1.255 inet alias 192.168.1.9 255.255.255.0 192.168.1.255 !route add -inet default 192.168.1.1 up
See also :
IPv6
IPv6 is the new protocol to use facing the lack of ipv4 available. The main advantage is that you don't need a router doing NAT : your machine is directly reachable.
To configure your interface, magic happens in /etc/hostname.if with the "inet6" lines:
inet... inet6 2001:db8:1:1::2 64 # adresse inet6 autoconf # SLAAC, if you want !route add -inet6 default 2001:db8::1 up
You can mix inet and inet6 instructions.