COMP3000 Operating Systems W22: Tutorial 1

From Soma-notes
Jump to navigation Jump to search

In this first tutorial you will be learning the basics of command-line interaction in Linux (Ubuntu) and how to find out information about the OS environment. When source files are needed, you can download them by clicking on the hyperlink.

Tutorials are graded based on participation and effort (so no need to try to have the “correct” answers — what matters is the process), but you should still turn in your work. Submit your answers on Brightspace as a single text file named "<username>-comp3000-t1.txt" (where username is your MyCarletonOne username). The first four lines of this file should be "COMP 3000 Tutorial 1", your name, student number, and the date of submission.

The deadline is usually four days after the tutorial date (see the actual deadline on the submission entry). Note that the submission entry is enforced by the system, so you may fail to get the effort marks even if it is one minute past the deadline.

You should also check in with your assigned TA online (by responding to the poll in the Teams channel tutorials-public). Your TA will be your first point of contact when you have questions or encounter any issues during the tutorial session.

You get 1.5 marks for submitting answers that shows your effort and 0.5 for checking in, making this tutorial worth 2 points total.

Getting Started

For the tutorials, you need to get access to a Linux machine. We suggest you use an SCS Openstack instance (see the instructions here, with the key steps highlighted below). You will need access to a system for the entire semester, ideally the same one.

The concepts covered below are mostly part of standard UNIX/Linux tutorials. Feel free to consult one or more of them. However, remember that you are trying to build a conceptual model of how things work. Thus, don't memorize commands; instead, try to understand how things fit together, learn how to find out the answer with that model, and ask questions when things don't work as expected!

Feel free to discuss this tutorial on Teams in your corresponding tutorial-(TAname)-(section) or the tutorials-public channel.

Openstack

For emphasis: do not take snapshots! (see below)

Create a VM on the SCS openstack cluster by following the instructions. Make sure of the key steps: ①Carleton VPN, ②refresh your SCS account if needed, ③the COMP3000-W22 project, ④the COMP3000-W22-image snapshot image, ⑤adding the ping-ssh-egress security group, and ⑥associating a floating IP address.

The 192.168.X.X IP addresses are private (and cannot be accessed outside of the Openstack cluster), the 134.117.X.X floating IP addresses can be accessed from the Carleton network and will allow you to access the wider Internet.

You need to ssh to your VM instance. Windows 10/11, Ubuntu and MacOS all have SSH clients available from their command lines, just type "ssh student@<IP address>" where the IP address is the floating IP address you assigned to your VM. Other tools supporting SSH (e.g., PuTTY) also work. Once you are prompted to log in, the default user is student, default password is student. Please change your password after you first connect to your machine (using the passwd command).

The image provides an "scs-backup" command that will backup the student user's directory to the SCS linux machines. So if your SCS username is janedoe, you can type:

scs-backup janedoe

and it will create a copy of everything in the student account’s home directory (note: you can customize it) in a directory called "COMP3000VM-backup" in your SCS home directory. You can ssh/sftp to access.scs.carleton.ca in order to access this copy of your VM's files. You should do backups at the end of every session and before you do anything dangerous.

Note that you cannot take snapshots of your VM, so please don't try (it will keep trying and never succeed, and you'll make work for the tech staff who have to cancel what you did).

Making use of Man pages

The man (short for manual) command is a way to access the built-in software documentation system, whose entries come with the system and software packages. For example, if you look at files in an installed software package (e.g., dpkg -L ftp to list files in the ftp package) you can see there are a few files such as /usr/share/man/man1, 5, … They are the man page entries (sections).

The topics go beyond just software/command manuals, but also include conventions and abstract concepts (e.g., man syscalls and man man-pages) that can serve as an info center. There are multiple sections for different purposes, which can be specified as the first argument, e.g., 1 for general commands, 2 for system calls, 3 for library functions, etc. For instance, tee is both a command (man 1 tee) and a system call (man 2 tee). A default will be used if the section is not specified.

For any commands mentioned in the tutorials, you can use man to find the usage. The advantage is the exact match with your current environment, compared to searching on the Internet.

Background

The Shell

The shell or command line provides a text interface for running programs. While not as visually pleasing as a graphical interface, the shell provides a clearer representation of the functionality provided by the operating system.

