A Practical Guide To OpenBSD
A Practical Introduction to OpenBSD's Built-in Tools
OpenBSD is known for its security, simplicity, and minimalist design philosophy. Unlike many other operating systems that require you to install numerous additional packages to get work done, OpenBSD comes with a surprisingly comprehensive set of tools right out of the box. In this post, I'll explore what's included in a fresh OpenBSD installation and demonstrate some of its built-in capabilities.
Coding in OpenBSD: The Basics
OpenBSD ships with everything you need to write, compile, and debug code without installing any additional software. Let's start with a simple C program using the classic Ed editor.
Ed is considered the classic Unix editor, and while it's not as user-friendly as modern alternatives, it's a part of Unix history. Here's how to use it to write a simple "Hello World" program:
```
$ ed
a
#include <stdio.h>
int main() {
printf("Hello world\n");
return 0;
}
.
w main.c
59
q
```
In the above example:
- `a` enters append mode
- `.` (a single dot on its own line) exits append mode
- `w main.c` writes the buffer to a file named main.c
- `q` quits the editor
Now we can compile our program using the built-in C compiler:
```
$ cc main.c -o main
$ ./main
Hello world
```
OpenBSD uses Clang as its default C compiler. You can verify this by checking the man page for `cc`.
Built-in Debugging with GDB
Impressively, OpenBSD includes GDB out of the box. While it's an older version (from 2004), it's fully functional for basic debugging needs:
```
$ gdb main
(gdb) run
Hello world
(gdb) quit
```
If you need a newer version of GDB, you can install it using the package manager, but having a built-in debugger is quite convenient.
X11 Window System
Another notable inclusion is X11. OpenBSD maintains its own version of X11 that is more minimal than the mainstream implementation. You can start it simply by running:
```
$ startx
```
This launches a basic window manager. Of course, you can install your preferred window manager through the package manager if desired.
Built-in Games
In the Unix tradition, OpenBSD comes with several games pre-installed:
```
$ ls /usr/games
```
For example, you can play the classic snake game:
```
$ snake
```
In snake, you use the arrow keys to navigate and collect dollar signs without hitting the walls or your own tail.
Excellent Documentation
The documentation in OpenBSD is exceptional. You can access it on the website by visiting the FAQ section, but more importantly, comprehensive documentation is available directly in the man pages:
```
$ man release # Information about building OpenBSD
$ man boot # Details about the boot process
$ man scanf # Documentation for C functions
$ man 2 write # Documentation for system calls (category 2)
```
To learn about the different man page categories (called sections):
```
$ man man
```
This shows that:
- Section 1: General commands
- Section 2: System calls
- Section 6: Games
- And so on...
You can specify the section when looking up a command. For example:
```
$ man 6 snake # Documentation for the snake game
```
Package Management
To install additional software, OpenBSD has a built-in package manager called `pkg`:
```
$ pkg_add vim
```
When multiple variants of a package exist, you'll be prompted to choose. For instance, when installing Vim, you might choose between versions with or without X11 support.
Exploring the OpenBSD Kernel
One of the fascinating aspects of OpenBSD is how accessible the kernel is for exploration. Let's look at some advanced features by rebooting and interrupting the boot process:
```
$ reboot
```
When you see the boot prompt, press any key to stop the automatic boot. From here, you can interact with the bootloader:
```
boot> help # Show available commands
boot> echo hello world
boot> hexdump 0x7c00 512
```
The hexdump command is particularly interesting as it allows us to examine the Master Boot Record (MBR), which is loaded by the BIOS into memory address 0x7c00. The MBR is 512 bytes long and ends with the signature bytes 55 AA, which is a standard identifier for bootable media.
We can boot into single-user mode for more advanced kernel interaction:
```
boot> boot -s
```
After booting into single-user mode and selecting the default shell (sh), we can enable the kernel debugger:
```
# sysctl ddb.console=1
```
This allows us to access the kernel debugger by pressing Ctrl+Alt+Escape. From the debugger prompt, we can:
```
ddb> help # Show available commands
ddb> break sys_exit # Set a breakpoint on the exit system call
ddb> c # Continue execution
```
Now, when a process calls the exit system call, the debugger will be triggered. We can test this:
```
# sh
# exit 12345678
```
This will trigger our breakpoint, and we can examine the stack to see the exit code we provided:
```
ddb> x/d $esp # Examine stack in decimal format
```
By pressing Enter repeatedly to view more stack values, we'll eventually find our exit code (12345678).
The Hidden Game in the Kernel Debugger
Perhaps the most unexpected feature in OpenBSD's kernel debugger is a built-in hangman game:
```
ddb> hangman
```
This game uses kernel function names as its word bank, and you're actually forced to play until you win! This is a quirky example of the developer humor embedded in OpenBSD. There's also a user-mode version available in `/usr/games` if you want to practice without locking up your system.
BSD History
As a final note, it's worth mentioning that OpenBSD, along with FreeBSD and NetBSD, all derive from BSD (Berkeley Software Distribution). The history goes back to AT&T's Unix, which was created at Bell Labs in the 1960s. AT&T licensed the Unix source code to various organizations, including the University of California, Berkeley, which developed their own distribution called BSD.
Conclusion
OpenBSD demonstrates a remarkable philosophy of including essential tools right out of the box. From development environments to window systems, games, and powerful debugging tools, a fresh OpenBSD installation provides a surprisingly complete computing environment. Combined with excellent documentation and a focus on security, it's clear why OpenBSD has earned its reputation as a robust and well-designed operating system.
Link:
Comments
Post a Comment