Linux | DevOps | AWS

Linux | DevOps | AWS

Linux with DevOps

Thanks for joining me in this DevOps Learning Journey. Let’s explore!

What is Linux?

Linux is an open-source operating system (OS). The Linux OS was developed by Linus Torvalds in 1991, which sprouted as an idea to improve the UNIX OS. He suggested improvements but was rejected by UNIX designers. Therefore, he thought of launching an OS, designed in a way that could be modified by its users. Nowadays, Linux is the fastest-growing OS. It is used from phones to supercomputers by almost all major hardware devices.

-> Linux is a kernel, not OS

-> Linux is not a UNIX derivative it was written from the scratch

-> A Linux distribution is the Linux kernel and a collection of software that together, create an OS

-> Linux distribution means different types of Linux version

-> Secure :

-> Light weight - less memory required

-> Package means Software

-> OS = Kernel + GNU (Collection of software)

 - OS = Kernel + software packages

Structure of Linux

Why should we learn Linux?

  • Free and Open-Source.

  • Extremely Flexible.

  • Lightweight Infrastructure.

  • Great in-build security

  • End-to-end encryption

  • Portable Environment

  • Shell/ Command-line Interface

  • Automation friendly

What is a “distribution?”

Linux has a number of different versions to suit any type of user. From new users to hard-core users, you’ll find a “flavor” of Linux to match your needs.

Popular Linux distributions include:

  • LINUX MINT

  • DEBIAN

  • UBUNTU

  • SOLARIS

  • FEDORA

  • OPENSUSE

And don’t think the server has been left behind. For this arena, you can turn to:

  • Red Hat Enterprise Linux

  • Ubuntu Server

  • Centos

  • SUSE Enterprise Linux

Basic Linux commands:-

  • cd path_to_directory --> change directory to the provided path

  • cd .. --> chnage directory to one step back.

  • cd ../.. --> Change directory to 2 levels back.

  • cd ~ or just cd --> change directory to the home directory

  • pwd --> print work directory. Gives the present working directory.

  • whoami → to know current username

  • date -> to see current date and time with time zone

  • uptime -> to see host uptime

  • top--> current running processes with high cpu

  • free -m--> to know current memory and swap usage

  • df -h--> information about all the file systems human readable format

  • grep -> to filter with specific name

  • mkdir -> make directory

  • mkdir -p A/B/C/D/E -> to create a nested directory A/B/C/D/E. The -p switch creates parents’ directories.

  • touch -> make file (with specific extension if specified)

  • ls -> list the sub directories and files avaiable in the present directory

  • ls -l--> list the files and directories in long list format with extra information

  • ls -a --> list all including hidden files and directory

  • ll--> The ll command can be used to list the contents of a directory, the properties of a file, or both.

  • echo -> to print something

  • awk -> Scans a file line by line ,Splits each input line into fields, Compares input line/fields to pattern ,Performs action(s) on matched lines

  • sudo--> to run any command with root previlages

  • |--> It’s called pipe, used to take one command output and send the output to next command as input

  • &&--> to write multiple commands which all will run together but will give separate output/work

  • restart/init 6 -> to restart the host

File System Hierarchy

root = admin

/ -> Top level root directory and below are sub directories e.x. C:\ drive in win

  1. /root - It is home directory for root user

  2. /home - Home directory for other user

  3. /boot - It’s contains bootable files for linux e.g. initrd - POST - Power on self test

  4. /etc - It contains all the configuration files - e.x. Machine hardware configuration file

  5. /usr - by default software are installed in this directory

  6. /bin (binary) - It contains commands used by all users including root user

  7. /sbin (system binary) - It contains commands used by only root user

  8. /opt - Optional Application software packages e.x. chrome

  9. /dev - Essential device file. Then include terminal devices, usb or any attached to the system - e.x. Printer

How to create a file

  1. cat - cat > f1 - ctrl+d to exit file

  2. touch - touch f1 f2 f3 f4 - create empty file

  3. vi/vim

  4. nano

Cat Command

  1. Create new file - create a single file

  2. Concatenate file - To add more than one file into single file

  3. Copy file - To copy the content of x into y

  4. tac (reverse of cat) - to see the content in bottom to top

E.x.

  1. cat > f1 how are you - > greater then means redirect output

  2. cat f1 - see the output of file

  3. cat > f2 i am good

  4. cat >> f1 thank you - >> two time greater than means add new content with existing f1 file content

  5. cat f1 f2 > f3 -> merge file with f1+f2 and create new file f3 = f3

Touch Command

  1. Main uses - Update timestamp

  2. Create an empty file

  3. Create multiple empty file

  4. Change all timestamp of a file

  5. Update only access time of file, modify time of file

