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
- Setting up CLI sandbox
- Trying out basics of bash shell
- Solving bash shell exercises
NB! In case are struggling understanding some of the abbreviations (or underlying terms), here are some references to read:
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 directory:
mkdir dir1
- listing the files in directory:
ls dir1
- renaming the directory:
mv dir1 dir2
- copying file
cp file1.txt dir2/file1.txt
- navigating to dir1
cd dir1
- navigating one level up in the directory tree
cd ..
- discovering the current directory (or current write directory (CWD))
pwd
- removing file
rm file1.txt
- removing all text files (by *.txt extension)
rm *.txt
- recursively removing all files in current directory
rm -r *
- creating directory:
- There many more commands we face during this lab (here we showed just few file management ones)
As you can see, working with CLI might feel not convenient, confusing and even dangerous, if lacking the basic CLI skills (the command showed in last example will remove all the files and directories from the current directory. There are many tutorials and best practice materials exists regarding the safe usage of the CLI. The truth is that just reading them is not enough without the practical exercises and while exercising we will eventually to errors, discover them and fix according to documentation. The question is if ruin the system once by misusing the CLI how we going to proceed afterwards ? It would be nice to have a kind of sandbox that would allow us to perform most of the CLI actions (even the dangerous ones) without damaging our actual system. Therefore we start our lab with building one ;)
Setting up CLI sandbox
In this section we are going to create the CLI sandbox for playing with the command line interpreter. We may refer to sandbox as an isolated environment or in case of UNIX command line we could just use the existing web based CLI emulators like the following one
... however we find those a bit limited and in it turns out the best learning experience is to use the same OS we are going to install on our servers later. Therefore, we will use the VirtualBox VM instance to isolate the running Live OS of Debian 9.3 GNU/Linux. The setup like that has a number of advantages compared to web-based:
- Standalone (no Internet is required once the Live ISO is downloaded)
- Performance (no lags in console rendering due to hypervisor support)
- Isolation (the actual host system is not affected due to virtualization)
- Compact (no additional hard drive space required apart from Live ISO)
- Recovery (even if you manage to ruin the system all the changes might be reset to default by just restarting the VM).
Setting up the Sandbox-VM
Here we expect you have the basic Operation Systems (OS) skills, like those obtained during the course:
- Operating Systems LTAT.06.001
In class room we have the class PCs with pre-installed VirtualBox software.
If you preferring class PCs, you can skip the next three sections.
Installing VirtualBox
If you prefer using your personal Laptop you may need to install the VirtualBox package:
- VirtualBox 5.1 Install Manual:
'''VirtualBox and it's alter
natives for Linux users'''
If you are Linux user, you in fact do not need any additional work this lab. As you have the Linux (CLI) natively in your OS. You can use the bash
shell of your Linux distro to solve the exercises of this week.
In case you want to have fresh environment (a sandbox) in order no to damage your own Linux while solving exercises of this week - you may consider the following options:
- Install
VirtualBox
package using the package manager of your Linux distro.- Once the VirtualBox package is installed, you should continue similarly to the Windows users.
- Debian/Ubuntu/Mintu (deb-based)
# apt-get install virtualbox virtualbox-qt
- ... or follow manual on VirtualBox page section Debian-based Linux distributions
- Fedora/Suse/RHEL (rpm-based)
- follow manual on VirtualBox page section RPM-based Linux distributions
- Arch/Gentoo
- you definitely know how to install VirtualBox :)
- Another option for you may be the
Qemu/KVM
emulator (alternative to VirtualBox):- detailed manual on Qemu/KVM you may read here
Enabling CPU-virtualization (Intel VT-x or AMD-V) features
In order to achieve better performance both VirtualBox and Qemu/KVM can rely on virtualization feature of the host CPU. The presence or absence of this feature depends on the CPU model and host Bios settings. Checking the feature state can be done as follows:
- In Windows video
- In MacOS manual
- In Linux:
# lscpu
- and make sure in Virtualization entry there is
VT-x
value - ... if you do not have an
lscpu
command, make sure theutil-linux
package is installed - or use the command:
# cat /proc/cpuinfo |grep -E "vmx|svm"
- ... and make sure either
svm
orvmx
is in the list of processor flags.
- and make sure in Virtualization entry there is
NB! In case you do not have a virtualization flag enabled, you should enable it in the BIOS of your personal PC or Laptop. Reboot the Laptop and issue delete
or F1
or F2
keys to enter the BIOS (usually there is a usage message viewed during the boot showing BIOS setup and other options).
NB! In case you cannot find the virtualization option in the Bios settings, it may be due to absence of this feature in the processor (check the specifications of the processor model). Both VirtualBox and Qemu can perform without it, the performance of the VM however might be slower.
Creating the VM
Next let's create VM suitable to run Debian 9.3 GNU/Linux. This newly created VM becomes our sandbox for playing with command line.
>>todo<<
First of all download the ISO file of the distribution:
NB! Class PC users, please avoid downloading the ISO twice in case you find the corresponding file is present in the PC you are currently using.
ISO File md5sum: dfbcb2f2df816d66d74032493bdcfeb2
Once the ISO downloaded and is valid, we may setup the VM (dependent on chosen emulator):
- For
Qemu
users:- in command line, start Qemu as follows:
qemu-system-i386 --enable-kvm -m 256M -cdrom debian-live-9.3.0-i386-xfce.iso -boot d
- the options used here:
--enable-kvm
forcing KVM hypervizer (CPU-virtualization feature has to be enabled)-m 256M
amount of RAM to assign for the VM-cdrom debian-live-9.3.0-i386-xfce.iso
install Live ISO file into VM's CD-ROM-boot d
force VM to boot CD-ROM first
- in command line, start Qemu as follows:
As a result the VM should start Live CD boot process
- the issued command will run blocking your CLI
- new window will opened illustrating VM's VGA output
- NB! For releasing the mouse pointer use left
Ctrl+Alt
key combination
- NB! For releasing the mouse pointer use left
- you should see the boot menu page with possible boot options
Killing the running VM instance can be do by
- Closing the VM-window
- Issuing
Ctrl+C
in the CLI
Remember the corresponding command you used to run the VM again once you have killed it.
- For
VirtualBox
users:- Open the VirtualBox application, and click
New
Button- Give a new VM a name
SandboxDebian
- Choose a type
Linux
- Choose version
Debian (32-bit)
- Click
Next
- Specify amount of memory
512MB
, as this will be enough fortesting purposes
- Click
Next
- Select not create any virtual hard drives (we running Live CD - there is no need for storage)
- Click
Next
- The warning should appear (telling no HDD is specified), but we agreed - this what we need
- Click
Continue
- Give a new VM a name
- The newly created VM should be visible in the VM list (in the left in VirtualBox main window you should see
SandboxDebian
icon) - Right-click on it and select
Settings
- Go to
System
->Processor
tab -> select2 CPU
cores andEnable PAE/NX
->Acceleration
tab -> Verify thatEnable VT-x/AMD-V
is marked -> ClickOK
.
- Go to
- Double-click on
SandboxDebian
VM in the list of VMs - During the first boot, the VM will ask to provide an ISO image to boot from
- Provide the ISO file of the live version of Debian 9.3 which was downloaded.
- Open the VirtualBox application, and click
As a result the VM should start Live CD boot process
- the new window will appear illustrating VM's VGA output
- For releasing the mouse pointer use right
Ctrl
key
- For releasing the mouse pointer use right
- you should see the boot menu page with possible boot options
In order kill/reset the running instance:
- In menu
File
of the running VM's window - Select
Close
- Chose
Power off ...
Next time you need to run the VM you can just double-click the corresponding icon in the VM list
Now we are ready to boot into Debian Live and start playing with CLI!
Booting Sanbox-VM and accessing CLI
After you have started the VM a Debian Live boot windows should appear:
- Please select
Debian GNU/Linux Live (kernel 4.9.0-4-686)
and hitEnter
- The standard boot process will take place and bring to a login prompt:
- The default user for any Debian live is
user
with passwordlive
- The default user for any Debian live is
Now we have the shell of Debian Live and we are ready to refresh our memory with Linux basics. In order to do so we heavily recommend working through the following tutorial:
- If your computer has Estonian/Non English keyboard It might be more convenient to install your native keyboard to the VM. There are 2 methods for doing it.
- Method 1
- there is a line:<property name="XkbLayout" type="string" value="us"/> in file .config/xfce4/xfconf/xfce-perchannel-xml/keyboard-layout.xml
- in order to allow more layouts we need to modify this line adding more valueslike "us,ee"
- we can use sed script to do so cat <file> | sed 's/us\,/us\,ee\,/g'now *** we just need to store the output under
- Method 2
- setxkbmap ee - estonian keyboard
- Method 1
- NB! Recommended for All
- Learning the Shell by William E. Shotts, Jr. © 2000-2017
- This one contains the well-detailed tutorial of the
shell
usage
- This one contains the well-detailed tutorial of the
- Learning the Shell by William E. Shotts, Jr. © 2000-2017
While reading tutorial try to solve the following exercises: (assuming dir is an abbreviation for directory, folder)
- 1. What is current working directory (Command: PWD) ?
- 2. Go to your home dir.
- 3. Make a new dir to your home dir (give it any name you like).
- 4. List the contents of the working dir (can you see the newly created directory in there?).
- 5. Go to your new dir.
- 6. Make sure you are in your new dir.
- 7. Create a new file "mytest" to the working dir.
- 8. Append some text into the file you have just created.
- 9. Print the content of the file to standard output.
- 10. Open the file with your the editor (nano, vi, ed, emacs ... which one you like better?).
- 11. Write something into your file.
- 12. Save and close your file.
- 13. Print the content of the edited file again.
- 14. Make a copy of your file.
- 15. Make sure that the copy was successful. How can you make sure ? (file size, modification date, checksum ...)
- 16. Print the contents of both files simultaneously.
- 17. Delete the original file.
- 18. Make sure that the original was deleted.
- 19. Clear the terminal window contents.
- 20. Rename the copy to "theNewFile".
- 21. How big is your file?
- 22. Copy the file to your home dir.
- 23. Delete the file from the working dir.
- 24. Make sure that the working dir is empty.
- 25. Copy the file back from the home dir to your working dir without changing the working dir.
- 26. Remove the file from the working dir without changing your working dir.
- 27. Make sure that the copied file is not in your home dir anymore.
- 28. Go back to home dir.
- 29. Delete the dir you created while the file is in this dir.
- 30. Prove that both the dir and file are deleted.
Users and file ownership
- 1. Create user someone.
- 2. Create a new dir.
- 3. Go to new dir.
- 4. Create file.
- 5. Change user to someone.
- 6. Go he previously created dir.
- 7. Try to edit previously created file.
- 8. Can you?
- 9. Why?
- 10. What should be changed to allow the new user to modify said file?
- 11. Who can premit such change?
- 12. Do it.
- 13. Modify the file as the new user.
- 14. Save changes.
- 15. Revert back to the your original user.
- 16. Is the file changed?
- 17. Remove the premissions for the previously created user.
- 18. Add the user to your user group.
- 19. Can your original user do it?
- 20. Why?
- 21. Become someone who can.
- 22. Add the created user to your original user group.
- 23. Become original user.
- 24. Become the created user.
- 25. Can the new user modify the file?
- 26. Why?
- 27. Revert back to your original user.
- 28. Were the changes successful?
- 29. Delete the file.
In case you need more details about particular shell usage you may refer to:
- 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
- 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 life 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.
- Advanced Bash-Scripting Guide
- Just in case you fee bored by simple tutorials ;)
- Alo Peets' cheat sheet about Linux
Advanced bash exercises will be shared during the lab!