Lab 01: INS Linux Server

Jan 4, 2024  │  m. Jan 2, 2024 by Charlotte Curtis

Objectives

Introduction

In this course, you will do all of your assigned programming work using a Linux server named “INS” (ins.mtroyal.ca). A server is a computer that allows multiple users concurrent access, unlike your PC, which only allows one user access at a time.

The following diagram provides a rough idea of how INS is set up and how you are accessing it.

High level depiction of the INS Linux server configuration

What is Linux?

Linux is an operating system. Put simply, an operating system (OS) is the software which manages a computer’s hardware, and allows application programs (such as word processors) and users (like you) to use the machine and all its resources. Another example of an operating system is Microsoft Windows, which is the OS installed on the lab PCs.

On INS, the Linux OS performs many tasks, such as:

Linux is commonly used in industry, and is a favourite of many computing professionals. Although it might look a little strange at first, it is a very powerful operating system which supports a large number of sophisticated features and commands. In this course, though, you will only need to master a few of the most basic Linux commands.

Linux is a multi-tasking, multi-user operating system. This means that it can run more than one program at a time, and that more than one user can be logged in simultaneously.

Step 1: Logging in

The remainder of this lab is a hands-on tutorial session. Don’t hesitate to consult your instructor or an IA if you experience problems at any point during the session.

HINT: As you go through Labs 1 and 2 you should create a “cheat sheet” of commands that you can reference until the commands become second nature.

To log in to our Linux server in a MRU lab is a multi-step process:

  1. Log in to a lab PC using your MRU username and password.
  2. Connect to the MRU VPN (Virtual Private Network).
  3. Connect the INS via SSH (Secure Shell).

I’m going to assume you know how to log in to the lab computers. However, the other steps are a bit unintuitive, and you must do them in the right order.

Connecting to the VPN

The first thing you must do is start OpenVPN by double-clicking on the icon pointed at by the red arrow below. This won’t have a visibly striking result - but if you look in the system tray (shown by the green arrow) - you should see an icon that wasn’t there before.

OpenVPN desktop icon and system tray in Windows 10

Itty bitty new icon

You’ve launched OpenVPN, but you still have not connected to the VPN! Right-click on the icon indicated by the yellow arrow and select “Connect”. You should see the following:

Enter your MRU username and password and click “OK”. This should result in a series of messages appearing in the dialog box, and once the connection process is complete, the box will disappear on its own.

Do not check the “Save password” box on the lab computers. You can do so if you are logging in from a personal computer.

SSH into INS

We will be interacting with INS using a “secure shell”. This has a bunch of fancy security implications, but what it means for this class is that you will log in to INS using a terminal window and a command line interface. No fancy GUIs or mouse clicking once you get in!

To connect to INS, open a terminal window by pressing the Windows key and typing “Terminal”. You can also use Git Bash or kitty if you prefer; all are installed on the lab computers.

From your terminal, type the following command, then press Enter:

ssh <your MRU username>@ins.mtroyal.ca

where <your MRU username> is replaced with your MRU username.

Note: Throughout this course, I will be using angle brackets <> to indicate placeholder text. Do not include the angle brackets when you type the command.

The first time you log in, you may see a message about the authenticity of the host. Type yes and press Enter to continue.

If all goes well, you should be greeted by the message of the day (MOTD), for example:

####################################################################

    Writing the first 90 percent of a computer program takes
    90 percent of the time. The remaining ten percent also
    takes 90 percent of the time.

    - N.J. Rebenking

####################################################################
Last login: <some date> from <some IP address>

Step 2: Exploring Linux

You’re in! Now what?

Much of the following material will be familiar to you from COMP1701, particularly if you practiced using the Bash shell on your local machine.

Following the MOTD, INS will display a “shell prompt”. This means that INS is waiting for you to type a Linux command. The “shell” is the part of the OS which prompts the user, reads a command, runs that command, and then re-prompts the user for the next one.

Note: the look of your shell prompt will be username@ins.mtroyal.ca ~>. Since everyone’s prompt is different, all lab write-ups will denote the shell prompt as a dollar sign ($). The @ins.mtroyal.ca is the name of the computer being used, while ~ is your current location.

What’s my quota?

INS is your personal slice of computer to use for your coursework. Just like an actual personal computer, the disk space is limited, and in addition, the number of files you can store is limited. To check how much space you’ve used and how much is left in your quota, type the following command and press <enter>:

$ quota -s

The -s option tells the quota command to display the results in a human-readable format, with K for kilobytes, M for megabytes, G for gigabytes, etc. Ignore the /users/whoever part - that has to do with how the virtual machine is created and is more or less meaningless.

