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
CLI Intro
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.
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: mouse@lab.cs.ut.ee
Let's log into a machine called "lab.cs.ut.ee" with our university users. For Linux/Mac users this task is easy (classroom computers have Linux installed) - open terminal, and insert the previous command. Windows people will need to find out a solution for themselves. Common tool is called "putty".
You need to be able inside the University network to log in into that machine. University VPN helps if you're not in class.
Bash introduction
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 (give it any name you like).
- 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 some text into the file you have just created.
- Print the content of the file to standard output.
- Open the file with your favourite text (nano, vim, ed, emacs, joe ... which one you like better?).
- Write something into your file.
- Save and close your file.
- Print the content of the edited file again.
- Make a copy of your file.
- 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.
- Move the file to /tmp.
- Check the contents of /tmp without being in there.
- Go into /tmp.
- Make a directory, then a directory inside that directory, and then another directory inside the previous one.
- Move your file into the deepest directory with one command, without moving inside them.
- Delete the directories you have made recursively.
- 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?
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 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
Intermediate tasks
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 when no root access (cannot use sudo, 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/random 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