Operating Systems 2026F: Tutorial 1: Difference between revisions

From Soma-notes
No edit summary
 
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''THIS TUTORIAL IS STILL IN DEVELOPMENT.'''
In this tutorial you will be learning the basics of command-line interaction in Linux.
In this tutorial you will be learning the basics of command-line interaction in Linux.


Line 43: Line 41:
* <tt>more</tt> and <tt>less</tt>: output the contents of larger text files, allowing you to navigate through them. (Guess which one came first?)
* <tt>more</tt> and <tt>less</tt>: output the contents of larger text files, allowing you to navigate through them. (Guess which one came first?)
* <tt>vi</tt> and <tt>nano</tt>: Edit text files. (Which one is easier to use?)
* <tt>vi</tt> and <tt>nano</tt>: Edit text files. (Which one is easier to use?)
If you are wondering which program binary a command is running, use <tt>which</tt>.
Many commands support the <tt>--help</tt> and <tt>--version</tt> options. Others will just give you information on the command if you run it with no options or with an option it doesn't understand. There are also manual pages that can be accessed with the <tt>man</tt> command, but that command isn't available in LinuxOnTab by default.


===Processes===
===Processes===
Line 52: Line 54:
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.
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===
===Files, Links, and Filesystems===
 
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 <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.
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.
Line 72: Line 62:


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.
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.
A filesystem is a set of files that all come from the same source. This source could be some sort of persistent storage medium (e.g., an SSD), but it could be anything. When the system first starts it has the root filesystem in the / directory. Other filesystems are "mounted" onto specific directories, so all files are in a single hierarchy under /. Hard links are limited to a single filesystem, while symbolic links can span filesystems.
You can add, remove, and see what filesystems are mounted using the <tt>mount</tt> command. The <tt>df</tt> command can show how much free space is available on each mounted filesystem.


===Environment & Shell Variables===
===Environment & Shell Variables===
Line 124: Line 118:


==Tasks/Questions==
==Tasks/Questions==
For the following questions, try to figure out the answers '''WITHOUT SEARCHING ONLINE'''. The point of these exercises is not the specific answers but the processes you use to find the answer. The information given above, plus some exploration, is enough to answer all of the following questions.


<ol>
<ol>
<li>When you have logged in to a shell, how (i.e., using what commands?) do you first find out information about the environment?</li>
<li>Can you find any files where the size is zero but the file clearly contains data? What filesystem(s) contain such files?<li>
<ol style="list-style-type:lower-alpha">
<li>What is the PATH environment variable for?</li>
<li>The version of your Linux distribution and the version of your Linux kernel.</li>
<li>When you run the <tt>nano</tt> command, something weird happens the first time but not subsequently. What is happening on first execution? Why?
<li>The name (binary path) of the current shell.</li>
<li>When you have logged in to a shell, how (i.e., using what commands?) do you first find out information about the environment?
<li>RAM, disk space, and CPU.</li>
<ol style="list-style-type:lower-alpha">
</ol>
<li>The version of your Linux distribution and the version of your Linux kernel.</li>
<li>Using the man command, find out what the following commands do: <tt>which</tt>, <tt>pwd</tt>, <tt>whoami</tt>, and <tt>env</tt>. Try using each of them.</li>
<li>RAM, disk space, and CPU.</li>
</ol>
</li>
<li>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.</li>
<li>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.</li>
<li>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.</li>
<li>By default <tt>df</tt> doesn't report on all mounted filesystems. Why do you think this is?</li>
<li>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?</li>
<li>What do you think busybox is? Where do you see it on the LinuxOnTab system?</li>
<li>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).</li>
<li>Run the following command. What is the value of $x afterwards? What if you interrupt it in the middle, what is the value of $x?
<li>What are the owner, group, and permissions of /etc/passwd and /etc/shadow? What are these files used for?</li>
<pre>
<li>What does it mean to have execute permission on a directory?</li>
  for x in 1 2 3 4 5 6 7 8 9 10; do echo $x; sleep 1; done
<li>The <tt>ls</tt> command can be used to get a listing of the files in a directory. What options are passed to <tt>ls</tt> to see: the permission bits above; all the files within a directory (including hidden files)? How
</pre>
to make a file hidden?</tt>
</li>
<li>What happens to the value of $x if you run the following?
<pre>
  unset x
</pre>
</li>
<li>Run the following command. What is the value of $x afterwards? What if you interrupt it in the middle, what is the value of $x? How does this differ from the answer to Question 8?
<pre>
  (for x in 1 2 3 4 5 6 7 8 9 10; do echo $x; sleep 1; done)
</pre>
</li>
<li>Run the following command. How does the behaviour of this command differ from previous commands? How can you stop the command from running? And what is the value of $x, at the end of the command and while it is running?
<pre>
  (for x in 1 2 3 4 5 6 7 8 9 10; do echo $x; sleep 1; done) &
</pre>
</li>
<li>Can you make a command that outputs the numbers 1 through 10, delaying for a second in between, using the shell command language? Your program shouldn't be written on just one line!
(Programs written in the shell language are known as shell scripts. Are there any other commands on the system that are actually shell scripts?)</li>
<li>What happens if you add the following command as the last line of your shell script from the previous question? How does that change the value of $x?
<pre>
  sh
</pre>
</li>
<li>What happens if you add the following to the start or end of your shell script?
<pre>
  export x
</pre>
</li>
</ol>
</ol>

Latest revision as of 18:38, 14 September 2026

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.

But at its most basic level, a shell allows you to type a command, and then the shell executes that command. Traditionally most commands are just the name of a program, the command just tells the shell to run the specified program, with the text after the program name passed as arguments to the program, much as you would pass arguments to a function. Command line arguments are separated by spaces from each other.

Here are some commands to get you started:

  • ls: list the files in the current directory
  • cd: change directory, e.g., cd /home. Note that "." refers to the current directory, and ".." refers to the parent directory. So "cd .." goes up one level.
  • cat: output the contents of an input file to the specified file. This is useful for looking at smaller files.
  • more and less: output the contents of larger text files, allowing you to navigate through them. (Guess which one came first?)
  • vi and nano: Edit text files. (Which one is easier to use?)

If you are wondering which program binary a command is running, use which.

Many commands support the --help and --version options. Others will just give you information on the command if you run it with no options or with an option it doesn't understand. There are also manual pages that can be accessed with the man command, but that command isn't available in LinuxOnTab by default.

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.

You can also run the top command to get an interactive view of the processes running on the system. It also gives information about available system resources.

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.

Files, Links, and Filesystems

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.

A filesystem is a set of files that all come from the same source. This source could be some sort of persistent storage medium (e.g., an SSD), but it could be anything. When the system first starts it has the root filesystem in the / directory. Other filesystems are "mounted" onto specific directories, so all files are in a single hierarchy under /. Hard links are limited to a single filesystem, while symbolic links can span filesystems.

You can add, remove, and see what filesystems are mounted using the mount command. The df command can show how much free space is available on each mounted filesystem.

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

For the following questions, try to figure out the answers WITHOUT SEARCHING ONLINE. The point of these exercises is not the specific answers but the processes you use to find the answer. The information given above, plus some exploration, is enough to answer all of the following questions.

  1. Can you find any files where the size is zero but the file clearly contains data? What filesystem(s) contain such files?
  2. What is the PATH environment variable for?
  3. When you run the nano command, something weird happens the first time but not subsequently. What is happening on first execution? Why?
  4. When you have logged in to a shell, how (i.e., using what commands?) do you first find out information about the environment?
    1. The version of your Linux distribution and the version of your Linux kernel.
    2. RAM, disk space, and CPU.
  5. 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.
  6. By default df doesn't report on all mounted filesystems. Why do you think this is?
  7. What do you think busybox is? Where do you see it on the LinuxOnTab system?
  8. Run the following command. What is the value of $x afterwards? What if you interrupt it in the middle, what is the value of $x?
      for x in 1 2 3 4 5 6 7 8 9 10; do echo $x; sleep 1; done
    
  9. What happens to the value of $x if you run the following?
      unset x
    
  10. Run the following command. What is the value of $x afterwards? What if you interrupt it in the middle, what is the value of $x? How does this differ from the answer to Question 8?
      (for x in 1 2 3 4 5 6 7 8 9 10; do echo $x; sleep 1; done)
    
  11. Run the following command. How does the behaviour of this command differ from previous commands? How can you stop the command from running? And what is the value of $x, at the end of the command and while it is running?
      (for x in 1 2 3 4 5 6 7 8 9 10; do echo $x; sleep 1; done) &
    
  12. Can you make a command that outputs the numbers 1 through 10, delaying for a second in between, using the shell command language? Your program shouldn't be written on just one line! (Programs written in the shell language are known as shell scripts. Are there any other commands on the system that are actually shell scripts?)
  13. What happens if you add the following command as the last line of your shell script from the previous question? How does that change the value of $x?
      sh
    
  14. What happens if you add the following to the start or end of your shell script?
      export x