# Linux

## Why Linux?

* Ansible do not run on Windows
* Kubernete do not plan to run on Windows

## Distro

KALI LINUX ™, Ubuntu, and Parrot all come from Debian.

CentOS comes from Red Hat.

## Shell

The **shell** is the command-line interpreter.

Type of shell:

* Bourne-Again Shell (bash)
* C Shell (csh)
* Korn Shell (ksh)
* Enhanced C shell (tcsh)
* Z Shell (zsh)

{% hint style="info" %}
**ksh** and **bash** use the dollar sign (**$**) to indicate where users type in their commands. Other shells, such as **zsh**, use the percent sign (**%**) for this purpose.

// or to check which type of shell, using

echo $SHELL&#x20;
{% endhint %}

## CLI

<details>

<summary>Basic commands</summary>

#### the ./

```bash
# indicate the command should be executed in the current directory
# instead of searching for it in system's path
./gcloud --version
```

#### mkdir

```sh
# -p: make entire directory hierarchy
mkdir -p /tmp/subdir/work/202304
```

#### rm

{% code overflow="wrap" %}

```shell
# -r: recursively
# remove all folder content & sub-folder
rm -r /tmp/subdir
```

{% endcode %}

#### mv

```bash
# default: -f
# move but do not overwrite
mv -n
```

#### cat

```sh
# add content to file
cat > contents_file.txt
This is some content text

# number the output lines
cat -n [filename]
```

#### less

```bash
# show contain with line number
less -B [filename]

# when in less, using /word to find [word]
/word
```

#### grep

```sh
# grep 'keyword' [target_file]
grep 'temporary password' /var/log/mysqld.log

# with pipe |
# list all .php file
ls -la | grep ".php"
```

#### apropos

```bash
# open manual of a command
man tail

# one-line description of the command
whatis nano

# search the manual page for specific description
# -a, --and option to search for multiple keywords.
apropos [keyword]
apropos -a change password
```

#### history

```bash
# to view all the history commands
history
```

</details>

<details>

<summary>pipe</summary>

```bash
# using output of 1st clause, use as input for 2nd clause
cat filename | grep "word"
```

</details>

<details>

<summary>curl</summary>

```bash
# curl 

```

</details>

<details>

<summary>Vi editor</summary>

Command mode & Insert mode. When open a VI editor, the default mode is COMMAND MODE.

#### open VI editor

```sh
vi index.html
```

#### switch to INSERT mode

* using <mark style="background-color:blue;">i key</mark> to enter insert mode.&#x20;
* using <mark style="background-color:blue;">esc key</mark> to return to command mode.

#### navigate in COMMAND mode

there are 2 ways

* using arrow keys
* using K (up) -J (down) -H (left) -L (right)

![](https://286463586-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4PHaS9B0QkS2WKcFwT0s%2Fuploads%2FXA5czZtocf0hVHJTZDAu%2Fimage.png?alt=media\&token=91a27940-ee36-41fa-98c6-1ce7f055b8b2)

#### find stuff

```sh
# for example, you want to find "of"
/of
```

#### copy, paste, delete

<img src="https://286463586-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4PHaS9B0QkS2WKcFwT0s%2Fuploads%2FvhRibfyRTSRcAOenBpno%2Fimage.png?alt=media&#x26;token=844a3279-705b-4224-ba40-9760e277bb7c" alt="" data-size="original">

#### save, quit

![](https://286463586-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4PHaS9B0QkS2WKcFwT0s%2Fuploads%2FNUAfCK2dr59oEut4NZXv%2Fimage.png?alt=media\&token=cf8bb561-f201-4324-957b-e3fb4b6f1c2c)

#### do not save, quit

Esc > `:q!`

</details>

<details>

<summary>User accounts</summary>

```sh
# check which user
whoami

# check id
id

# using system user
# if no username was specified, it will switch to root user
su [username]
Password: *****

# using ssh
ssh [username]@192.168.1.2

# using root permission
# ex: list content of /root folder, but the current user do not have the permission
sudo ls /root
```

</details>

<details>

<summary>Download files</summary>

```sh
# download some-file.txt
curl http://www.example.com/some-file.txt -O

# another way is using wget
# -O: specify the name of the local file you want to store
wget http://www.example.com/some-file.txt -O some-file.txt
```

</details>

<details>

<summary>Tips</summary>

#### check OS version

```sh
# using wild-card to check OS version
ls /etc/*release*

$ ls /etc/*release*
/etc/lsb-release  /etc/os-release
```

#### check details of OS version

```sh
cat /etc/*release*

$ cat /etc/*release*
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"
NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.4 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
```

</details>

#### SSH

{% embed url="<https://quickref.me/ssh>" %}

<details>

<summary>Connecting</summary>

```sh
# connect to a server with default port 22
ssh root@192.168.1.5

# connect with specific port
ssh root@192.168.1.5 -p 6222

# connect via pem file (400 permission)
ssh -i /path/file.pem root@192.168.1.5
```

</details>

<details>

<summary>ping</summary>

command to send network ICMP (Internet Control Message Protocol) echo requests to a specific IP addres&#x73;**.**

```bash
# -c option: specify the number of echo request send
ping -c 3 10.154.0.2
```

</details>

### Package manager

A **package manager** is a tool that helps users install, manage, and remove packages or applications. Linux uses multiple package managers.

#### .rpm

<details>

<summary>RPM (Redhat Package Manager) - Redhat</summary>

```sh
# install package
rpm -i telnet.rpm

# query the details of the package
rpm -q telnet.rpm

# get exact package name
rpm -qa | grep ftp

# uninstall package
rpm -e telnet.rpm
```

</details>

<details>

<summary>YUM (<strong>Yellowdog Updater Modified) - CentOS</strong></summary>

```sh
# install package
yum install ansible

# install package-version
yum install ansible-2.8.11-1.el7

# uninstall package
yum remove ansible

# list all YUM repos
yum repolist

# show all duplicated packages
yum --showduplicates list ansible

# show configuration of the repos
ls /etc/yum.repos.d/

# search for particular package
yum list ansible
```

</details>

#### .deb

<details>

<summary>dpkg - Debiban</summary>

</details>

<details>

<summary>APT (<strong>Advanced Package Tool) - Ubuntu/Debian</strong></summary>

**APT** is a tool used with *Debian-derived* distributions.

```sh
# install app: suricata
sudo apt install suricata

# remove app
sudo apt remove suricata

# list all installed app
apt list --installed
```

</details>

### Services

```sh
# start a service, e.g: httpd
service httpd start

# or
systemctl start httpd
systemctl stop httpd

# check status
# which file the service configured
systemclt status httpd
thor@host01 ~$ systemctl status app
● app.service - My python web application
   Loaded: loaded (/usr/lib/systemd/system/app.service; disabled; vendor preset: disabled)
   Active: inactive (dead)


# enable, disable after configuration
# when enable, the service start automatically when system boots up
systemctl enable httpd
systemctl disable httpd
```

#### \[app].service

```sh
thor@host01 ~$ cat /usr/lib/systemd/system/app.service
[Unit]
Description=My python web application

[Service]
ExecStart=/usr/bin/python3 /opt/code/my_app.py
ExecStartPre=/bin/bash /opt/code/configure_db.sh
ExecStartPost=/bin/bash /opt/code/email_status.sh
Restart=always

[Install]
WantedBy=multi-user.target
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mamawhocode.gitbook.io/devops/pre-requisite/linux.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