What do you think the various columns are telling you?

pwd - Where am I?

Modern operating systems like to hide the directory structure from users to make things easier and protect key system files. As computer scientists, it is important to understand how the file system is organized.

As you learned in COMP 1701, a directory can contain a mix of files and other directories (called “subdirectories”). In other words, the “file-system” is organized hierarchically. People often view it as a large, upside-down tree. There is one top-level directory called the “root” directory, denoted by a /. This structure looks something like the following:

root directory
├── sub directory A
│   └── file 1
└── sub directory B
    ├── file 2
    ├── file 3
    └── sub directory C
        └── file 4

Each user has a home directory, created when that user’s account was first added to the system. This space is for the user to store his or her files. As you will see shortly, a user can also create subdirectories beneath the home directory, to better organize his or her data. Initially, the home directory is mostly empty.

Each time you log in, Linux places you in your home directory. With Linux, you are always in a current working directory (cwd). As you will see later, it is possible to change your cwd. However, keep in mind that, at the start of each new session, you always start in your home directory.

To check your cwd, try:

$ pwd

This is the print working directory command. It displays the name (not the contents) of the current working directory.

If your user name was cstud789, you would see something similar to:

$ pwd
/users/cstud789
$

The name of your home directory matches your user name. In this case, pwd would be telling you that you are in a directory named cstud789 (your home directory). In fact, the complete output would be saying that:

The full name displayed by pwd is called an absolute path.

ls - What’s here?

To list the contents of your current working directory, use the ls (“list”) command. Try it now:

$ ls

Can you explain the output displayed by the command?

If you want to get fancy, you can even look at the whole directory tree within your cwd. Try:

$ tree

cp - Copying files

You won’t learn how to create a brand new file until next the next lab, but you can create a copy of an existing file using the cp (“copy”) command. Try the following (type it exactly – the last three characters on the line are a letter, then a space, then a period).

$ cp /library/students/everyone/rules.txt .

Note: following the command name are two “command line arguments”. In order for the computer to understand the command each pair of items must be separated by at least one space – so this command is cp SPACE /library/students/everyone/rules.txt SPACE .. The /library/... is a path and in Linux a path must NOT include any spaces!

The first argument is the source of the copy: an existing file named rules.txt. From what directory is it being copied? The second argument is the destination directory. In this case, it is a period. “Dot” is an alias for “current working directory”.

Now, what do you expect to happen when you try ls again? Run that command to see if you are right.

cat and less - Viewing files

You can view the contents of a file using either cat or less. Try first one, and then the other, running the commands below. Note: the text in this file is too long to fit on the screen.

$ cat rules.txt
$ less rules.txt

The less command displays the contents in stages, using hotkeys to navigate. Here’s a few common ones:

KeyAction
<space>next screen
<enter>next line
b(back to) previous screen
/patternsearch for the first occurrence of pattern
nsearch for the next occurrence of the pattern
Nsearch for the previous occurrence of the pattern
qexit
hsummary of less commands

Note: the less command is a bit more complicated than cat, but it is more powerful, especially for longer documents.

mkdir - Creating directories

You can create a subdirectory using the mkdir (“make directory”) command. Try:

$ mkdir newdir
$ ls

Notice that your home directory now contains a subdirectory named newdir. The new directory is empty.

How could you place a copy of rules.txt inside newdir?

mv - Moving (and renaming) files

You could move a file by copying it to a new location and deleting the original, but it’s easier to do it all in one go. Try the following:

$ mv rules.txt newrules.txt

Note: we just “moved” the file rules.txt to a file named newrules.txt, which you might think of as “renaming”.

You can also move a file to a different directory - Linux is smart enough to figure out what you want to do. Try:

$ mv newrules.txt newdir

You’ve just moved newrules.txt to the newdir directory. Check that it’s there by using ls to list the contents of newdir.

What happens if you try to mv the original /library/students/everyone/rules.txt file to your home directory?

cd - Changing directories

Great, we’ve created newdir, but we’re still working in our home directory. To change directories, use the cd (“change directory”) command.

First, predict the output from each step, then execute the commands in order to check your answers.

  1.  $ pwd
    
  2.  $ ls
    
  3.  $ ls newdir
    
  4.  $ cd newdir
     $ pwd
    
  5.  $ ls
    

To go back up to your home directory, use cd ... .. is an alias for the directory above your current working directory.

rm - Removing files

It’s nice to keep your drive tidy, so now that you’ve read all the rules (right?), let’s remove it with the rm command:

$ rm rules.txt
rm: remove regular file 'rules.txt'?

