Due: After you are done, submit the science.txt, linux_info.txt, and hello.c files to CMS by Sunday 1/27 at 11:59 PM


Part 0: Virtual Machine and SSH Overview

This part can be used to reference terms you don't know, but is not necessary for completing the lab. You should skim it.

What is Linux?

Linux is an operating system that is free to the public to download, since it was created as an open-source program. The system is based on an older OS version called UNIX. To navigate through a Linux or UNIX system, you need to type in instructions to the computer using what is called a command-line prompt (nowadays, we often use graphical user interfaces (GUIs) to click on folders instead of typing commands to find those folders). You can even download software using the command-line prompt. Later, you will learn some of the most common Linux commands.

What is Ubuntu?

It is an African word meaning "the belief in a universal bond of sharing that connects all humanity" (https://en.wikipedia.org/wiki/Ubuntu_(philosophy)).

More seriously, it is a version of Linux. Some of the most popular distributions include names like Ubuntu, Mint, Fedora, openSUSE, and Debian, though there are dozens more. Ubuntu is known for being kind to beginners with its software center and media management while Debian is less user friendly and geared towards more technically oriented users. We will be using Ubuntu for our OS. In fact, we have created an Ubuntu version specifically for this class!

What is a Virtual Machine (VM)?

A virtual machine is an application that can imitate hardware such that you can run more than one operating system on your own computer. For example, I can run Windows on a Mac computer using a virtual machine. The VM can run while my usual operating system running without requiring a reboot. It works like any other application, sitting in a window you can minimize when you want to go on Facebook and maximize when you want to work on CS 3410 again. In case you hear this terminology, the Host OS refers to your computer, while the Guest OS is the operating system you are running on the VM.

(So why do people use dual booting? Well, one reason is that VMs tend to have higher overhead, such as being slow with 3D graphics, but booting one OS at a time means that the OS can use the computer’s full speed.)

Instructions to download our VM are in Part 1a.

What is a kernel?

The kernel is a program that has complete control over the whole operating system. It is the first program loaded when the OS starts up, placed in a safe location in memory to prevent being overwritten. It handles all the interfacing between the hardware and software. For instance, it may take input requests from a keyboard translate them into instructions the CPU understands.

What is a shell?

It is a program that provides the user interface for the kernel. This is where you type in the commands we mentioned earlier. Just as there are various Linux versions, there are also several shell versions, such as sh, bash, csh, tcsh, and ksh. Again, these versions came about due to improvements or extra features implemented by others (e.g. bash built upon sh by adding command completion and command history). On a Mac, the shell is called the Terminal. On Windows, the analogous form is the Command Prompt.

What is SSH?

SSH (Secure SHell) is a method of remotely connecting to and running commands on another computer (“remote” meaning something far away, so we are connecting to a computer far away). It can be accessed using the ssh command in a shell (like the Mac Terminal). It lets you access the resources of another computer that you may not have on your own computer. However, you cannot use SSH without an Internet connection. Cornell makes Ubuntu computers available to computer science students, which you can access remotely with SSH.

How to SSH is explained in Part 1b.

Windows does not come with an SSH client, which means you will need to install one on your computer if you are a Windows user. The we suggest Git (or Cygwin). Git lets you use the basic Linux commands that you'll need for this class. Cygwin puts a full UNIX environment (or close to it) but it has more overhead and you do not need it.

Part 1: VM and SSH

For this class, you will have the option of either using the VirtualBox VM to run Ubuntu or using SSH to remotely connect to a Cornell Ubuntu machine. Choose either Part 1a or Part 1b to ensure you have access to an Ubuntu machine. SSH is preferred by most of the TAs. VM is okay if SSH does not work or you don't have internet connection.

Why are going through all the trouble of setting this up?

For one, it's often very difficult to get all of the course software on the variety of different computers and operating systems that everybody brings to the class. But more importantly, we want to ensure that the everyone is working in the same environment so that we can guarantee that if it works for you, it will also work for us (and our autograder). We therefore require that you do all of your projects work on the Ubuntu environment that we set up for you, because some of the projects we will be doing later on may work differently in different environments. We want to avoid anyone submitting anything that works on their computer but then doesn't work on our machines because the environment is different.

Part 1a: Virtual Machine Setup

For the setup:

  1. Go to the course resources page
  2. Follow the instructions under the Computing Environment section.
    Note: On the VirtualBox website, download the VirtualBox platform packages associated with your current Operating System.
  3. Once you have started up the OS, you can move on to part 2.
    Note: It may take some time to start up.

* You can learn more about VirtualBox at https://www.virtualbox.org/manual/ch01.html

Part 1b: SSH

SSH (Secure shell) is a method of remotely connecting to and running commands on another computer. Cornell makes Ubuntu machines available to computer science students and we have created one specifically for this class that is equivalent to the VM that is made available.

Important Note for SSH

This is a system that will work anytime you’re connected to the internet... on Cornell Campus. Cornell's SSH network requires being connected to the same network as the machine you’re connecting to. Never fear though, the solution to this is called a VPN - a Virtual Private Network. This is a service that routes your traffic through a server (in this case on Cornell campus) so that you can connect to the VPN even when not on Cornell Wifi.

You can find the VPN installation and connection instructions here. After installation, follow the connection tutorial at the same page to set up the connection.

Important: If you use Windows, your computer does not come with an SSH client. Skip down to "For Windows users only", then come back here to complete the process"

For everyone:

  1. Make sure you are connected to the VPN or Cornell's Wifi
  2. Enter the terminal (Windows users remember to use Git Bash or the Cygwin terminal) and type ssh netid@ugclinux.cs.cornell.edu, where you replace netid with your netid
  3. Type yes to accept the new SSH target
  4. Now type the password associated with your netid
  5. You’re in! If you don’t already know how the Unix command line works, you’ll learn it later - that will be how you interact with the files on this machine.
  6. To exit the shell, type exit or hit Ctrl-D

For Windows users only:

Choose one of the two options:

Git
If you do not already have Git installed on your computer, install it from here. You will be able to ssh by opening the "git bash" program and using the ssh tool there. If you would like to ssh through the windows command line, follow in the instructions below.
  1. If you do not already have Git installed, install it now.
  2. You’ll now need to find where Git installed its ssh client. It will either be in “C:\Program Files\Git\usr\bin” or “C:\Program Files\Git\bin”. Find which of those directories contains a file called “ssh.exe”, then copy that filepath
  3. Open Control Panel -> System and Security -> System, then click “Change Settings”. Go to “Advanced”, then click “Environment Variables”
  4. Under “User Variables” click “Path”, then click “Edit”
  5. Click “New”, then paste in the filepath from above (You may see just a string to edit instead - in this case simply type a semicolon separator then paste the path)
  6. Done! Now when you open the windows command line, you will be able to ssh.
Cygwin
  1. Visit https://www.cygwin.com/ and download the setup script appropriate for your computer (almost certainly the 64 bit one)
  2. Run the setup script. Select the option to download cygwin, then it will prompt you for a download site - I recommend simply selecting the top one.
  3. When it asks you to select packages, scroll down to “net” and click on “default” until it changes to “install”. This will ensure that the ssh client is installed with Cygwin
  4. Hit next and allow Cygwin to complete the installation process. Hit OK to install dependencies when it asks. The installation process may take some time (~5 to 10 minutes)
  5. Now when you need to ssh, you’ll open the program “cygwin” and use the terminal it provides

Part 2: Vim and Git Overview

Now that we are using a Linux environment (via SSH or VM), let’s learn some commands to make your Linux machine do your bidding! But first, we have a few more bits of knowledge for you. Note that there are some necessary tasks to complete along with the reading!

Which type of shell are we using?

We are using the bash shell, which is a typical default for Linux systems. Type in the following command into your shell to check!

echo $0

Can we use text editors within the shell?

Yep. You have several at your disposal - vim, emacs, and nano. Each have their own set of commands for various behaviors like saving text, exiting, etc. If you have never used any of these editors before, we suggest getting started with vim, one of the most popular text editors.

Complete the following:

  1. To start vim, type vim into the shell. You are now using vim in Command mode which means you will not be able to type normally.
  2. To enter Insert mode press the i key. You can now type as you would expect.
  3. Type “I love math, science, and computer science!” into the vim window.
  4. To save your file, you must reenter Command mode. Hit the ESC key to do so. Then type :w science.txt which will appear at the bottom of the terminal. This writes the file to the given file name.
  5. To search for the word “science”, in Command mode type /science, hit Enter, and watch the cursor jump to the start of the first occurrence of “science”. To again find the same string, hit n. Hit N one more time to return to the first occurrence.
  6. To exit out of a vim session, in Command mode type :wq to write and quit, or :q! to discard all of your unsaved edits and quit.
  7. To open your file again, you can type vim science.txt. Because you opened a file with a name, to save you can just use the command :w to save your work, without specifying the filename.
  8. You should now know enough to be able to experiment with the other commands on your own. Vim is extremely powerful once you learn more commands. Check out this tutorial if you have free time.
  9. Keep reading to learn more UNIX!

How do I get files from my computer to the remote machine and back?

For this class, the best option is to use your git repository. Complete the following to get the repository on your remote machine:

  1. Use a web browser and login to your git account at http://github.coecis.cornell.edu/
  2. Navigate to your repository, if you don't have one you should create one.
  3. Click on Clone or download on the right. Under "Clone with HTTPS" you will see a link. You will need that link (you may not be able to use copy and paste on the VM, remember when we suggested SSH? Yeah...).
  4. Return back to your shell. Type in git clone your_repository_link, where your_repository_link is what you found in step 3.

You should now be able to navigate through the downloaded directory. Use the git commands to add, commit, push, pull, etc. to update your files. See the tutorials on how to use Git in the Git, etc. section of the course resources page.

For this lab (and most of the class) you will only need a few commands:

  1. On your Ubuntu machine (SSH or VM) type git clone REPO_LINK
  2. This will create a new folder with your repo's name, go into that directory using cd REPO_NAME
  3. Get any remote changes that you don't have yet: git pull
  4. Edit all your files in this directory. Add your changes in this directory when you're done using git add .
  5. Say you're sure you want to add these files and say what the changes are: git commit -m "MESSAGE_ABOUT_CHANGES"
  6. Push your changes to your Github repo: git push
  7. Your files should now be on the Github website. Get these files onto your host computer by typing (NOT on your VM or SSH) git clone REPO_LINK if you don't have the repo cloned yet.
  8. Once the repo is cloned: cd REPO_NAME and then git pull
  9. And that's all! You just repeat these steps (except git clone REPO_LINK if you already have the folder) every time you want to transfer code.

What are man pages?

These are manual pages, not pages for men. If you cannot remember what parameters you need for a command, you can type in man command_name to obtain a description. For example, try man mv. Use the up and down arrows to scroll, and use q to leave the screen. You can even do man man to get information about man itself.

Part 3: UNIX Commands

Read this whole part carefully before starting!

Complete Tutorials 1, 2 (see note below), and 3 from the UNIX Tutorial for Beginners.

For the second command under Section 2.1 Copying Files, instead of copying the file given on the website, copy your previously made science.txt file from the unixstuff parent directory using

cp ../science.txt . (note the dot at the end - there are four in this line!)

Save information about your Linux distribution to a file called linux_info.txt. This is to verify that you genuinely have the correct environment set up. Try running the command lsb_release -a and looking at the output. The lsb_release command prints out information about your Linux distribution (Ubuntu). LSB stands for Linux Standard Base, which is a project to help standardize the various distributions of Linux. It should look something like this:

No LSB modules are available.
Distributor ID: LinuxMint
Description:    Linux Mint 17.3 Rosa
Release:    17.3
Codename:   rosa

The above is an example of lsb_release -a output created by running the command on a different version of Linux. Yours should look very similar. Do not copy ours.

Yes, you will see the message No LSB modules are available. From what we can figure, Ubuntu is not totally LSB compliant since it is "a considerable amount of work for little measurable benefit" according to those working on the Debian project.

Put the output of this command in a file by redirecting the output of the command, which you should have learned in Tutorial 3 of the UNIX commands. The redirection you learned redirects only stdout (standard output), which means the No LSB modules are available. line will still be output to the terminal and not to the file. This is because that line is output to stderr (standard error), and so isn't redirected with the rest of the text. This is fine - we're only looking for your file to contain the lines outputted to stdout, which means the contents of your file should look similar to the block above but missing the first line.

Some useful tips:

Part 4: Intro to C Overview

Welcome to C! It is a programming language that was first introduced almost half a century ago but is still one of the most commonly used programming languages due to its speed, efficiency, and ability to closely interface with hardware.

Let's get to coding in C. On your Ubuntu machine (VM or SSH), open up a text editor (vim) and create a new file called hello.c. You can create and name the file in one step by entering the command vim hello.c. Type in the following C program (remember Insert mode):

#include <stdio.h>

int main() {
    printf("Hello world! I am [netid].\n");
    return 0;
}

But replace [netid] with your NetID (don't print square brackets). When you are done typing, save the file and exit the editor (remember Command mode and :wq?). Now you are ready to compile and run the program you just created! In your terminal, navigate to where you saved hello.c. Now run the command:

gcc -o sayhello hello.c

The C compiler (GCC) has compiled your source code hello.c into an executable named sayhello. The -o option allows us to name the resulting executable file anything we want. Without the flag, the default name given to the executable is a.out.

If this gives you any errors, make sure you are in the right working directory (use ls to confirm that hello.c is in the same directory). If you are in the right location, then you did not enter the program correctly — go back to your editor and fix the program. Otherwise, you have just compiled a C program. You can run your program by running the command:

./sayhello

And your program should run! It should print Hello world! I am [netid]. and do nothing else. For example, if your NetID is "abc123", then compilation and execution would look like something like this:

~$ gcc -o sayhello hello.c
~$ ./sayhello
Hello world! I am abc123.

What does ./sayhello mean? The command ./sayhello means, run the executable sayhello which is located in the current directory (remember . means current directory and .. mean parent directory).

That's it! Now look back at how to transfer files using git and submit science.txt, linux_info.txt, and hello.c to CMSX