# Introduction to Linux

## Where does Linux originate from?

![Unix history](../assets/Unix_history-simple.svg)

* The spiritual mother of Linux is [Unix](https://en.wikipedia.org/wiki/Unix).
* [POSIX](https://en.wikipedia.org/wiki/POSIX) is not an OS in itself but a **standard**.
	* Which OS's are [POSIX compliant](https://en.wikipedia.org/wiki/POSIX#POSIX-oriented_operating_systems)

## Who is behind this project?

* Who is [Richard Stallmann](https://en.wikipedia.org/wiki/Richard_Stallman)?
* Who is [Linus Torvalds](https://en.wikipedia.org/wiki/Linus_Torvalds)?

## What is the deal with 'GNU-slash-Linux'?

* [GNU/Linux](https://en.wikipedia.org/wiki/GNU/Linux_naming_controversy) controversy
	* What is a [kernel](https://en.wikipedia.org/wiki/Kernel_(operating_system))
	* What does [GNU](https://www.gnu.org/software/) bring to the table?
	* What's the deal with [userspace and kernelspace](https://unix.stackexchange.com/questions/137820/whats-the-difference-of-the-userland-vs-the-kernel)?
		* [userspace](http://www.linfo.org/user_space.html)
		* [kernelspace](http://www.linfo.org/kernel_space.html)

![Linux kernel](../assets/linux_kernel.png)

## Where can you *get* some Linux?

* You probably already have a Linux computer running at [home](https://opensource.com/article/19/8/everyday-tech-runs-linux)!

![Timeline](../assets/distro_timeline.png)

## Debian

* [Reasons](https://www.debian.org/intro/why_debian) to choose Debian.
* Why is Debian called [the universal operating system](https://www.reddit.com/r/debian/comments/22j0wf/so_why_debian_is_called_the_universal_operating/)?
	* [comparison](https://en.wikipedia.org/wiki/Comparison_of_instruction_set_architectures) of CPU architectures
	* [download](https://www.debian.org/distrib/netinst) Debian

# How to create a virtual machine

In order to run virtual machines, or VM's, we need a **host** program.
One of the most popular ones out there is called `virtualbox`.
It should be installed on your machine but in case it's not you can download it [here](https://www.virtualbox.org/wiki/Downloads).
If you **expand** the section below you'll see a step by step walk-through of a VM creation in virtualbox with some notes on each step with best practice pointers.

<details>
  <summary>Expand me...</summary>

**Nice!**
 
1. Name your machine and select *linux* if it's not chosen automatically.
![virtualbox setup](../assets/2022-02-18-122703_1920x1080_scrot.png)
1. Set the amount of RAM for your machine.
This can be anything between an absolute minimum and your physical machine's limit.
You can't *magically* use more RAM than physically available!
![virtualbox setup](../assets/2022-02-18-122833_1920x1080_scrot.png)
1. You need a virtual hard drive to store the OS and all your data to.
![virtualbox setup](../assets/2022-02-18-122930_1920x1080_scrot.png)
1. The format doesn't matter that much.
I always go for the default one.
![virtualbox setup](../assets/2022-02-18-122943_1920x1080_scrot.png)
1. Most of the time you'll want dynamic size meaning the disk **file** will only take up as much **space** as it needs.
For example, if you set a size of 50GB for your disk and your OS plus personal files take up 12GB, the actual space this image takes op on the physical disk of your **host** will be about 12GB.
![virtualbox setup](../assets/2022-02-18-122951_1920x1080_scrot.png)
1. We'll soon discover **how much space** a basic Linux installation takes up but for now I would recommend the following.
	* **with** a graphical environment: 20GB
	* **without** a graphical environment: 10GB (minimal/server/headless install)
![virtualbox setup](../assets/2022-02-18-123011_1920x1080_scrot.png)
1. By default your machine will have only **one** CPU core but you can add more via the settings.
This is something you can change whenever you need more processing power but the same rule as with the RAM applies, it's not a magical way to add resources to your machine.
![virtualbox setup](../assets/2022-02-18-123047_1920x1080_scrot.png)
1. Last but not least we need to insert the installation disk into the virtual machine and boot it up!
![virtualbox setup](../assets/2022-02-18-123121_1920x1080_scrot.png)
</details>

# Breakdown of the Debian installation

All right, enough talking, let's get started!
I'll install a full blown and modern graphical Debian machine and I would like you to **not** do it yourself but take **notes** on each step so you'll be able to reference your notes later **when** I ask you to install a machine yourself.
Below is a gain a step by step walk through with some of my tips but a more details guide can be found in the [Debian Administrator Handbook](https://debian-handbook.info/browse/stable/sect.installation-steps.html).
This book is a real *bible* of information and I highly advise you to read through it.

My most important notes would be the following.

* **Read** each section carefully and you'll know what to do.
* Don't set a root password but use `sudo` instead.
* Don't install a graphical environment if you don't need one.
It's a lot easier to add one later than to remove it.
* Don't forget to install `grub` at the end otherwise your installation will not be able to boot.
You *can* recover from this mistake with [supergrub2](https://www.supergrubdisk.org/super-grub2-disk/) but that's for an other day.

<details>
  <summary>Expand me...</summary>

![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_32_45.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_32_57.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_33_07.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_33_11.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_33_18.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_33_21.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_33_27.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_34_18.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_34_29.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_34_34.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_34_39.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_34_52.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_34_55.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_35_01.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_35_17.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_35_20.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_35_23.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_35_27.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_35_32.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_37_53.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_37_57.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_38_00.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_38_02.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_39_44.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_39_53.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_40_07.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_49_11.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_49_15.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_50_26.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_50_38.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_50_55.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_51_07.png)
</details>

# Exercise

OK, now it's up to you!
Please install a Debian machine with the default graphical environment. 
Once installed, take some time out to explore the system.
Which software is installed by default?
How can you tweak the system settings such as language, keyboard, networking?
Can you install extra software?

# Quick look at an idle system

You can inspect the system by running the *gnome task manager*.
It lists all your running processes, sorted by CPU percentage, in the first tab which can give you a good idea of what's happening on your system. 
Note that an idle system uses almost **no resources**!
Plus, a fresh install, with quite a few essential programs such as a browser, music player, text editor, etc only takes up about 5GB of disk space!
This is mind blowing compared to a windows installation.
How is this possible?
That's the magic of a good package manager!

<details>
  <summary>Expand me...</summary>

![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_53_40.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_54_33.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_54_41.png)
</details>

# Package managers

What is a [package manager](https://en.wikipedia.org/wiki/Package_manager)

## Graphical installation

Modern Linux has come a long way and it's now quite usable without any command line knowledge.
We can install extra software from the graphical environment if we want.

Let's look for `vlc`, a popular open source video player.
When we look for it from `activities` we *see* a reference to it via the `software` program.
If we click on it we'll be take to a sort of *app or play store* where we can choose to install or uninstall programs.
Because adding and removing software from our machine is quite *invasive*, we need to **prove** we have the right to do so.
A prompt will pop up where you'll need to input **your** password.
If your password does not work, you probably set a `root` password and you should use that one instead.

<details>
  <summary>Expand me...</summary>

![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_51_24.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_52_06.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_52_15.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_52_36.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_52_46.png)
![Debian installation](../assets/VirtualBox_debian_18_02_2022_12_52_53.png)
</details>

## Command line installation

```bash
waldek@hellodebian:~$ htop
-bash: htop: command not found
waldek@hellodebian:~$ apt install htop
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
waldek@hellodebian:~$ sudo apt install htop
[sudo] password for waldek: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
  lm-sensors strace
The following NEW packages will be installed:
  htop
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 127 kB of archives.
After this operation, 328 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bullseye/main amd64 htop amd64 3.0.5-7 [127 kB]
Fetched 127 kB in 0s (2,030 kB/s)
Selecting previously unselected package htop.
(Reading database ... 141359 files and directories currently installed.)
Preparing to unpack .../htop_3.0.5-7_amd64.deb ...
Unpacking htop (3.0.5-7) ...
Setting up htop (3.0.5-7) ...
Processing triggers for mailcap (3.69) ...
Processing triggers for desktop-file-utils (0.26-1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for gnome-menus (3.36.0-1) ...
Processing triggers for man-db (2.9.4-2) ...
waldek@hellodebian:~$ 
```

![htop](../assets/VirtualBox_debian_18_02_2022_13_18_25.png)
![htop](../assets/VirtualBox_debian_18_02_2022_13_18_51.png)

The *menu bar* at the bottom shows you can press **F10** to quit.
This does not work because of a shortcut of gnome-terminal!
You can deactivate this shortcut in the preferences though.
But pressing **q** also exits the program.

![htop bug with gnome-terminal](../assets/VirtualBox_debian_18_02_2022_13_19_35.png)

## Adding a secondary desktop environment

During the initial installation we where offered a choice of desktop environments to install.
This menu was an actual program called `tasksel` we can run again to add different ones!
As it's a program that can heavily modify the system, we need administrator privileges.

```bash
waldek@hellodebian:~$ sudo tasksel
[sudo] password for waldek: 
```

![tasksel](../assets/VirtualBox_debian_18_02_2022_13_21_33.png)
![tasksel](../assets/VirtualBox_debian_18_02_2022_13_22_33.png)
![tasksel](../assets/VirtualBox_debian_18_02_2022_13_22_47.png)
![tasksel](../assets/VirtualBox_debian_18_02_2022_13_23_57.png)

# Changing the runlevel

The **first** process started by the kernel can be discover by `htop` and is `/usr/sbin/init`.
This is rather vague but we can find out a *bit* more about this program via `ls` which we'll get into more detail later.
For now just have a look at the output of the following commmand.

```bash
waldek@hellodebian:~$ ls -l /usr/sbin/init 
lrwxrwxrwx 1 root root 20 Jul 13  2021 /usr/sbin/init -> /lib/systemd/systemd
waldek@hellodebian:~$ 
```

Now, what *is* this [systemd](https://en.wikipedia.org/wiki/Systemd)?
And more importantly, *how* do we interact with it?
For now just accept that the main program to communicate with `systemd` is `systemctl`.
A quick look at the `man` pages gives us the following.

```bash
SYSTEMCTL(1)                                                         systemctl                                                         SYSTEMCTL(1)

NAME
       systemctl - Control the systemd system and service manager

SYNOPSIS
       systemctl [OPTIONS...] COMMAND [UNIT...]

DESCRIPTION
       systemctl may be used to introspect and control the state of the "systemd" system and service manager. Please refer to systemd(1) for an
       introduction into the basic concepts and functionality this tool manages.

```

```bash
waldek@hellodebian:~$ sudo systemctl set-default multi-user.target
[sudo] password for waldek: 
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /lib/systemd/system/multi-user.target.
waldek@hellodebian:~$ sudo reboot now
```

After the reboot you'll be confronted with the following screen.
Don't stress, your machine is not broken!
It's just running in a more *minimal* mode.
You can log in just as with the graphical login window but you'll only have a command line to interact with the machine.
A quick look at `htop` tells us not a lot of stuff is running, just the bare minimum.

![minimal runlevel](../assets/VirtualBox_debian_18_02_2022_13_40_00.png)
![minimal runlevel](../assets/VirtualBox_debian_18_02_2022_13_40_11.png)

To *undo* this change and regain the graphical interface again you can run the following commands.

```bash
waldek@hellodebian:~$ sudo systemctl set-default graphical.target
[sudo] password for waldek: 
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /lib/systemd/system/graphical.target.
waldek@hellodebian:~$ sudo reboot now
```

# Exercise

Please create a second virtual machine and install a **minimal** Debian.
Minimal means **no graphical environment**.
I advise you to **not** set a root password.

Once this machine is up and running, install a few programs.
The only one we saw up until now is `htop` but maybe try out `bmon`, `elinks` and/or `ranger`.
Once these are installed have a look at the running services and programs via `htop` and compare it to both your graphical installation **and** your graphical install but running in `multi-user.target`.


# Guest additions

Let's make our user experience a bit nicer.
Virtual machines can *integrate* with the host machine more fluently when you install the guest additions in the VM.
It's a three step process.

1. install the required packages to build the guest additions in you VM
1. insert the guest additions CD into your VM
1. run the correct script from the CD

The dependencies can be installed as follows.
First you `update` your package list, next you install three packages.

* `build-essential`
* `dkms` 
* `linux-headers-$(uname -r)`

The *third* package name is a bit weird looking no?
This is a bit of command line kung fu.
The actual *name* of the package is as follows.

```
waldek@hellodebian:~$ echo linux-headers-$(uname -r)
linux-headers-5.10.0-11-amd64
waldek@hellodebian:~$ 
```

The `$(uname -r)` is a call to a program called `uname` with the option `-r`.
We'll go more into detail on this later.

```
waldek@hellodebian:~$ sudo apt update
Hit:1 http://deb.debian.org/debian bullseye InRelease
Hit:2 http://deb.debian.org/debian bullseye-updates InRelease
Hit:3 http://security.debian.org/debian-security bullseye-security InRelease
Reading package lists... Done                 
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
waldek@hellodebian:~$ sudo apt install build-essential dkms linux-headers-$(uname -r)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu dctrl-tools dpkg-dev fakeroot g++ g++-10 gcc gcc-10 libalgorithm-diff-perl
  libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan6 libatomic1 libbinutils libc-dev-bin libc-devtools libc6-dev libcc1-0 libcrypt-dev
  libctf-nobfd0 libctf0 libfakeroot libgcc-10-dev libitm1 liblsan0 libnsl-dev libstdc++-10-dev libtirpc-dev libtsan0 libubsan1
  linux-compiler-gcc-10-x86 linux-headers-5.10.0-11-common linux-headers-amd64 linux-kbuild-5.10 linux-libc-dev make manpages-dev patch
Suggested packages:
  binutils-doc debtags debian-keyring g++-multilib g++-10-multilib gcc-10-doc gcc-multilib autoconf automake libtool flex bison gdb gcc-doc
  gcc-10-multilib gcc-10-locales glibc-doc libstdc++-10-doc make-doc ed diffutils-doc
The following NEW packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu build-essential dctrl-tools dkms dpkg-dev fakeroot g++ g++-10 gcc gcc-10 libalgorithm-diff-perl
  libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan6 libatomic1 libbinutils libc-dev-bin libc-devtools libc6-dev libcc1-0 libcrypt-dev
  libctf-nobfd0 libctf0 libfakeroot libgcc-10-dev libitm1 liblsan0 libnsl-dev libstdc++-10-dev libtirpc-dev libtsan0 libubsan1
  linux-compiler-gcc-10-x86 linux-headers-5.10.0-11-amd64 linux-headers-5.10.0-11-common linux-headers-amd64 linux-kbuild-5.10 linux-libc-dev make
  manpages-dev patch
0 upgraded, 43 newly installed, 0 to remove and 0 not upgraded.
Need to get 62.0 MB of archives.
After this operation, 258 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
```

Once this is done we can **insert** the CD.
You can verify the disk's content via the files explorer in gnome.

![CD](../assets/VirtualBox_debian_18_02_2022_15_01_18.png)

Now, open a terminal and run the following.

```
waldek@hellodebian:~$ cd /media/cdrom0/
waldek@hellodebian:/media/cdrom0$ ls
AUTORUN.INF  NT3x          TRANS.TBL                          VBoxLinuxAdditions.run          VBoxWindowsAdditions.exe
autorun.sh   OS2           VBoxDarwinAdditions.pkg            VBoxSolarisAdditions.pkg        VBoxWindowsAdditions-x86.exe
cert         runasroot.sh  VBoxDarwinAdditionsUninstall.tool  VBoxWindowsAdditions-amd64.exe
waldek@hellodebian:/media/cdrom0$ sudo bash VBoxLinuxAdditions.run 
Verifying archive integrity... All good.
Uncompressing VirtualBox 6.1.28 Guest Additions for Linux........
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel 
modules.  This may take a while.
VirtualBox Guest Additions: To build modules for other installed kernels, run
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup <version>
VirtualBox Guest Additions: or
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup all
VirtualBox Guest Additions: Building the modules for kernel 5.10.0-11-amd64.
update-initramfs: Generating /boot/initrd.img-5.10.0-11-amd64
VirtualBox Guest Additions: Running kernel modules will not be replaced until 
the system is restarted
waldek@hellodebian:/media/cdrom0$ sudo reboot now
```

Once the machine is rebooted you'll can resize the window and the screen resolution will adapt automatically!
You can also enable copy/paste and drag and drop between your host and VM now.

![full screen](../assets/VirtualBox_debian_18_02_2022_15_08_01.png)

