DEV Community

Cover image for DevOps Prerequisite (Part 1): Linux Basics
Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on • Edited on

DevOps Prerequisite (Part 1): Linux Basics

Image description
There might be different shell types when you try to work on shells:

Image description

You can check the shell type

echo $SHELL
Enter fullscreen mode Exit fullscreen mode

Image description
us

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

Some basic commands

Image description

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

Image description

or,

Image description

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

Also , you can remove a directory using this:

Image description

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

Image description

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

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

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

Check out them in your system:

Image description

Image description

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

vi <file name>
Enter fullscreen mode Exit fullscreen mode

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

Image description
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
To switch from insert mode to command mode, press esc key

Image description

Some basic commands:

Image description
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
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
To see all occurrences of "of", you need to use "n" to go to next "of".

Image description

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 " . Here su is for switch user.

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

Image description

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 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Image description

Also, you can use wget command.

wget <link> -O <file name to which you want to save as>
Enter fullscreen mode Exit fullscreen mode

Image description

For example.

Image description

Check the OS version of your system

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

Enter fullscreen mode Exit fullscreen mode

Image description

For example,

Image description

Image description

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
To check all of the packages installed using rpm

rpm -qa
Enter fullscreen mode Exit fullscreen mode

Image description

Assume, you have been asked to do this

Image description
You have to first enable root access using

sudo -i
Enter fullscreen mode Exit fullscreen mode

then go into /opt folder and then check which file we have.
we had

Image description
then install using rpm

rpm -i <package you want to install>
Enter fullscreen mode Exit fullscreen mode

Image description

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

Image description
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
Enter fullscreen mode Exit fullscreen mode

SO,

Image description

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

Image description

We use the command

yum install ansible
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

to remove use

yum remove ansible
Enter fullscreen mode Exit fullscreen mode

Image description

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

yum --showduplicates list ansible
Enter fullscreen mode Exit fullscreen mode

to install a particular version, use

yum install <packagename>-<version>
Enter fullscreen mode Exit fullscreen mode

if we are asked to Install ansible with yum but version should be 2.8.11

Image description

Image description

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

service httpd start
Enter fullscreen mode Exit fullscreen mode

or,

systemctl start httpd
Enter fullscreen mode Exit fullscreen mode

Also, you can stop, check status, enable & disable servers

Image description

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

print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

to run the python file,
use

/usr/bin/python3 /opt/cpde/my_app.py
Enter fullscreen mode Exit fullscreen mode

/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
Enter fullscreen mode Exit fullscreen mode

The my_app.py file will be executed there.

Image description
To make this code as a service, I can use

Image description
To stop it as a service, I can use

Image description

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
Create a service now

Image description
reload the systemctl

Image description
Now start the service we created

Image description
Now you can check status and output in host

Image description
Stop it using

Image description

To make our service run when the system bootups:
Image description

THen enable it

Image description
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

FOr additional metadata is needed to provide,
use [unit]
Image description

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

Image description

Image description

To restart it when system crashes, set
Restart=always
Image description

SO, that is it.
For example, this is the docker service file
You can learn more from this course from KodeKloud.

Image description

Top comments (1)

Collapse
 
iftakharrahat profile image
IftakharRahat

Very informative blog vaiya