Operating Systems 2026F: Tutorial 1: Difference between revisions
No edit summary |
|||
| Line 54: | Line 54: | ||
The <tt>ls</tt> command with the <tt>-l</tt> option can be used to show both the permissions of a file as well as the owner and group associated with the file. Permissions are listed first, followed by the owner and the group. | The <tt>ls</tt> command with the <tt>-l</tt> option can be used to show both the permissions of a file as well as the owner and group associated with the file. Permissions are listed first, followed by the owner and the group. | ||
===Links=== | |||
Files in Linux are combinations of two things: a file name (which is an entry in a directory) and file contents (which is represented by an inode). This separation allows for two kinds of links that associate a file name with file contents. | |||
* A '''hard link''' binds a file name to an inode. | |||
* A '''symbolic link''' binds a file name to another file name. | |||
Note that both allow for multiple file names to refer to the same file contents, either through hard links to the same inode or through symbolic links all referring to the same filename. | |||
===Environment & Shell Variables=== | ===Environment & Shell Variables=== | ||
| Line 104: | Line 113: | ||
On Linux, the filesystem is more than a system for storing programs and data. Some files are "special" and others have a zero size but contain arbitrary amounts of data. Three top-level directories are of particular importance: <tt>/dev</tt>, <tt>/proc</tt>, and <tt>/sys</tt>. We will be exploring these directories through the following questions. | On Linux, the filesystem is more than a system for storing programs and data. Some files are "special" and others have a zero size but contain arbitrary amounts of data. Three top-level directories are of particular importance: <tt>/dev</tt>, <tt>/proc</tt>, and <tt>/sys</tt>. We will be exploring these directories through the following questions. | ||
==Tasks/Questions== | ==Tasks/Questions== | ||
Revision as of 14:20, 14 September 2026
THIS TUTORIAL IS STILL IN DEVELOPMENT.
In this tutorial you will be learning the basics of command-line interaction in Linux.
Getting Started
For this tutorial, you need to get access to a Linux or UNIX machine. In the next tutorial we'll start using Openstack, but for this tutorial we will instead use LinuxOnTab, a version of Linux that runs in a browser tab.
To do the following, please go to https://next.linuxontab.com, give it a few moments to load, and then proceed with the following.
Note that by default this linux instance is ephemeral, so don't keep anything you care about in it - it will go away!
Background
LinuxOnTab
LinuxOnTab is an open source project developing a version of Linux that can run inside of a browser tab. The original version included an emulator of an x86-compatible processor which allowed it to run the same Linux programs that would run on most PCs. We, however, are using the new version which is based on WebAssembly.
In this tutorial you'll learn both about Linux and about this WebAssembly-specific version; in Tutorial 2 you'll learn about a more conventional cloud Linux system.
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 more clear representation of the functionality provided by the operating system.
To run a program contained in the current directory in the shell, you need to prefix the name of the command with a ./. This "./" tells the shell that the location of the command you wish to run is the current directory. By default, the shell will not search for executable commands in the current working directory. To run most system commands, the name of the command can be typed without a path specification.
Note that there are many kinds of shells, and people can be very opinionated about which shell is best. We will be normally using bash, but there are many others including ones that have been around forever (sh, csh, tcsh) and somewhat newer, more feature-filled shells (ksh and zsh). There are also shells that were first built for non-UNIX-like systems but now run on Linux (Powershell). Wikipedia has a nice article comparing the features of different command shells.
Shell Basics
Note that bash is the default shell on most Linux systems. Other UNIX-like systems can default to other shells like csh or tcsh; there are many alternatives such as zsh that you may prefer. When you change shells the syntax of the following operations can change; however, conceptually all UNIX-like shells provide the same basic functionality:
- run external programs with command-line arguments
- view and set environment variables
- redirect program input and output using I/O redirection and pipes.
- allow for the creation of scripts that combine external programs with built-in programming functionality.
Processes
Each application running on a system is assigned a unique process identifier. The ps command shows the process identifiers for running processes. Each process running on the system is kept separated from other processes by the operating system. This information will be useful for subsequent questions.
When you enter a command at a shell prompt, most of the time you are creating a new process which runs the program you specified.
Permissions
Your permission to access files in Unix is determined by who you are logged in. A logged in user has a user ID and belongs to one or more groups.
A file is always owned by someone and is always associated with a group. All files on the Unix file system (including directories and other special files) have three different sets of permissions:
- owner permissions
- group permissions
- other permissions
Each of these have read, write, and/or execute permissions along with some other special permissions we'll discuss later.
The ls command with the -l option can be used to show both the permissions of a file as well as the owner and group associated with the file. Permissions are listed first, followed by the owner and the group.
Links
Files in Linux are combinations of two things: a file name (which is an entry in a directory) and file contents (which is represented by an inode). This separation allows for two kinds of links that associate a file name with file contents.
- A hard link binds a file name to an inode.
- A symbolic link binds a file name to another file name.
Note that both allow for multiple file names to refer to the same file contents, either through hard links to the same inode or through symbolic links all referring to the same filename.
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 (eg: 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.
To delete an environment variable, you can unset X.
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.
One of the key reasons people choose alternatives to bash is because of quirks like this!
Controlling Processes
On Linux, you can control processes by sending them signals.
You send signals when you type certain key sequences in most shells: Control-C sends INT (interrupt), Control-Z sends STOP.
You can send a signal to a process using the kill command:
kill -<signal> <process ID>
So to stop process 4542, type
kill -STOP 4542
By default, kill sends the TERM signal.
Special Directories
On Linux, the filesystem is more than a system for storing programs and data. Some files are "special" and others have a zero size but contain arbitrary amounts of data. Three top-level directories are of particular importance: /dev, /proc, and /sys. We will be exploring these directories through the following questions.
Tasks/Questions
- When you have logged in to a shell, how (i.e., using what commands?) do you first find out information about the environment?
- The version of your Linux distribution and the version of your Linux kernel.
- The name (binary path) of the current shell.
- RAM, disk space, and CPU.
- Using the man command, find out what the following commands do: which, pwd, whoami, and env. Try using each of them.
- 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.
- 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.
- 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?
- 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).
- What are the owner, group, and permissions of /etc/passwd and /etc/shadow? What are these files used for?
- What does it mean to have execute permission on a directory?
- 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?
- Compile and run csimpleshell.c. How does its functionality compare to that of bash? List at least 3 differences.