# Lab3 - LCL

Open Source-based technologies reduce the prospect of a product suddenly becoming obsolete. It allows for more collaboration and better innovation between industries.

{% hint style="info" %}
Linux is one of the most popular operating systems among professionals. In this article, you'll learn the most-used Linux commands. Also, you will be introduced to Docker!
{% endhint %}

&#x20;

## Introduction

In this Lab, you will learn how to work with Docker. Before talking about Docker, let’s take a moment to highlight containers. A container packages code and all its dependencies into a single unit, thus letting an application run quickly and reliably from one computing environment to another. This makes such applications easily portable between machines and solves the “it works on my machine” problem. Though the technology behind containers has been around for a while, Docker made it easier to work with containers. Since its debut in 2013, Docker has become an industry standard. Currently, the core technology exists as a popular, open-source container runtime called Docker Engine.&#x20;

To create Docker containers, you’ll first need a Docker image. If you’re familiar with object-oriented programming concepts, think of images as classes and containers as objects.    Images include everything needed to run an application: code, runtime, system tools, system libraries, and settings.&#x20;

Docker is ideal for the following use cases, and many more:

* Software prototyping and packaging
* Microservice architecture implementation&#x20;
* Network modeling
* Continuous integration and delivery
* Reducing debugging overhead
* Running more workloads on the same hardware      &#x20;

&#x20;

## Docker Desktop App

One of the best ways to get started with Docker is by installing Docker Desktop—especially if you’re a developer using Mac or Windows. That said, you might be wondering, “What’s Docker Desktop, and how’s it different from the open-source Docker Engine?”&#x20;

While some developers envision Docker Desktop as just a GUI on top of Docker Engine, that characterization barely scratches the surface. Docker Desktop is an easy-to-install application and includes Docker Engine, Docker CLI client, Docker Compose, Docker Content Trust, Kubernetes, and Credential Helper. Docker Desktop still uses Docker Engine at its core. However, the seamless integration and interoperability of these tools make Docker Desktop user-friendly—regardless of your experience with Docker.

By installing and using Docker Desktop, you’ll enjoy the following features:

* Simple and easy-to-install environment to build, ship, and run your containers
* Easy way to create and manage using volumes
* Local and remote management of Docker images
* Better collaboration by sharing repeatable and reproducible development from your local machine to the container
* Simple, one-click Kubernetes setup for your local machine
* A dashboard for a quick overview of running containers, images, and volumes
* Support for building and using multi-architecture images

&#x20;

In the next section, we will see how to run Linux using the Docker Desktop App

## Running Linux

First head over to <https://hub.docker.com/> and complete the registration process, then login to your account

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2F2AwcGs0nT90cxa2iOECG%2Fimage.png?alt=media&#x26;token=f45d9cdc-5869-4069-9bc9-1e4c3a73a56d" alt=""><figcaption></figcaption></figure>

&#x20;

Then install the Docker desktop app from <https://www.docker.com/products/docker-desktop/> and log in with the credential you just created.&#x20;

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2FqCGwB1ZP9y5oWmpTq7OP%2Fimage.png?alt=media&#x26;token=d80a3ad1-d60d-48b2-9100-40d26d46ef20" alt=""><figcaption></figcaption></figure>

On the search bar, type Ubuntu and then Run its latest version:

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2FFxO1wgFvV3bCYMVUrDQ8%2Fimage.png?alt=media&#x26;token=5ebc7190-4399-4072-94bd-7fe762873616" alt=""><figcaption></figcaption></figure>

When you Run the container, you will see this message in the terminal:

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2FXuMZpEpzZJNwVbK3o7En%2Fimage.png?alt=media&#x26;token=d7b83f7b-c33c-488f-8cb6-e2476c9af6bd" alt=""><figcaption></figcaption></figure>

That means you need to run your container in interactive mode to be able to work with it. So let’s run the command prompt and then execute the following commands:

* Run the powershell
* Use: `docker ps` to see the running processes or containers
* Use: `docker ps -a` to see the stopped containers as well
* Finally, use: `docker run -it ubuntu` to run the container in an interactive mode

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2FxDsM7BBJPlxM3LC4bYSJ%2Fimage.png?alt=media&#x26;token=a68a4aa3-36c3-4cd9-b965-d04e6de453af" alt=""><figcaption></figcaption></figure>

What we have here is called The Shell, It is a program that takes our commands and passes them to the OS for the execution. In this line, `root@982250960ecd:/#`, the root is the current user, after `@` we have the name of the machine, i.e., `982250960ecd`, automatically generated by Docker, after ‘:’ you can see where we are in the file system, i.e., `/`, which represents the root directory, finally we have `#` which means we have the highest privileges. If I logged in as a normal user instead of # you would see a `$` sign.

Next, we will explore some Linux commands.

## Working with Linux Command Line

Let’s see the location of the shell program. You need to run `echo $0` as you can see it refers to `/bin/bash` , bash is the enhanced version of the original shell program. Also using the history command you can see all the commands you have executed lately.

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2F48bFk1hCcfqr5pEIXiUP%2Fimage.png?alt=media&#x26;token=4d2ebb3d-4234-4ccd-9c25-48071e7e3ecc" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
*Note #1: “shell” is a broad term that refers to any program that provides a command-line interface, "Bash" is a specific type of shell that is widely used in Unix/Linux systems.*
{% endhint %}

{% hint style="warning" %}
*Note #2: if you have noticed in Windows, we use a forward slash but in Linux, we use a backslash to separate directories.*
{% endhint %}

{% hint style="warning" %}
*Note #3: Linux is a case-sensitive operating system! There is no command “Echo” it is “echo”!*
{% endhint %}

## Working with Grep

Now we are going to learn about "grep" command. Grep stands for *Global regular expression print*. As the name implies, Grep is used to search text files with regular expressions (shortly regex). It prints the lines matching the given pattern in a text file. If no file is given, grep will recursively search the given pattern in the files in current directory. Grep has two variants, namely *egrep* and *fgrep*. These variants are deprecated but are provided for backward compatibility. Instead of using "grep -E" and "grep -F", you can use "egrp" and "fgrep", respectively.

First, let’s create a file with some random worlds and show its content.

`cat file.txt`

| Here is the output:                                                                                                                      |
| ---------------------------------------------------------------------------------------------------------------------------------------- |
| <p>ostechnix</p><p>Ostechnix</p><p>o$technix</p><p>linux</p><p>linus</p><p>unix</p><p>technology</p><p>hello world</p><p>HELLO world</p> |

&#x20;

To begin the search, just type grep followed by what it is you're looking for and where you are looking from. For example, I am going to look for the string "nix" in file.txt. To do so, I run:

`grep nix file.txt`

What did you get as the output?! (Include results in your learning journal)

{% hint style="warning" %}
*Note#4: You can also use -n flag to show the line numbers in the output. This can be useful when you're working with a really long code.*
{% endhint %}

Please note that grep is case-sensitive. What you will get if you run these commands:

```
grep os file.txt
grep -i os file.txt
grep -i 'hello world' file.txt
```

(Include results in your learning journal)

We can also pipe an output of a command into grep. Have a look at the following example.

```
cat file.txt | grep os
```

(Include results in your learning journal)

Now see what we've got. The output of the file.txt is piped into grep and the words that contain the letters "os" in file.txt have been displayed.

We can also use some special characters or regular expressions in grep.

* ^ - search at the beginning of the line.
* $ - search at the end of the line.
* . - Search any character.

What you will get if you run the following commands:

```
grep tech file.txt
grep ^tech file.txt
grep x$ file.txt
grep .n file.txt
```

(Include results in your learning journal)

You should now have a basic understanding of grep usage.

## Managing Users in Linux

&#x20;

In Linux we have some commands to add, modify, and delete a user:

```
useradd, usermod, and userdel
```