E.x.

  1. stat f1 - all the file details are displayed with access time, modify time and update time

  2. touch f1 - create a empty file

  3. touch f2 f3 f4 - create multiple empty file

  4. touch f2 - change timestamp with current timestamp

  5. stat f2 - check the timestamp details

  6. touch -a f3 - only access time change

  7. touch -m f4 - only modified time change

  8. head f1 - top 10 file line

  9. tail f1 - last 10 file line

  10. more f1 - pages - more allows us to view them as a single file separated by lines

  11. less f1 - one screen at a time - less allows us to switch between them

Time Stamp

  1. Access time - Last time when a file was accessed - e.x. touch -a

  2. Change time - Last time when a file metadata (Property) was changed - e.x.

  3. Update time - Last time when a file was modified - e.x. touch -m

VI - Editor command

  1. Programmer text editor

  2. It can be used to edit all kinds of plain text.

E.x.

  1. vi f5 -> create a file

  2. i - insert to write data

  3. Esc

  4. :wq + enter (W= Save, q = Quit)

  5. :w -> Save file

  6. :wq or :x -> save and quit

  7. :q -> quit

  8. :q! -> force quit, no save

Nano Command

  1. nano f6 -> create file and write data

  2. ctrl+x -> exit

  3. Shift+y -> save data and Press enter to exit

User Command

  1. sudo su -> make a root user - sudo means super user do and su = switch user

  2. $ - normal user sign

  3. # - Root user sign

File Command

How to create hidden file

  1. touch .file1

  2. ls -a -> show hidden file and folder

How to Copy file

  1. cp source destination - e.x. cp f1 f2

How to cut and paste file

  1. mv source destination - e.x. mv f1 dir1

How to rename file and directory name

  1. mv old_name new_name - e.x. mv file1 myfile1

Remove - Delete Command

  1. rmdir : this command is used to remove directory (working only if empty)

    - e.x rmdir dir1

  2. rmdir -p : Remove both parent and child directory (p-parent )

    - e.x. rmdir -p dir1/dir2

  3. rmdir -pv : Remove all the parent and subdirectory along with verbose

  4. rm -rf : Remove even non-empty file and directory

  5. rm -rp : Remove non-empty directory including parent directory

  6. rm -r : Remove empty directory

Basic Linux Commands

  1. To view what’s written in a file.

Ans: We can do that by simply using ‘cat’ command :

cat file_name.extension

2. To change the access permissions of files.

Ans: For this , command is ‘chmod’ followed by permissions and file name. Here we have first checked which permission is currently present by doing “ll” or “ls -l” .

Now as we can see, execution permission is not present for the specific file, we have given full (r-w-x) permission to both user and user group later on. You can check this link to know more about chmod command and permissions.

chmod command to give permission

  1. To check which commands you have run till now.

Ans: We have to run ‘history’ command to check previous commands. Here as I am having nearly 200 commands , I have sorted with last 10 commands using history $n(specific number, for me it’s 10).

history 10 to get last 10 commands

4. To remove a directory/ Folder.

Ans: We have to use rm in case of file; rm –rf in case of directory. Though you can run rm -rf for file also, but it’s not recommended to use this command in production as it will delete everything silently, ignoring all interrupt.

Note: you can’t delete a directory using rm command. You will get error. You have to use rm -rf , so it will also delete all subfolder/file inside that directory. BE CAREFUL.

5. To create a fruits.txt file and to view the content. Add content in devops.txt (One in each line) — Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

Ans: You can use vi or vim or nano text editor as per your OS flavor. Here I have used vim.

For VI/VIM :- type vim filename>press “i” to go to insert mode> add your content> press ESC> type “:”> type {wq or wq!} if you want to save your work and quit else {q!} to quit without saving.

For nano:- type nano filename>add your content>press ctrl+x > Capital Y(for yes to save) > ENTER.

Now you can use cat command to see the content!

Checking file content after editing with text editor

6. To Show only top three fruits from the file.

Ans: “head” [space] -number’ (of items you want to see) [space] ‘filename.txt’ , this command will help you to achieve your result.

7. To Show only bottom three fruits from the file.

Ans: As you remember, we have used “head” to see top items. To see from bottom, we do use “tail” command. Syntax is same as “head” command.

8. To create another file Colors.txt and to view the content. Add content in Colors.txt (One in each line) — Red, Pink, White, Black, Blue, Orange, Purple, Grey.

Ans: Refer number-5. Same process followed.

9. To find the difference between fruits.txt and Colors.txt file.

Ans: We use “diff” command to know difference between multiple files. Syntax is simple ; diff ‘file1.txt’ file2.txt’

What is Kernel?

The Linux® kernel is the main component of a Linux operating system (OS) and is the core interface between a computer’s hardware and its processes. It exists within the OS and controls all the major functions of the hardware, whether it’s a phone, laptop, server, or any other kind of computer.

What is Shell?

A shell is a special user program that provides an interface for the user to use operating system services. Shell accepts human-readable commands from users and converts them into something which the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or starts the terminal.

What is Linux Shell Scripting for DevOps?

