VM and Linux Primer

UNDER CONSTRUCTION

Virtual Machine Basics

Host vs. Guest:
GUI vs. Headless VM Mode:
Ports:

Shell Basics

“Shell” is a command-line interpreter for Unix and Unix-based systems. (It is what you get when you access a Ubuntu system via ssh—including when you connect to your VM using vagrant ssh. You also get it on Mac via the “Terminal” program.)

When the shell is ready for your commands, it displays a shell prompt. On our VM, it has the form USER@HOSTNAME:current_path$, where current_path tells you where you currently are in the directory (folder) hierarchy. For example, the prompt may be vagrant@precise64:~$; here, ~ stands for your home directory. You type in a command, hit return, and the command will execute. When the command finishes, the shell gives your another prompt. If a command hangs and you want to stop it, [CTRL]-c will usually do the trick.

If you are new to shell, please check out this tutorial for beginners. At least go through the sections “Navigation,” “Looking Around,” and “Manipulating Files.” We highly recommend going through the entire tutorial; it is not that long.

Shell provides access to a wide range of useful commands with text interfaces. We sample several of them below.

Viewing a File

To see what’s in a text file named myfile, simply use the command:
less myfile
It enters a “paging” mode, which lets you view/scroll/search the file inside your terminal. When less is running, you can use the following key strokes/sequences:
[SPACE] or f goes forward one page, and b goes back one page.
[UP] scrolls up one line, and [DOWN] scrolls down one line.
g goes to the beginning of the file, and G goes to the end of the file. More generally, type in a number and then g, and you will go to that line of the file.
/ lets you search for a string, and n goes to the next match in the output.
? lets you search in the reverse direction.
h lets you view the help page.
q quits less and brings you back to the shell.

Editing a File

Both nano and vim come pre-installed on your VM and lets you edit text files within a shell. nano is very easy to learn, and we recommend that if you are new to shell. To edit a (new or existing) text file named myfile, simply use the command:
nano myfile
The rest is pretty self-explanatory. The bottom of the screen always show the list of commands. ^x means [CTRL]-x.

vim is very powerful but can take quite a while to get used to. An advantage over nano is that vim provides nice syntax highlighting by default for many languages, including Python and SQL. To learn vim, we recommend a nice interactive tutorial.

Searching Through Big Files or Lots of Files

If you have big files or many files to search through, use grep. Here are some common usage examples:
grep 'foo' file finds lines in file containing the string foo (anywhere within the line).
grep -n 'foo' file also give you the line numbers.
grep 'foo' *.sql searches for foo in all files in the current directory whose name ends with .sql.
grep -r 'foo' dir/ recursively searches for foo in all files found anywhere under the directory dir/.

Working with Files in VM

Local VirtualBox VM

If your VM runs locally on your own computer, and if you have followed the setup instructions correctly, you will find a directory named shared under your home directory on the VM. This directory automatically mirrors the directory containing the Vagrantfile on your host. For example, to work on Homework #1, you can make a subdirectory hw1 under the directory with Vagrantfile on your host. Then, you may create and edit a file named 2-queries.txt inside this subdirectory (using a text editor on your host). Your VM will see this file automatically as shared/hw1/2-queries.txt. Inside your VM, change your working directory to shared/hw1:
cd ~/shared/hw1/
and do your work there, e.g.:
ra beers < 2-queries.txt > 2-answers.txt
This command will produce a file called 2-answers.txt under shared/hw1/, which you will find automatically on your host as hw1/2-answers.txt under the directory with Vagrantfile. You can submit this file via websubmit using a browser on your host.

Remote Amazon VM

There is no automatically mirroring of files between your VM and host. You can use FileZilla (free from Duke OIT) to transfer files between your VM and your computer. For example, to submit files on the VM for grading, you should first use FileZilla to download them to your computer, and then use a browser on your computer to submit them via websubmit.

To connect to your VM, you will need it public IP address and the file dbcourse.pem. To find your VM’s public IP, see instructions at Getting Started with Amazon AWS, under “Useful Tasks”. To set up FileZilla, following the instructions here, which also points to a video tutorial.

If you hate using editors inside your VM with no GUI (like nano), then with FileZilla (or other similar programs), an alternative workflow is to create/edit code or input files on your local computer (using whatever your favorite editor is), use FileZilla to upload them to your VM, work inside your VM through vagrant ssh, and then use FileZilla to download any output files from your VM back to your computer. You will need to remember to synchronize the files constantly and appropriately.

Yet another possibility with FileZilla is to use its built-in file editing future. You will need to enable it explicitly under settings; make sure you choose to “watch locally edited files and prompt to upload modification.” Once enabled, this feature allows you to select a remote file in the FileZilla interface and view/edit it in-place.