If you run this `useradd --help` you will see a bunch of options, you can use with this command. Here we only need `-m` or `--create-home` and let’s create the user called *john*.

```
useradd -m john
```

where is this user?! And how can I verify it?!

The user’s info is stored in a configuration file in `etc` directory. Let’s check it:

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2F1Y6jYUDiOaN7MEB0CcOs%2Fimage.png?alt=media&#x26;token=6cd4904c-8b99-45eb-ad2a-45a3a41d03ea" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
*Note #5: the name passwd is a bit misleading! Only user’s info will be stored here not their password!*
{% endhint %}

```
john:x:1000:1000::/home/john:/bin/sh
```

In this line “x” means the password is stored somewhere else! “1000:1000” refers to the user’s and its group id. Then we have “/home/john”, the home directory of the user, finally, we have “/bin/sh” the shell program used when this user logs in to this system.

&#x20;

But what if we want to use bash instead of shell when this user logs in? we should modify this information using `usermod` the command. First, let’s see what options we have:

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2FilFqPQA6KynlcSl9blUX%2Fimage.png?alt=media&#x26;token=3bcd0a5e-6c17-49cc-9ea4-656e21134fc5" alt=""><figcaption></figcaption></figure>

Here I am going to use -s to modify the shell program.

```
usermod -s /bin/bash john
```

Now we can verify it!

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2FtHsx72GIf60QvHQUsrtc%2Fimage.png?alt=media&#x26;token=9ac9011b-b6c8-4f58-89ca-86ed8fbe51e7" alt=""><figcaption></figcaption></figure>

&#x20;

Where are the passwords?! There is another file called shadow where all password is stored in an encrypted format. This file is only accessible to the root user!

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2Fk8jn9MqKSqtwYIHLyLqQ%2Fimage.png?alt=media&#x26;token=f971b10e-6bbc-4393-9875-59f43154563f" alt=""><figcaption></figcaption></figure>