Shell Scripting is a program to write a series of commands for the shell to execute. Shell is a command-line interpreter and Shell script is nothing but a list of commands executed by the shell. It can combine lengthy and repetitive sequences of commands into a single and simple script that can be stored and executed anytime which, reduces programming efforts.

What is #!/bin/bash?

#!/bin/bash is a shebang line used in script files to set bash, present in the ‘/bin’ directory, as the default shell for executing commands present in the file. “#!” is a shebang in the character sequence consisting of the character’s number sign and exclamation mark ( #!) at the beginning of a script. We must specify this at the beginning of any shell script to let the script know, under which shell it’s being run.

Can we write #!/bin/sh as well?

#!/bin/sh: It is used to execute the file using sh, which is a Bourne shell, or a compatible shell.

Just like that, we have #!/bin/csh. It is used to execute the file using csh, the C shell, or a compatible shell.

-: Basic Linux Commands :-

  • Write a Shell Script which prints

  • Write a Shell Script to take user input, input from arguments, and print the variables.

See the output difference of both the commands.
1st one output is showing in next line, whereas in 2nd one,output is showing in
same line itself.

Also for variables, in 2nd example , we took input and variable in same line.

  • Write an Example of If else in Shell Scripting by comparing two numbers.

Linux daily uses command:

  1. sudo su - make current user to root user

  2. hostname, hostname -i : current working machine ip

  3. ifconfig

  4. cat /etc/os-release

  5. yum install httpd (yum - package - yellowdog updater modified - by default installed)

  6. yum remove httpd (httpd = apache software, d =daemon)

  7. yum update httpd

  8. service httpd start

  9. service httpd status

  10. chkconfig httpd on - (When you open laptop automatically httpd started in backend)

  11. chkconfig httd off

  12. which - find location or find software - e.x. which tree

  13. Whoami - Who are you?

  14. echo - Print Message - also use to create a file [echo ‘welcome’ >> filez, if you write command ‘echo > filez’ then filez lost the existing data and empty the file

  15. yum list installed - list all the installed packages or software in your system

  16. grep : Search the data - e.x. grep root /etc/passwd

  17. pwd - print working directory

  18. sort - sort by alphabetical order in file name - e.x. cmd = sort file1

  19. Ctrl + l -> Clear window terminal shortcut

  20. tree : tree command get output in drawing format with directory and folder structure same as ls command

  21. useradd - To create a user - e.x. useradd sag - check with cat /etc/passwd

  22. groupadd - To create a group - e.x. groupadd DevopsAWS - check with cat /etc/group

  23. gpasswd -a/-M : To add user into group, to add multiple user, -a = single user

    1. gpasswd -a sag DevopsAWS

    2. gpasswd -m sag,raj,kal,vijay DevopsAWS (first create a user then after use this cmd)

  24. ln : hard link - backup - automatically take backup for current folder/file

    1. - e.x. ln file2 backupfile2

- ls -> show the output of above command

  1. ln -s : soft link - shortcut key/icon - e.x. ln -s file1 softfile(you can write any name)

  2. tar : Tar is archiver used to combine multiple files into one

    1. tar -cvf dirx.tar dirx : c=create, v=verbose, f=forcefully
  3. gzip : Gzip is a compression tool used to reduce the size of a file

    1. gzip dir.tar
  4. gunzip : e.x gunzip dirx.tar.gz and tar -xvf dirx.tar : unzip and abstract folder

  5. wget : Wget is the non-interactive network downloader : e.x. wget <url>

Access Mode/Permission Command

Access Mode

File 

Directory

r

4

To Display the content

To list the content

w

2

To Modify

To create or remove

x

1

To execute the file

To enter into directory

E.x. output : drwxr-wr-- 1 root root 16 Nov 2 09:57 f1

d (directory or file ‘-’ ) 

rwx

rw (Group)

r _ 

1

root

root

16

  • rwx = 4+2+1 = 7

  • r_w=4+0+1 = 5

  • r_ _ = 4+0+0= 4

Command

rwx rwx rwx = 7 7 7

  1. chmod 777 dirx : full permission of dir

E.x. If you change the permission to file the : d r - x wx r _ then command is chmod 5 3 4 dir1

  1. chmod : change the access mode of file/dir

  2. chown : Change the owner of file or dir : chown sag f1 (Replace ‘root’ with ‘sag’ name)

  3. chgrp : change the group of file or dir : chgrp devops f1

Alphabetic ways

owner= u

Group = g

Other = o

  • \= same permission

  • + add permission

  • - remove permission

E.x. r_ rwx wx if apply this permission then use below command

  • u=r, g=rwx, o=wx file1

Added write permission to owner and not disturb other for below example

E.x. Cmd = chmod u+w f6 (Added permission)

chmod u-w f6 (Remove Permission)

chmod u=x f6 (Replace permission with write to execute)

chmod u+w,g-x f6 (Multiple permission)

Hope this article with help you to start your linux journey !

Thanks for reading !

- Sagar Radadiya