DevOps Prerequisite (Part 1): Linux Basics
Tags: devops
Published: 2022-09-25 14:35:22


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itwbux6dj92mtp25o9lv.png)
 There might be different shell types when you try to work on shells:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipj7ikg6ctq3ynacvnhq.png)

You can check the shell type 

```
echo $SHELL
```
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vedbxpmxzdu19rdcigzd.png)
us

here echo commands lets you print and $ sign means an environment.

**Some basic commands**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r2pwqu5riubhgadw2rd.png)

If you want to create multiple directories, you can follow this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nd5gvgcq2vf5o9mvi1mw.png)

or,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kklrvpl8yet0wvwihtzv.png)

here you are using -p which means you are creating parent directory.

Also , you can remove a directory using this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/byj8phdzotynosbxlwc0.png)

use -r before file path.
Note: “-r” option for recursive 
To copy a directory, we can use:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugu44tr75elu3c2dypol.png)

This will copy my_dir1 to the next location.


**Work with files**
This is how you can create a file, edit contents in it, copy the file, remove the file/rename the file. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwxw6foja2l8ja7z8kjf.png)


Here you can see I created a dir_1 & dir_2 directory.
Then I entered into dir_1 directory and created a file called hey.txt . After that I added texts on that file. I also created a file named hlw.txt. I copied the content of hey.txt into hlw.txt

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uo65etny0p2immgaj35e.png)

All this, I have done in a directory named Demo.

Check out them in your system:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ocn7y3pxscjxffgao6da.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x8ih2j8vccpfitu9zwgc.png)


**Basics of VI Editor**
Most of the operating system has vi editor. To use it, go to terminal and use 

```
vi <file name>
```

For example,
We are in a directory where we have hey.txt and hlw.txt . Let's open the hey.txt using vi

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqaymbzfpyy6q8ir6sd6.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpb6dk8m1mc8bfq5652c.png)
You can see this kind of output.
Vi eidtor has 2 modes. When we open a file , it is generally in Command mode.
But you can not add content using command mode. For that, use insert mode by pressing "i" in keyboard.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cwqpbts146ocjcubgrc9.png)
To switch from insert mode to command mode, press esc key

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r2gebtma8z4bmaztffg7.png)

Some basic commands:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ac7fx68giccxn9tmc8l6.png)
To delete a word, use "x" but to delete a line , use "dd"
To copy, use "yy" but to paste use "p"
To scroll up use "Ctrl+u" and to scroll down, use "Ctrl+d"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11iaizaxtdnat2z6rzuz.png)
Also, press ESC and then typing ":" will take you to Command prompt, to save some changes, press ":w" . To quit changes , press ":q". To save and quit, press ":wq".
Now , if you want to find a word "of" , you need to use "/of" in prompt.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8uxa5ntyastko6iirb7.png)
To see all occurrences of "of", you need to use "n" to go to next "of".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdaubxjulp52499wezdc.png)



**More linux commands**
1. To know the user name of your system, you need to use "whoami" command.
2. To check user ids , use "id"
3. To switch to other user, use "su <usrname>" . Here su is for switch user.

4. To use a host using a username, follow the format, ssh username@hostname 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2evew9b1dtabum4q6s2.png)

Now, assume we have a user called "Mathew" who wants to access root user , but as he is a normal user ;he can not access it. 
Root user is something that can perform any task towards the system.

But if you need to work as a root user, you have a option which is "sudo"

using 

```
sudo ls/root 
```
or ,

Enter the command `sudo passwd root `. This will create a password for root, essentially "enabling" the account. Don't forget this password.
Type `sudo -i `. Enter the root password when prompted.
The prompt will change from $ to # , indicating you have root access.
```
sudo -i
```
will give access to root by adding you to /etc/sudoers file.

SO, the user Mathew is still a normal user but can have root benefits .


**How to download files?**

Use the curl command and then the link and also -O to download it , otherwise it will just show the file but won't download it.

```
curl <link> -O
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdn6iljaj3uaa8gcnvop.png)

Also, you can use wget command.

```
wget <link> -O <file name to which you want to save as>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pq1o4o0mz2le0fn295r.png)

For example.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vpwhqqv7l73ee0c1d732.png)


**Check the OS version of your system**

