# OpenBSD: a simple laptop network setup (with dhcp)

Note, each of the commands comes with a man page, which I encourage you to read. This post is merely to get you started.

## Manuel Ethernet Device Setup

List available network devices and configuration

```
# ifconfig
``` 

Typical ethernet devices are `em0`, `bge0`, `re0`. Look at the manpage of [em(4)](https://man.openbsd.org/em.4), [bge(4)](https://man.openbsd.org/bge.4) or any other device you might find to learn about what it is.

You can configure these devices with

```
# ifconfig <device> <option>
```

Read [ifconfig(8)](https://man.openbsd.org/ifconfig.8) to learn about available options.

In a typical home network with dhcp, you would type something like this

```
# ifconfig em0 inet autoconf
```

If you want to add IPv6 connectivity do the same with "inet6"

```
# ifconfig em0 inet6 autoconf
```

As a last step, you need to activate the device

```
# ifconfig em0 up
```

## Manuel Wireless Device Setup

Typical wireless devices are `run0`, `iwx0`, `iwn0`, `iwm0`. They have manpages as well look up the device name without number.

You connect a wireless device to your network without

```
# ifconfig iwx0 nwid MyNetworkSSID wpakey MyWifiPassword
```

Once this is done, the configuration can continue like with an ethernet device

```
# ifconfig iwx0 inet autoconf
# ifconfig iwx0 inet6 autoconf # for IPv6
# ifconfig iwx0 up
```

To remove the IPv6 configuration from an interface, you can do

```
# ifconfig iwx0 -inet6
```

Most confgurations can be removed this way.


## Setting the configuration active without restarting

If you changed the `/etc/hostname.*` files, you can reconfigure the network with the netstart shell script

```
# sh /etc/netstart
```

Note that this script adds the confguration to devices as specified. If you remove a `/etc/hostname.*` file, netstart will not know about this device and ignore it.


## Making the configuration permanent

The concept of storing the network device setup is decribed in the manpage [hostname.if(5)](https://man.openbsd.org/hostname.if.5).

A typical configuration for a wireless device would look like this

```
# cat /etc/hostname.iwx0
join MyHomeNetworkSSID wpakey MyHomeWifiPassword
join MyWorkNetworkSSID wpakey MyWorkWifiPassword
inet autoconf
inet6 autoconf
up
```

You may recognise these commands from the manual configuration. The file is named `hostname.<device>` and it basically contains the ifconfig commands that you can also add after `ifconfig <device>`.

Note that I added two `join` commands here instead of `nwid`. The difference is, that `nwid` connects to exactly one wifi network, while `join` builds up a list of multiple networks that are tried in order. If you only have one network to connect to, it makes no difference if you use "nwid" or "join".

You can clear the join list just like you can remove any other configuration from a device.

```
# ifconfig iwx0 -join
```

This `/etc/hostname.<device>` based configuration makes it easy to copy configuration to another device

```
# cp /etc/hostname.iwx0 /etc/hostname.run0
```

## Switching between Wifi and Ethernet without interrupting the connection

You can create a [trunk(4)](https://man.openbsd.org/trunk.4) device, which internally switches between your ethernet and your wireless device.

For this to work, we need three configurations and you should be able to understand most of it by now.

```
# cat /etc/hostname.em0
up

cat /etc/hostname.iwx0
join MyHomeNetworkSSID wpakey MyHomeWifiPassword
join MyWorkNetworkSSID wpakey MyWorkWifiPassword
up

# cat /etc/hostname.trunk0
trunkproto failover
trunkport em0
trunkport iwx0
inet autoconf
inet6 autoconf
up
```

In this setup, we add the configuration necessary for the device to become operational to the device configuration. All the network configuration moves to the new device trunk0, which we have created.

About the special tunk configurations

```
trunkproto failover
trunkport em0
trunkport iwx0
```

The fist line means that we do a failover. This means we use the fist device if we can. If the first device (master device) is down, then we use the second one.

In this concrete example it means, if a network cable is plugged in to `em0`, we use this device. If the network cable is pulled, we simply continue with the wireless device `iwx0`.