There is no Recycle Bin or Trash on Linux, so rm is a permanent deletion! The remove regular file? prompt is there to make sure it’s what you want to do. Type y and hit <Enter> to confirm.

Now try removing newdir the same way. What happens?

rmdir - Removing directories

To remove a directory, use the rmdir (“remove directory”) command. Try:

$ rmdir newdir

Note: this only works on empty directories. You can first delete the files inside the directory, or you can use rm -r to recursively delete the directory and all its contents.

More about Linux commands

The general format of a Linux command is:

command-name <options> <argument> ... <argument>

The “options” are a set of “switches” which alter the behaviour of the given command. Options are typically prefixed with a dash and are usually denoted by a single letter. Each command supports a different set of possible options.

For example, try each of the following versions of the ls command:

CommandDescription
lsList the contents of the cwd
ls -lDisplay more detail (“long” listing)
ls -aDisplay all files, including hidden files (prefixed by .)
ls -laBoth “all” and “long”

The arguments are the “objects” on which the command is to operate. Often, these are either files or directories. Some commands require that one or more arguments be specified. Some commands don’t accept arguments, or provide a default if arguments are omitted.

For example, try each of the following:

$ ls --al
$ ls --al /library/students/
$ mkdir

man - RTFM

Do you need to memorize all the possible option and argument combinations for each Linux command? No!

The man (“online manual”) command is used to display information about Linux commands of your choice.

For example, try:

$ man ls

Note that the entry is displayed using the less command, so the keyboard shortcuts in the viewing files section apply here too.

File paths

There are two kinds of path:

  1. absolute, where the location is specified starting at the root (/) of the entire file system; and
  2. relative, where the location is specified relative to the current working directory.

In the previous example, a file named some_file that exists in your home directory would have an absolute path of /users/cstud789/some_file. If you were in your home directory, the relative path would be ./some_file, where . is an alias for the current working directory.

If a path doesn’t start with either / or ./, it is implied to be relative to the cwd. Therefore, some_file is equivalent to ./some_file.

Tab completion

As a convenience, when typing in a command or argument name, you can make use of the shell’s “auto-complete” feature.

For example, assume you wish to change into the course directory. Type just the following and then stop, without hitting <enter>:

$ cd /libr

Next, hit <tab>. The shell will attempt to complete the rest of the word library for you. It will fill in as much of that directory name as possible, until an ambiguity.

So, you should see:

$ cd /library/

Next, fill in the first letter of the next stage as shown below, and hit <tab> (not <enter>):

$ cd /library/s

This should get you to /library/students/. Next, try adding the start of comp1633:

$ cd /library/students/c

The shell should complete the comp, but then it gets stuck because there is more than one subdirectory beginning with comp. Hitting <tab> a second time tells the shell to give you a list of matches. Try this now.

Next, add the 16:

$ cd /library/students/comp16

… and hit <tab> to continue auto-completing. In this way, quickly fill in the rest of the absolute path so that it looks like:

$ cd /library/students/comp1633

Once the command line is complete, hit <enter> to run the command. Congratulations, you’ve reached the comp1633 directory! Enter cd with no arguments to return back to your home directory.

Aside: another time-saving feature of the shell is command history. Try scrolling back through your previous commands by hitting the up arrow key. Down arrow takes you in the opposite direction. This is an easy way of re-entering an earlier command line. Note that you can also edit it, and then run the modified version (e.g. to fix a typo or change an option).

exit - Logging out

When you are finished working on INS, you should log out. To do so, type:

$ exit

This will shut down your INS session and bring you back to the Windows Terminal. You can now close the terminal window, or if you want to explore more, try using some of the commands that you’ve learned in Windows. Not all of them will work, but ls, cd, and pwd should behave the same way.

You should also shut down your VPN connection by right-clicking the OpenVPN icon in the system tray and selecting “Disconnect”, and log out of the lab PC entirely when you leave.

Summary

You’ve been introduced to the VPN system, learned how to log in to INS via SSH, and learned about some fundamental Linux commands, specifically the following:

Important concepts we’ve explored include:

Don’t forget to log out of INS, the VPN and the lab PC at the end of your lab session.

Troubleshooting

INS Rejects my login attempt

Try again. Make sure that you are typing your username and password correctly. If INS rejects you repeatedly, consult with your instructor or an IA.

My shell is frozen

This might mean your VPN has disconnected. Right click the OpenVPN icon in the system tray and make sure the connection is still there, and reconnect if needed. If that doesn’t work, close your terminal and open a new one to reconnect via SSH.



Next: Lab 02: More Linux and Emacs