Assignment 3: Learn to Use vi
In future assignments, we’ll be migrating from Raspberry Pi OS to Alpine Linux, which uses a version of the vi editor supplied by the BusyBox suite of tools. In order to become familiar with this editor, we’re first going to practice using it on Raspberry Pi OS.
Background
The vi editor was originally created by Bill Joy in 1976 as an improvement to the line-oriented ex editor. Joy was using an ADM-3A terminal at the time, which included a keyboard without arrow keys. Hence, vi was designed as a modal editor, meaning that it has several different modes of operation. The initial mode, known as normal mode (or command mode), is for giving commands to the editor. Actually changing the text in the document requires first switching to insert mode or one of its special cases (such as append mode).
On UNIX-like systems, which include Linux, FreeBSD, and macOS, the relevant specifications require that the vi editor be present. As such, it is the one editor that you can assume is installed on any standards-compliant, UNIX-like system. For this reason, it is important to have at least a basic working knowledge of vi even if you prefer to use something else as your regular editor.
There are multiple different re-implementations of vi, some of which have extra features (notably Vim and Neovim). However, the UNIX specifications only require a basic vi implementation that follows a set of conventions used for the editor, so the exact editor will vary by system. On many Linux systems, for example, the base environment includes Vim running in vi compatibility mode.
Alpine Linux makes use of BusyBox, which is a lightweight suite of tools that provide the basic userspace commands for a UNIX-like system. BusyBox is widely used in a variety of “smart” devices, including routers, TVs, and other equipment running embedded Linux. One of the utilities supplied by BusyBox is vi.
BusyBox is an example of a multi-call binary. There is a command named
busybox
that can be used to run any of the programs it implements. For
example, we’ll be running busybox vi
on Raspberry Pi OS to get access to
its vi implementation. However, it is also possible to create a symbolic
link to the busybox
program named vi
. If the busybox program
sees that it is being called with the vi name, then it will automatically run
the vi implementation. This approach with symbolic linking is used in Alpine
Linux.
Now when it comes to actually using vi, there are resources “out there” with plenty of documentation. However, I’ve tried to simplify things with the following video:
If you prefer to learn interactively, you could try at least the first few parts of OpenVim, VimHero, or the game-based VIM Adventures in your browser. These interactive tutorials are technically for Vim, which has more features and capabilities than the basic vi we’ll be using, but they may be helpful anyway.
Procedure
It appears that BusyBox is already available in Raspberry Pi OS. However, to be safe, run the following command:
sudo apt install busybox
You can now run the BusyBox version of vi using:
busybox vi
To exit this test run, type :q and press Enter.
Now, let’s create a file named hosts and practice editing this file. You will be doing a step like this during the Alpine Linux installation. Run:
cd
busybox vi hosts
We run the “cd” command first with no arguments to be sure that you’re not accidentally editing an important file somewhere on Raspberry Pi OS.
Using the above video as a guide, add the following content to this file:
127.0.0.1 localhost localhost.localdomain
::1 localhost localhost.localdomain
Now save the file, but don’t quit the editor or run any other commands. You should see a message at the bottom of the editor that begins with ‘hosts’. Take a photo of your screen with your phone.
This file is the beginning of the /etc/hosts file that we’ll have to edit as part of the Alpine Linux installation. Both lines of this file map the hostname “localhost” and the fully-qualified domain name (FQDN) “localhost.localdomain” to the IP address that refers to this computer using the loopback interface. The first line does the mapping for the IPv4 address (127.0.0.1), while the second line handles IPv6 (::1). We want the term “localhost” mapped to this loopback address both for performance reasons and to avoid sending unroutable traffic out onto our local area network.
Now, when we perform the Alpine Linux installation in the next assignment, we’re also going to want to map the machine’s real hostname on the network to these loopback addresses. This way, any software on our device that tries to connect to our device using our public hostname also goes through the loopback interface instead of going out onto the network. We’re going to use a hostname made from the pattern username-csci311, where username will be replaced with your CCU username (the part of your email address before the @ sign). Our domain name to make our FQDN will be coastal.edu.
Edit your file to look like the following, replacing my username (mmurphy2) with your own username:
127.0.0.1 localhost localhost.localdomain mmurphy2-csci311 mmurphy2-csci311.coastal.edu
::1 localhost localhost.localdomain mmurphy2-csci311 mmurphy2-csci311.coastal.edu
Once again, save the file, but don’t quit. Take a photo of your screen with your phone.
Once you’ve taken the second photo, you may quit the BusyBox vi editor.
Submission Checklist
Upload the two photos you took by following the procedure as the submission for this assignment in Moodle. The first photo should show the base hosts file with only the localhost entries, while the second photo should show the edited file with your custom hostname entries that are based on your CCU username.
ABET Assessment
Successful completion of this assignment satisfies the following performance indicator:
- 1.2. Analyze a complex problem by breaking it down into smaller components.