```
ls /etc/*release*
cat /etc/*release*

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onqorrp3js8mdp8okbyq.png)


For example,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymlrdiudzk7lbj8liasz.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mdul9jtd2drfq5p1r81.png)



**Package managers**
To install files, we need to use package managers. RPM (Red hat package manager) is one of them. See the commands to install some package using rpm.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9zvay3x6ab35yke0srm.png)
To check all of the packages installed using rpm

```
rpm -qa
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wd7277vkgtb2oh820s9s.png)

Assume, you have been asked to do this

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55yn0j6c4uzll9kg4uo0.png)
You have to first enable root access using 

```
sudo -i
```
then go into /opt folder and then check which file we have.
we had 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djesl7xitnj1t28btu5r.png)
then install using rpm

```
rpm -i <package you want to install>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ny6tgkfdqphrfil8knzc.png)


Again, assume you have been told to remove that ftp package

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mverdt46p9mcosev9449.png)
Here you can check the list of downloaded rpm packages using `rpm -qa` and search using `grep ftp`
BUt you can do this both in 1 command using

```
rpm -qa | grep ftp
```
SO,


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cd7h1gvbonumtth6jerc.png)




But rpm, does not install dependencies to run that package, thus we came up with "yum"
To install ansible using yum,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vxfiq570o193bed4v8t.png)

We use the command 

```
yum install ansible
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rrakfjb8gh82ixosaue.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23hmugrz1gv6a6m2iwbv.png)



This will search in cloud the dependencies it needs and download it . Also, yum uses RMP (Red hat package manager) to install things. Just the additional things it adds is that it downloads the dependencies.

The dependencies are listed in `/etc/yum.repos.d`

To check a installed package details , for example ansible use

```
yum list ansible
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/34qhc1pr0oc1dvzedehm.png)


to remove use

```
yum remove ansible
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uviazolvalr2aq8zpudy.png)


to check packages that you might download of previous versions and new ones

```
yum --showduplicates list ansible
```

to install a particular version, use

```
yum install <packagename>-<version>
```
if we are asked to Install ansible with yum but version should be 2.8.11


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxds9u71th6ff6gy74rx.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xec7op0fbq0lz3o5o5z.png)




**Services**
If any server is installed, it acts like sever and if there is any boot happening, it automatically starts working again.

To start a server called httpd
You can use both
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7w4vl4wqwrv6nt6m8ot.png)


```
service httpd start
```
or,

```
systemctl start httpd
```
Also, you can stop, check status, enable & disable servers

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxza282x6ldlf7662ppx.png)

For example, you have a my_app.py code which has 

```
print("Hello, World!")
```
to run the python file,
use 

```
/usr/bin/python3 /opt/cpde/my_app.py
```
/usr/bin/python3 is used to use python3 dependencies
and /opt/cpde/my_app.py is the location of the python file.

Now to show the output of the file to a host http://localhost:5000
use 

```
curl http://localhost:5000
```
The my_app.py file will be executed there.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6mocq7nfnc8pdvmlo8i.png)
 To make this code as a service, I can use

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvhbwrl7e86kqyda8twi.png)
To stop it as a service, I can use

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/giros1t2qyj0pj8ixsdv.png)

Now if we want our services to automatically start when system boots up etc then we need to make it systemd service. systemctl uses this actually. SO, lets make our my_app.py as one of them.

systemd service is configured from systemd unit file.
THis file is located at 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ou6wz5agmxx9ui1gwdif.png)
Create a service now 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvr6omis84jvgcfel817.png)
reload the systemctl

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/015qh0a5yusddvbtno1d.png)
Now start the service we created

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0ahtcs73qwi13u3t2hd.png)
Now you can check status and output in host

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/790keq1jm0jw2guuadrb.png)
Stop it using 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ey81wr0ixavjxisi0png.png)

To make our service run when the system bootups:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kvnu21ijkt4pstwwf2gs.png)

THen enable it

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrwdag9ehwoxtk3353zy.png)
For example, enabling in real life would look like this:
(WE started a httpd server and enabled it  so that it starts automatically when system boots up and we dont need to manually start the service.)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wba3oc1whmtyzlnapjo5.png)


FOr additional metadata is needed to provide,
use [unit]
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xl8nyux6wqdv71un35i4.png)

If the service has dependencies to run before and after , add them

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7cmcrx0wukrirf528vo.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ingc00d9oc82ft97asy1.png)

To restart it when system crashes, set 
Restart=always
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0l7zimfbx17k8qcixae5.png)

SO, that is it.
For example, this is the docker service file
You can learn more from [this course](https://kodekloud.com/courses/devops-pre-requisite-course/) from KodeKloud.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jta0eol77id3zhugh9c.png)