It works as an interpreter of the commands the user enters, i.e., taking commands and sending them to the OS for execution. Typical shells include bash (the current default we are using), ksh, zsh, csh, etc.

Permissions

Your permission to access a file in Linux/Unix is determined by who you are logged in as. All files on the file system (including directories and other special files) have three different sets of permissions. The first set of permissions denotes the allowed file operations for the owner of the file (rwxrwxrwx). The second set of permissions denotes the allowed file operations for a group of users (rwxrwxrwx). The third set of permissions denotes the allowed file operations for everyone else (rwxrwxrwx). A file is always owned by someone and is always associated with a group. The set of permissions is threefold: read, write, and execute (rwx).

Environment & Shell Variables

Environment variables on both Linux and Windows are variable-value pairs that are shared between processes that define important context-related information (such as the name of the current user, the current language, the timezone) for applications. The key advantage of environment variables is that they are available right when a program starts - they are given to it by the operating system.

In Linux, these environment variables can be printed on the command line in most shells by referring to the variable name prefixed with a $ sign (e.g., to output the value in the HELLO environment variable, one could write echo $HELLO).

Most shells also have internal variables which are private to the shell process. Typically, you can access shell and environment variables using the same mechanisms. By convention, shell variables are lower case or mixed case, while environment variables are all upper case. In bash, by default all variables are first shell variables. To make them environment variables, they must be "export"-ed. Thus:

X="Important Data"

just defines X for the current bash process. However, if you then type:

export X

X will be turned into an environment variable, and so every subsequent program will also get X. You can combine both in one line:

export X="Important Data"

This is the idiom for setting environment variables normally.

One thing to remember with the above is that spaces are used to separate arguments in bash and most other UNIX shells. Thus, it is an error to type:

export X = "Important Data"

as you now are giving export three arguments, not one.

To delete an environment variable, you can unset X.

Downloading files using commands

When working with the command line and you need to download a small number of files from a website (i.e., using the Web/HTTP protocol), you can choose one of the two below (use man to find out more if interested):

curl -o hello.c https://people.scs.carleton.ca/~lianyingzhao/comp3000/w22/tut1/hello.c
wget https://people.scs.carleton.ca/~lianyingzhao/comp3000/w22/tut1/hello.c

In later tutorials, this will be useful as you need source files to do your work.

Compiling programs

To compile a program, use gcc:

gcc -O2 hello.c -o hello

This compiles it with level 2 optimization and without debugging symbols.

To run, you have to specify where it is:

./hello

Remember you can change directories using the cd command. The single dot (".") represents the current directory and ".." refers to the parent directory.

Tasks/Questions

  1. When you have first logged in to a shell, how (i.e., using what commands?) can you find out information about the environment? Consider the following as examples. Try to find out mutiple ways for each (if you can).
    • The version of your Linux distribution and the version of your Linux kernel.
    • RAM, remaining/total disk space, and CPU.
    • The name (binary path) of the current shell.
  2. Using the man command, find out what the following commands do: which, pwd, who, whoami, whereis, and whatis.
  3. Linux commands can be classified as internal (built into the shell) and external (separate program binaries). How can you tell if a specific command (e.g., cd) is internal or external? Figure out where at least three external commands reside on the system.
  4. Making your own commands: the PATH environment variable lists the directories the shell uses to search for external commands. Where can you find documentation on it? How can you add the current directory (whichever directory you are currently in) to PATH? Then, how to make that change permanent? Try to identify multiple ways.
  5. Look at the permissions of the program binaries of the external commands you have just found above. Who owns them? What group are they in?
  6. For those same program binaries, figure out what the permission bits mean by reading the man page of chmod (this is the command you could use to change those permission bits).
  7. What are the owner, group, and permissions of /etc/passwd and /etc/shadow? What are these files used for (and how did you find this out)?
  8. What does it mean to have execute permission on a directory?
  9. The ls command can be used to get a listing of the files in a directory. What options are passed to ls to see: the permission bits above; all the files within a directory (including hidden files)? How to make a file hidden?
  10. Compile and run csimpleshell.c. How does its functionality compare to that of bash? List at least 3 differences.