CS 103 Lab 1 - Linux and Virtual Machines
Total Page:16
File Type:pdf, Size:1020Kb
CS 103 Lab 1 - Linux and Virtual Machines 1 Introduction In this lab you will login to your Linux VM and write your first C/C++ program, compile it, and then execute it. 2 What you will learn In this lab you will learn the basic commands and navigation of Linux/Unix, its file system, the GNU C/C++ compiler and a few basic applications such as Gedit. Important: The Linux operating system and environment shares a vast majority of commands and methods with Unix. 3 Background Information and Notes 3.1 Software to Install on your PC Start by following the course virtual machine installation instructions found at the link below. http://cs103.usc.edu/tools-and-links/installing-course-vm/ Below is a list of recommended software you should install that will be helpful over multiple courses in CS and EE. These tools allow you to access remote servers, run GUI apps on those servers, and transfer files between your PC and those servers. For Windows: FileZilla FTP – Available from : https://software.usc.edu/index.aspx XWin-32 – Available from : https://software.usc.edu/index.aspx PuTTY – Available from : https://software.usc.edu/index.aspx For Mac: X Server: http://developer.apple.com/opensource/tools/runningx11.html Fetch FTP – Available from : https://software.usc.edu/index.aspx Reference: http://www.usc.edu/its/unix/ Last Revised: 8/29/2014 1 CS 103 Lab 1 - Linux and Virtual Machines 3.2 Getting Started with Unix1 and Accessing your Account Solaris vs. Unix vs. Linux UNIX was developed by AT&T Bell Labs in 1969. Later, a research group at University of California at Berkley added some very important modules to the basic “kernel” of UNIX. This version of Unix is usually referred to as the Berkley Unix. It was here that Unix really took off as the operating system of choice for developers who wanted to add functionality to the kernel so as to fit their needs. Several companies took the Unix kernel and added various features to it to suit their machines and target market. Solaris is the operating system distributed by Sun Microsystems which uses the Unix kernel. The differences between Unix and Linux, however, are more subtle and therefore, arguable. Google the phrase “differences between Unix and Linux” if you really care to find out. From a novice user’s standpoint, there are really no differences. Almost all basic Unix commands work on Linux. 4 Procedure and Reference 4.1 Starting your VM Please reference the following video that will walk you through launching your Ubuntu Linux VM and starting to use it. Below is a small text summary. http://ee.usc.edu/~redekopp/Streaming/fa13_vm_walkthru/fa13_vm_walkthru.html Launch VirtualBox from your host OS. From the VirtualBox Menu choose File..Import. Then navigate to the folder where you extracted your Ubuntu VM Image and enter that folder. You should see a file with the .ova extension. Open this file. Accept the default options. Note: In Virtual Machine terminology we refer to the 'host' OS which is the actual OS your PC is running (OS X or Windows) and the 'guest' OS which is the OS running virtually (i.e. Ubuntu). 1 Acknowledgement: Much of the material covered in this handout is taken from a tutorial produced by Bilal Zafar and user guides prepared by the Information Technology Services at the University of Southern California. Please visit www.usc.edu/its/software for more details on the topics covered. 2 Last Revised: 8/29/2014 CS 103 Lab 1 - Linux and Virtual Machines Back in the Virtual Box Manager, click on the Ubuntu Image and click 'Start'. Say 'Ok' or 'Yes' to any information messages about 'Auto capture …' and you can check the box to not show those messages again. If the VM successfully launches you should arrive at a login screen with the user 'Student' highlighted. Your username is: student Your password is: developer Double-click the Student account and enter your password. You can now use your VM by launching a Terminal and entering commands. Continue through the rest of this lab to learn the basic Linux commands and file structure. 4.2 Unix File System and Navigation Commands It is important to understand how directories are arranged in Unix/Linux. Logically, Unix files are organized in a tree-like structure (just like in Windows). '/' is the root directory (much like C: is for Windows). Underneath root are other directories/folders with a sample structure shown below: / home usr etc student bluetooth pa1 pa2 examples Shortcuts: .. = parent directory (up one level) . = current directory ~ = home directory (/home/student) robot fileio / = root directory Figure 1- Unix/Linux File System Structure Everyone's account (and associated files) is stored under their home directory (usually their USC username on shared servers such as aludra.usc.edu or parallel05.usc.edu; your username is student on Ubuntu VM's) and is located under /home (e.g. /home/student). Your home directory is where you start when you log into the system. Most Unix applications are located under /usr directory. /etc contains system configuration info and scripts that only the administrator of the system is allowed to change. Last Revised: 8/29/2014 3 CS 103 Lab 1 - Linux and Virtual Machines The cd command is used to navigate between directories. Change directory to any other sub-directory in your home directory (mail, for example) by entering the command: cd <directoryname> To return to the home directory from ANY directory, just type “cd” (without any options or arguments) or “cd ~” as the ~ is an alias for your home directory. To go to etc directory (from your home directory), type: cd /etc In Linux we often want to specify the location of a file relative to the current directory. To help, the current directory can always be referenced by “.”, the directory one level above the current is referenced by “..” whereas the home directory is referenced by “~”. For example, suppose you were in the directory home/student/examples/loops and you wanted to go up to the “examples” directory you would type: "cd ..". If you wanted to go directly to the home directory, you could type "cd ../.." to go up two directories but it would be easier to just type: "cd ~" (the shortcut for your home directory). To go from the loops directory to pa1 you could type "cd ../../pa1". List (ls) is perhaps the most commonly used Unix command. It displays (lists) the files within a directory as well as any subdirectories. Let’s give it a try. Type: ls at the prompt. Notice that we did not use any options or arguments and the system returned a simple list of all the files and folders in the current directory. Let us tweak this command with some options. Type: ls –a at the prompt. Is the list of directory contents returned by "ls –a" longer than the one returned by just "ls"? Did you notice some files starting with “.” reported by "ls –a"? This is because the option –a asks forces the system to return ALL files, including system files and hidden files, to be displayed. Now, let’s try another flavor of the same command. Type: ls -la What additional information is produced by "ls –l"? We can specify more than one option for most commands. For example, the option “-t” sorts the list by time. Let us combine this with the “-l” option. Type: ls –lt Notice the dates corresponding to the files and directories reported. 4 Last Revised: 8/29/2014 CS 103 Lab 1 - Linux and Virtual Machines We can also give a folder as argument to the list command to view the contents of that directory rather than the current directory. Look for a directory in your home directory and use the command syntax ls –lt <directory name> to see its contents. We will look at several other Unix commands and utilities later in this lab. Files and directories in Unix/Linux have “permissions”. There are three levels of permissions: user, group, and all (world). User permissions set the access to a file for you and your account. Group permissions apply to other users in your group (usually all students in the course). All or world permissions apply to any user of the system. Within each level you can set the read, write, and executable permissions. To view the permissions, simply type "ls –l". At the beginning of each line will be listing of the user, group, and all permissions in that order. A dash means the permission is not set while the letter ‘r’, ‘w’, or ‘x’ indicates the permission is set. Most data files will only be readable and writable from your user level (no one else should be able to see or modify your files). Executable files (including all directories) should be marked with the user executable permission to allow you to run them (or enter them in the case of directories). In some cases you may need to change the permissions of a file. For example, when setting up your student web page, you want the world (all) to have permission to read your files. In this case you may set the permission to allow all to read and execute the page but not write your files. The chmod program will be used for this and is explained later. Text Editors Most Unix/Linux systems provide several text editors. vi (short for “visual editor”), nano, gedit and emacs are the most common editors. ‘gedit’ is perhaps the most user-friendly of these three editors while ‘emacs’ is the most powerful.