Computing: Computer Administration

Changing the IP address on OpenBSD.

In this tutorial, I'll show how to change the IP address (i.e. setting a new manual IP) on an OpenBSD server. The server, that I actually use, is OpenBSD 7.0. Not really sure, how far the tutorial applies to other OpenBSD releases.

The first step to make is to determine the name of the network adapter. To do this (it is supposed that you are logged in as root), run the command:
    ifconfig

Changing the IP address on OpenBSD: Running the 'ifconfig' command

As you can see on the screenshot, on my OpenBSD, the network adapter is called em0 (this may be different on your system!). The actual IP is 192.168.42.103, and I want to change it to 192.168.42.132.

You can temporarily change the IP using the ifconfig command (in this case it will be reset to the old value when rebooting). But, what we want to do here is to permanently change the IP.

On OpenBSD, the IP address associated with a given network adapter is stored in the file /etc/hostname.<adapter-name>. We can edit this file using the vi editor. In our case:
    vi /etc/hostname.em0

The screenshot shows this file opened in vi. As told by ifconfig, the actually used IP is 192.168.42.103.

Changing the IP address on OpenBSD: Editing the file '/etc/hostname.em0' (old settings)

All that we have to do is to replace "192.168.42.103" by "192.168.42.132" (change the "03" at the end of the IP by "32"). Be sure to save the file, before leaving the editor. The screenshot shows the new content of the file /etc/hostname.em0.

Changing the IP address on OpenBSD: Editing the file '/etc/hostname.em0' (new settings)

Note: If you aren't used to vi, editing a file may be a major issue. You can find some basic usage tips in the section Using vi on OpenBSD.

However, changing the file /etc/hostname.em0 is not enough! In fact, what we have changed is the IP address associated with the network interface em0, but the hostname is still associated with the old IP. Thus, we also must change the file /etc/hosts.

The screenshot shows this file opened in vi, after I have made the changes.

Changing the IP address on OpenBSD: Editing the file '/etc/hosts'

I suppose that we have to restart the network here. The best is to reboot the computer. This way, we can check if the new settings are preserved after rebooting.

To display the hostname and the actual network configuration, use the commands
    hostname
    ifconfig

The screenshot shows the output of the two commands. Note, the new IP address 192.168.42.132 for the interface em0.

Changing the IP address on OpenBSD: Display of the new network settings

We should also verify that the IP address of the host has been correctly changed. We can do that, using the following command ("sv-obsd" being the hostname of my OpenBSD machine):
    ping sv-obsd

The screenshot shows the successful ping of IP address 192.168.42.132.

Changing the IP address on OpenBSD: Successful ping of the host

Using vi on OpenBSD.

If you don't know vi, and after having opened a text document, you try to enter some text, you'll probably be surprised, perhaps also disappointed. Because: nothing happens! The only thing that seems to work is navigating around in the document using the cursor-keys ("arrow" keys), even though there might be operating systems where even this does not work. Is vi real so difficult to work with? Let's say, not really so difficult, just special, different from the text editors that we are used to.

The important fact to understand is that vi works in one of two modes:

To pass into command mode, press the ESC key. Now the input of vi commands is possible. For example, if (after having pressed ESC) you press o, a new line will be added below the line where the cursor is actually positioned; pressing O will add a line above the actual line. As these commands put the editor in entry mode, you can now go to the beginning of this line and enter text.

All this works rather well, but of course, you'll have to know and remember the commands. Thus, for example, to delete a character, you have to place the cursor at this character's position, and then press x. To insert text at the right of the actual cursor position, press a, and then type the text.

The implementation of vi is simplified on some operating systems by allowing the usage of some control keys. Thus, on OpenBSD, to delete a character, you can select this character and press the DEL key. And to insert text to the right of the cursor, you can use the INS key.

Besides these editing commands, vi also includes several file commands. They are entered by pressing ESC, followed by a colon (:). The cursor goes down to the last line on the screen (that isn't part of the document content, but a status and command line) and displays the colon. Now, you can enter a file command. Note, that you have to press the ENTER key after having pressed the command letter(s). For example, w allows to save the document, and q exits the editor (wq may be used for both actions). Pressing q, when the document has been changed and wasn't saved, does not work. If you want to save the document, press w first; if you want to let the document unchanged, press the q! keys.

So, to change the IP address in the two files on the OpenBSD machine, you can delete the last two digits of the IP, using DEL. Then position the cursor at the position of the now last digit of the IP, press INS and enter the 2 digits of the new IP.

If you are interested in details concerning vi, have a look at the article Basic vi Commands in the Solaris documentation.


If you find this text helpful, please, support me and this website by signing my guestbook.