To learn more about how this file works take a look at [this link](https://www.techtarget.com/searchsecurity/definition/shadow-password-file).

Now that we have created the user, we can login to the container with this user. To do this, while your container is still running in a new tab in the command prompt run the following commands:

First, let’s check if your container is still running and get its id with `docker ps`

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2F5AYF22kScpLxXnX2CAm7%2Fimage.png?alt=media&#x26;token=e950238c-8e40-44a8-8708-4119d20e8747" alt=""><figcaption></figcaption></figure>

In my case, the id is: `217ef9e08bba`. You may get a different ID!

Then use the command exec to execute the bash program in this container with the user *john*

Then use the following command to log in with the new user:

```
docker exec -it -u john 217ef9e08bba bash
```

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2F5YGgeKCEClsWsUDDlTh6%2Fimage.png?alt=media&#x26;token=efa49337-78cd-4165-9da1-e6ec66f8b55a" alt=""><figcaption></figcaption></figure>

Let’s see what happens if I want to access the shadow file using john user!

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2F3wC0u6XtltFETKI6ad5Y%2Fimage.png?alt=media&#x26;token=ba89da34-e4be-47fa-94f0-471f3d225f97" alt=""><figcaption></figcaption></figure>

This verifies that I am not a root user!

{% hint style="warning" %}
*Note #6: we also have an alternative command to create a user “adduser” it is more interactive than “useradd” and also allows you to set the password as well as additional information when you create a new user.*
{% endhint %}

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2FJWJxUWBfELCtI7FTXzQE%2Fimage.png?alt=media&#x26;token=fa04d4c0-b906-4079-90a6-2673c2b9be76" alt=""><figcaption></figcaption></figure>

## Managing Groups

First, let’s answer this question why do we need groups? All users in the same group will have the same permission to the system.

Let’s create a group called developers:

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2F9vdv2wvqI2L0XOJlO7Ow%2Fimage.png?alt=media&#x26;token=597bf7c9-5bec-4dab-815b-e2e297a14e13" alt=""><figcaption></figcaption></figure>

We can get its information in a configuration file in etc dir.

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2FYHNQIXMeEG8bXImN7fnP%2Fimage.png?alt=media&#x26;token=5603f9fb-f06c-45f0-a274-eb504d1591ae" alt=""><figcaption></figcaption></figure>

Now we want to add *`john`* to this group. Again we need to use `usermod` command but this time with different options.

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2FmUJyKrYCwx3SyOR6JXZw%2Fimage.png?alt=media&#x26;token=bc4f4802-b815-44c6-b333-f3c30acdc628" alt=""><figcaption></figcaption></figure>

As you can see we have two options: `-g` and `-G` what are the differences?

`-g` is used to modify a user’s primary group but `-G` is used to modify a user’s supplementary group.

* *Primary Group*: Used to decide which files are created by which users. One user must belong to only one group.
* *Supplementary Group*: Specifies one or more groups for users to share files between them. One user can belong to up to 15 secondary groups.

You can check out [this link](https://www.hostingadvice.com/how-to/linux-add-user-to-group/) to understand.

Here we want to modify john’s supplementary group:

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2FDXwH6LWwYV4yXFAS2c0o%2Fimage.png?alt=media&#x26;token=9131317c-6f00-42cd-a3f4-31c7acae2d7c" alt=""><figcaption></figcaption></figure>

We use the `groups` command to confirm this.

&#x20;

## File Permission

First, let’s go to the home directory and create a file called `deploy.sh` Files with this extension are called shell scripts. In these files, you can put your script and run it in one go!

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2FnuRZGtBMrJmxRRSvJwUC%2Fimage.png?alt=media&#x26;token=d41487e1-8fd7-4bb7-bbfc-a6a1529f503c" alt=""><figcaption></figcaption></figure>

To get permission for this file we should get a long listing:

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2FL7WWUBRo6lXIf08Zxq74%2Fimage.png?alt=media&#x26;token=040ac5ec-bb6c-4aa4-87db-d5b7d48e75b8" alt=""><figcaption></figcaption></figure>

* If the first letter is – it is a file. *d* means it is a directory
* We have 9 letters divided into 3 groups, e.g,. we have rw-r--r-- for the deploy.sh file. The first group is the permission for the user who owns this file. The second group is for the group that owns this file. The third group is the permission for everyone else.
* In each group we have *read*, *write*, and *execute* permissions. In john dire we have rwx that means we have full permission. We have *x* because we want to go into this directory.

If we try to execute the deploy.sh file we will get a permission error!

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2Fjn74qfoyrpQVFvR0XWPQ%2Fimage.png?alt=media&#x26;token=4b5318e5-2278-4d93-91e9-c5f8b34b7ede" alt=""><figcaption></figcaption></figure>

Because the root user does not have the execute permission.

Let’s add and then remove execute permission for the user who own this file with the command `chmod`:

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2Fgrgm3DDoIiyd12KfK9IK%2Fimage.png?alt=media&#x26;token=bf19640f-adc8-4656-81fc-9b9b6ed453d3" alt=""><figcaption></figcaption></figure>

We can do the same for the group and also others:

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2Fh3lDHMgqpKQQCksTkYfZ%2Fimage.png?alt=media&#x26;token=525b465d-1ca6-4b34-89a3-612190056a2f" alt=""><figcaption></figcaption></figure>

With that even john can execute this file! Because others have the *x* permission

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2F1tPo6IumlSd4lY815one%2Fimage.png?alt=media&#x26;token=a36fa498-a584-4eee-a80d-ae327ad56f38" alt=""><figcaption></figcaption></figure>

We can also combine these in one command:

<figure><img src="https://1117245002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAlkbozUljrP3jqwa7by9%2Fuploads%2F7w4PTXoGI8FL4EW2Q9oN%2Fimage.png?alt=media&#x26;token=a796bc1d-0abc-4d95-85d4-74cd7c04973f" alt=""><figcaption></figcaption></figure>

Here we removed x, r, and w from the group and the others.
