Lab1 Introduction to UNIX
During this week we will play-through the exercises refreshing your UNIX knowledge. Starting from the next week we start building the real serve-side service infrastructure. Therefore we find it critical for you to feel fluent with command line interface (CLI) of UNIX. The outline of this lab is as follows:
- CLI Intro
- Trying out basics of bash shell
- Solving bash shell exercises
- Playing bash games
This week's monitoring checks for tasks done in the following paragraph: Bash introduction
CLI Intro (do not run these)
Command line interface interaction can be reduced to: typing in the desired command and reading or interpreting the corresponding output.
- Examples (assuming
bash
shell syntax, andGNU/Linux
standard commands):- creating a directory:
mkdir dir1
- listing the files in a directory:
ls dir1
- renaming the directory:
mv dir1 dir2
- copying a file
cp file1.txt dir2/file1.txt
- navigating to the dir1
cd dir1
- navigating one level up in the directory tree
cd ..
- discovering the current directory (or current write directory (CWD))
pwd
- removing a file
rm file1.txt
- removing all text files (by *.txt extension)
rm *.txt
- recursively removing all files in the current directory
rm -rf *
- creating a directory:
- There many more commands we face during this lab (here we showed just few file management ones)
As you can see, using the command line is not always trivial, nor is it safe. The last command actually tries to delete all the files in current directory, without really asking you whether it should. This is why one should have a proper grasp of commands and techniques used to manipulate a UNIX/Linux system.
Sandbox
To prevent accidents, we will first go into a "sandbox" machine.
First of all, make sure that you are connected to the University of Tartu network. From remotely, this is achieved by using the University of Tartu VPN service. The guide for that can be found at https://wiki.ut.ee/pages/viewpage.action?pageId=17105590.
Moving from one host to another on Linux systems is usually done by utilizing a protocol called SSH (Secure Shell). This can be used to login into another machine.
The syntax usually follows like this: ssh <username>@<hostname> Example: ssh mouse@lab1.cloud.ut.ee
Let's log into a machine called lab1.cloud.ut.ee with our University of Tartu users.
For Linux/Mac users this can be done by opening your terminal application, and inserting the following command to the command line ssh <username>@lab1.cloud.ut.ee
. You will be asked whether you trust the host, and for your University of Tartu password.
You can tell whether you were successful with logging in by not getting an error, and the prompt changing. Also, the output of commands whoami
, hostname
and pwd
should be different from your own machine.
Windows users have two options:
- Windows Subsystem for Linux - WSL(more info at https://www.microsoft.com/en-us/p/ubuntu/9nblggh4msv6?activetab=pivot:overviewtab). Using WSL grants you the ability to open a linux terminal in windows to use the ssh command as mentioned above. WSL is harder to set up, but easier and more convenient to use in the future, as it emulates the whole command line.
- Software called PuTTY, that allows to create an SSH connection to the server. Putty is very easy to run, but in the future might cause you to need to jump through extra hoops to do some stuff, like add an SSH key. A guide on using PuTTY can be found on the University HPC page https://hpc.ut.ee/en/using-ssh/ .
You will need to use a SSH connection in every lab to complete your work on your virtual machines so it's best to get familiar with those programs/commands.
Scoring
During the course you will be monitored and scored by a scoring server: https://scoring.sa.cs.ut.ee . This site monitors the progress of all the labs that you complete. When you are completing labs, you can see that you did everything right when monitoring goes green.
The credentials are:
User: student
Password: SysAdm2021%
Once you access the scoring site, you can search for Services
on the left side panel. When you click on it, a new view appears, where there's a host called "Lab1". From there, search a line with your username in it (it appears for the first time a bit after you log into lab1.cloud.ut.ee).
When this line goes green, you have completed this week's task.
Helpful materials
At first, writing up commands that you won't remember is a very good idea. We will never prevent you from being able to look at your notes, even during exam!
This means that if you want, you can make a cheat sheet, or just write up any commands that you think you will not be able to remember. In fact, automation is encouraged during the exam - during the following weeks we will also be teaching how to use a tool called Ansible, which will take care of config management and more. A well written Ansible repository could in theory allow you to pass the exam in just a few minutes, so keeping it up to date might prove invaluable.
This year we also will provide you with helpful visualisations to further your knowledge, as there will be no physical lectures to explain the topics.
In case you need more details about particular shell usage you may refer to:
- Shell Commands Explainer
- Bash Guide for Beginners
- This one slightly larger but also detailed document about the
bash
shell usage - Recommended as a reference! When solving exercises you may quickly find the chapter to read through as the resource is having well organized index.
- This one slightly larger but also detailed document about the
Bash introduction
Here is this week's list of tasks. You are to run these tasks inside the lab1.cloud.ut.ee machine. We expect you to be able to complete this with the help of Helpful Materials, google and each other's help (yes, that is allowed), before starting the next week.
These are commands that you will constantly use, and we hope you have seen these in some other classes like Operating Systems or Infosec. If not, you need to do a bit of catching up.
Now when inside the machine, try out the following:
- What is current working directory (Command: pwd)?
- Find out your home directory.
- Go to your home directory.
- Make a new directory to your home directory called "newdir" .
- List the contents of the working directory (can you see the newly created directory in there?).
- Go to your new directory.
- Make sure you are in your new directory.
- Create a new file "mytest" to the working directory.
- Append the text "Welcome to Linux!" into the file you have just created.
- Print the content of the file to standard output.
- Open the file with your favourite text editor (nano, vim, ed, emacs, joe ... which one you like better?).
- Write "Glad to be here" into your file.
- Save and close your file.
- Print the content of the edited file again.
- Make a copy of your file called "mytest2".
- Make sure that the copy was successful. How can you make sure ? (file size, modification date, checksum ...)
- Print the contents of both files simultaneously.
- Delete the original file.
- Make sure that the original was deleted.
- Clear the terminal window contents.
- Rename the copy to "theNewFile".
- How big is your file?
- Copy the file to your home directory.
- Check the contents of /tmp without being in there.
- Go into /tmp.
- Make a directory that has the same name as your <username>, then a directory inside that called second, and then another directory called third inside the previous one.
- Copy your file from your home folders' newdir into the deepest directory with one command, without moving inside them.
- Go to /etc.
- Try to read a file called "passwd".
- Try to add a new line to the file. (Why can't you?)
- Try to read a file called "shadow". (Why can't you?)
- What are the two previous files for?
Intermediate tasks
This list of tasks is only recommended. You do not have to complete these, but it will make it easier in the future.
Some more difficult tasks:
- 1. How many cpu-s?
- 2. How much % of cpu time is used by kernel processes?
- 3. How many active processes currently running?
- 4. What is your ip address? (cannot use sudo, nor use root)?
- 5. Use pipe with cut/grep/whatever and make command that outputs all of your ip addresses?
- 6. Find all txt files in your system?
- 7. How much free room you have on your file system?
- 8. How many symbolic links do you have on your system?
- 9. Make a 10 MB random file (hint: use /dev/urandom and command dd)?
- 10. Delete the contents of the file you created using /dev/null and output redirection.
If you want to learn by playing:
Game called "bashcrawl", for beginner Linux users: Idea of the game: Crawl through a dungeon of bash. How to get started: (On our lab.cs.ut.ee machine, in your home directory) git clone https://gitlab.com/slackermedia/bashcrawl.git cd bashcrawl/entrance cat scroll (continue using the instructions) Game called "Command Line Heroes", for those who want to solidify their knowledge of command line. Idea of the game: Try to write as many Linux commands as possible in limited time. How to get started: Go to https://www.redhat.com/en/command-line-heroes/bash/index.html in your computer's browser Game that teaches how to write Bash scripts: https://www.hackerrank.com/domains/shell?filters%5Bsubdomains%5D%5B%5D=bash
- Do you have a feel for basic Bash commands (ls, cat, cd, etc)
- Did you find yourself a favourite text editor? (vim, joe, nano, emacs) You will need one in the next lab!
- Are you capable of finding out what a command does, even if it is completely foreign?
- What does
nmap -sS -p 80 localhost
do? - What about
for i in `ipcs -s | awk '/apache/ {print $2}'`; do (ipcrm -s $i); done
- What does