0% found this document useful (0 votes)
13 views80 pages

Linux Basics: Commands and File System

The document provides an introduction to Linux, covering its history, basic commands, file system structure, and package management. It explains the significance of Linux distributions and their components, as well as essential commands for navigating and managing files. Additionally, it highlights the importance of Linux in various applications, including embedded systems and servers.

Uploaded by

Yogesh Mungase
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views80 pages

Linux Basics: Commands and File System

The document provides an introduction to Linux, covering its history, basic commands, file system structure, and package management. It explains the significance of Linux distributions and their components, as well as essential commands for navigating and managing files. Additionally, it highlights the importance of Linux in various applications, including embedded systems and servers.

Uploaded by

Yogesh Mungase
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DevOps – Linux Introduction

And Commands
Learn some basic commands
Topics

▪ What is Linux
▪ Brief History
▪ File System
▪ Basic Commands
▪ Package Management
▪ Finding files
▪ Editing files
▪ Security
▪ Shell Scripting

2
Rules of this session

Be Excited Be Open to Learn! Carry Notebook & Pencil

Apply
Avoid Distractions Maintain Learning
Learnings
Flow

3
Brief History
▪ Mid-1960s Multics – MF Multitasking OS – Bell Labs, MIT, GE
▪ 1970s(1972) – Bell Labs – Ken Thompson and Dennis Ritchie ported Mimix in C – called Unix
▪ This increased portability across hardware platforms
▪ BSD (Berkeley Software Distribution)– add on to Unix around 70’s(1977)
▪ HP-UX and IBM AIX as commercial offerings
▪ Richard Stallman – Started GNU movement (recursive acronym – GNU is Not Unix – Free
software movement
▪ Andrew S. Tenenbaum – Author of a book OS Design And Implementation
▪ He created a Unix Variant MINIX (1987)
▪ Linus Torvalds inspired by Tenenbaum book, created his own OS Kernel and made it open
for others to modify under GNU (1991)
▪ GNU + Kernel is released as full OS and is called Linux (1992)
▪ There are various distributions of Linux
▪ Linux is in smartphone, tablets, computers, servers, IoT, vehicles, routers, email servers
▪ Linux has around 2000 contributors from across 500 companies , every year. It is the largest
single software project in the history of mankind
▪ 10 patches a day, 7 days a week, 24 by 7

4
Linux Basics

▪ Linux is a multi-user and multi-tasking operating system available with


various distributions.
▪ It is suitable for servers, desktops, laptops, and handheld devices
▪ OS is all the programs that are required to operate a computer and monitor
user programs
▪ Linux is made of 3 parts – The Kernel, The Shell, and the programs
▪ Kernel of an OS is a component that assists with hardware and inter-process
communication
▪ Device drivers are software components that directly talk to the hardware
and kernel of the OS

5
Unix

6
Linux Distributions
▪ Linux distributions do the hard work for us, taking all the code from the
open-source projects and compiling it, combining it into a single operating
system we can boot up and install.
▪ They also make choices such as choosing the default desktop environment,
browser, and other software.
▪ Most distributions add their unique things, such as themes and custom
software. E.g. the Unity desktop environment by Ubuntu

Applications
Device
libraries coreutils
Drivers
Kernal

7
8
9
Linux Distributions
▪ Linux Kernel
▪ Have own patches of Kernel to suit the target audience
▪ Core GNU utilities – bash shell, networking utilities, file system, and others
▪ Supplemental software – desktop env, games, and other productivity
applications,
▪ Desktop environments – runs on X Server to provide graphical desktop(GNOME,
KDE, Unity)
▪ System services – networking services, login, security services (e.g. systemd)
▪ Software management – how to install, remove, update
▪ Common one, Slackware, Debian, Redhat Linux, SUSE, Ubuntu(based on
Debian), Fedora desktop (own by Redhat), Open SUSE(desktop users), CentOS
(rebuild of Redhat without any enterprise support)

Extra Reading - [Link]

10
Linux Distribution

11
Linux in Embedded Systems

▪ Smart TVs
▪ Routers
▪ Home entertainment systems
▪ Machine control and industrial automation
▪ Spacecraft software
▪ Android is based on Linux
▪ Scientific applications (system monitoring)
▪ NASA – robotic system
▪ Medical Devices

12
Linux File System

Role of Linux file system


▪ Organization of data and easy-to-search content
▪ Persistence
▪ Data integrity
▪ Efficient retrieval of data

13
File System Types

•ext2 •msdos
•ext3 •ntfs
•ext4 •vfat
•fat •xfs

14
Linux File Hierarchy structure - root

▪ Linux FHS (Filesystem Hierarchy Standard) defines the standard structure of the
Unix/Linux file system (see [Link]
▪ Inverted tree (has a single root directory unlike Windows where you have
different drives)
▪ It is case sensitive
▪ Top is called root (denoted as “/”)
▪ Only root user has access to modify anything under most of these directories
▪ /root is the user directory for the root user
▪ Files preceded by . (dot) are hidden from normal view
▪ A single dot (.) denotes the current directory and a double dot (..) denotes a
parent directory

Use pwd, ls, ls –l, and to see the folder, file system
15
16
Linux File Hierarchy structure – other imp
directories
Directory Description of its significance and contents Examples
/bin/ Essential command binaries ls, cd, pwd
/boot/ Static files of the boot loader to boot computer Linux Kernel initrd,
vmlinux, grub files
/dev/ Where all the devices are referenced from HD, USB,
Keyboards, sound
cards
/etc/ (et cetera) Host-specific system configuration, and other /etc/[Link],
configuration files. (/etc/group, /etc/passwd) /etc/[Link]
/lib/ and Essential shared libraries & Kernel modules (drivers like [Link],
/lib64 video card, WiFi, printer etc) [Link].5.7
/media/ Mount point for removable media. Mainly system does it Floppy, cdrom,
cdrecorder, zip
/mnt/ Mount point for mounting a filesystem temporarily on
CDROM, external HDD. Not required nowadays (sysadm
access)
/opt/ Add-on application software packages /opt/bin – apps
17
/opt/lib libraries
Linux File Hierarchy structure – other imp
directories
Directory Description of its significance and contents Examples
/sbin/ Essential system binaries for sys admin For booting, restoring,
recovering
iptables, reboot, fdisk,
ifconfig, swapon
/srv/ Data for services provided by this system webservers
/tmp/ Temporary files
/usr/ (Unix System Resources) Secondary hierarchy,
read only data, sharable across FHS
(Filesystem Hierarchy Standard) host, multi-
user utilities and apps
/var/ Variable data files like logs, spools for tasks Spool directories, files, admin
e.g. for printer and logging data, tmp files
/home/ User files and folders /home/paratus, home/niche
/proc/ Info about running Linux system/processes Ps command uses data from
(procfs file this folder(file system)
system)
18
Linux Basic commands
Login

Exit

Help for commands

Creating Directory

Changing Directory

Listing files and directories

19
Login and Logout
▪ An available text terminal will prompt for a username (with the string login:) and
password. When typing your password, nothing is displayed on the terminal (not even a *
to indicate that you typed in something) to prevent others from seeing your password.
▪ Once your session is started you can also connect and log in to remote systems via
the Secure Shell (SSH) utility.
▪ For example, by typing ssh username@[Link]
▪ SSH would connect securely to the remote machine and give you a command line
terminal window, using passwords (as with regular logins) or ssh keys to prove your
identity

ssh -i 'c:\Users\abc\.ssh\[Link]' centos@[Link]

20
Rebooting and shutdown
shutdown command.
▪ This sends a warning message and then prevents further users from logging in.
The init process will then control shutting down or rebooting the system.
▪ The halt and poweroff commands issue shutdown -h to halt the
system; reboot issues shutdown -r and causes the machine to reboot instead of just
shutting down. Both rebooting and shutting down from the command line require
superuser (root) access.
▪ When administering a multiuser system, you have the option of notifying all users prior to
shutdown as in:
$ sudo shutdown -h 13:16 "Shutting down for scheduled
maintenance.“

▪ init 6 or init 0 – rebooting and shutting system respectively

21
Absolute and Relative Path

▪ Absolute path begins with /


▪ Example cd /etc/profile.d, cd /usr/share/doc
▪ Relative Path
▪ terraform/awsinfra/servers
▪ ../mak
▪ ../../etc/

22
Changing current directory

▪ cd <any directory inside doc without /)


▪ cd .. One directory up
▪ cd – goes to the last directory
▪ cd to go to the home directory (e.g. /home/centos)
▪ cd ~ also to go to the home directory

23
Creating, renaming and deleting directories

▪ mkdir – to create a new directory


▪ mkdir {abc,pqr,xyz} – create multiple directories
▪ mv – to move or rename a file/directory
▪ rm – to delete/remove a file
▪ mkdir –p <dirName>/<dirName> - creates every directory in the path
specified if it does not exist already
▪ rmdir – to remove a directory (only if it is empty)
▪ rm –rf <directory Name> – to remove a directory having some content (r is
recursive)

24
Copying files/directories

▪ cp – making a copy of a file


▪ cp –r <dir to be copied> <directory to copy to> – to copy a directory

25
Linux Command Line – keyboard shortcuts
▪ CTRL + a - to go to the first character of the command (i.e. beginning of the
line) from anywhere you are
▪ CTRL + e – end of line
▪ CTRL + k – deletes everything after the cursor including the character at the
cursor position
▪ CTRL + u – deletes everything before the cursor excluding the character at the
cursor position
▪ CTRL + l – clears everything on the screen except the line on which you are
currently typing commands
▪ CTRL + w – clears the last word before the cursor
▪ CTLR + t – switching the char at the cursor with the character before the cursor
▪ CTRL + f – to move forward one character at a time
▪ CTRL + b – to move backward one character at a time
▪ CTRL + d - to delete one character at a time where the cursor is pointing to
▪ CTRL + h – to delete one character before the cursor
▪ CTRL + p – brings up the last command executed on shell

26
Linux commands – Listing files

List content of the directories and their attributes


▪ ls –l (long listing)
▪ ls –lh (h – size in human readable format)
▪ ls –a ( files and folders including hidden files (starting with
dot)
▪ ls –R (lists directory recursively)
▪ ls –S (sort with largest first)
▪ ls –t (sort by modified, newest first)
▪ man ls (help on the ls command)

27
Linux commands – Where are they

How Linux executes the commands


▪ Which ls
▪ Whereis ls (additionally displays when in man it is located)
▪ Shell environment bash has variables that have many settings.
▪ These variables are called environment variables
▪ env – List all the environment variables
▪ $LOGNAME, $HOME,
▪ PATH – list of all the directories bash will look in for executing an application or a
command
▪ echo $PATH – use echo to list specific environment variables
▪ Execute a script in your home directory (Is it executing?)
▪ Execute using a full path ($ ./[Link])

28
Linux– Working with env variables

▪ export JAVA_HOME=“c:/program files/java/jdk1.8_212”


▪ echo $JAVA_HOME
▪ Appending the variable – PATH=$PATH:/opt
▪ unset JAVA_HOME

29
Linux commands – basic commands

▪ Whoami (lists the username who is currently logged in)


▪ Alternately you can use “echo $LOGNAME” env variable
▪ su - <user name> – substitute user/super user
▪ su - - for super user
▪ sudo –i – to login as the root user
▪ exit – leave the shell for the current user
▪ top –displays running processes in a system (real-time, use h key to see what
you can do with these commands)

30
Package Manager

31
Linux package management

32
Linux package management

▪ Method of installing, upgrading, configuring, and removing software and


services on Linux
▪ Different from the windows installation
▪ Concept of repositories
▪ Package consists of
▪ Dependency information for dependency on other software
▪ Version information
▪ Architecture information – 64 bit, 32 bit, processor information
▪ Two ways of installing
▪ Source code (tarballs) – compile
▪ Pre-build packages – RPM, and .deb

33
Red Hat Package Manager (RPM)

▪ *.rpm extension
▪ Used on RHEL, Fedora, CentOS, SUSE, OpenSUSE
▪ Tools for installing RPM packages
▪ YUM-Yellowdog Updater, Modified (command line) – RHEL, Fedora, and CentOS
▪ Zyper (Command Line) – SUSE, openSUSE
▪ GNOME(Graphical) – RHEL, Fedora, CentOS,
▪ YaST(graphical) – SUSE, openSUSE
▪ Updates/Upgrades of packages is also possible using these tools

34
Using Yum
▪ sudo yum search httpd
▪ sudo yum info httpd
▪ sudo yum install httpd
▪ sudo yum list installed httpd (list installed packages)
▪ sudo yum deplist httpd (dependency list)
▪ sudo yum remove httpd (does not remove the dependency)
▪ sudo yum autoremove httpd (removes dependencies that no longer require/are used
by other programs in the system)
▪ which httpd
▪ sudo yum repolist (links are in config file /etc/[Link].d)
▪ sudo yum check-update (check updates)
▪ sudo yum update (updates all software with latest versions from the online
repository)

Repeat above steps for installation of git

35
Linux commands – Command History and
completion
▪ Linux keeps a history of all the commands typed by the user
▪ Security/tracing
▪ Reusing the earlier commands for better productivity
▪ History is stored in a local directory in a hidden file “.bash_history”
▪ Using the UP key for the previous command
▪ HISTFILESIZE (env variable to decide how many commands to store)
▪ HISTCONTROL (modifies the history behavior e.g. ignore duplicate values)
▪ history command (!<history number>)
▪ TAB key for command completion
▪ Use CTRL + r to and then characters, it will search commands matching the characters
from history and display
▪ history –c – will clear the history
▪ Using ! And command start to get the last command starting with a string
▪ Using !<startingstringofcommand>:p displays the command instead of executing
▪ history | grep systemctl will give a list of commands executed containing systemctl

36
Globbing (* ? [ ])t
▪ It performs an action on more than one file having the same
pattern or to find part of a phrase in a text file.
▪ Globbing is an operation that recognizes the wildcard pattern and
expands it into its path name.
▪ Finding files and directories even if we do not know the exact
name
▪ ls *.txt - * represents 0 or more characters
▪ ls my*
▪ ls ?.txt, ls ???????.txt, param?.MD -? Represents a single
character
▪ ls [dD]*.sh – [ ] to match character within the square braces
▪ ls [Ee]quity[Rr]eport1[0-9]1018?1[0-9]1018.*

37
Quoting (" " and ' ')

Double quotes – contains string and any variables or commands within them
get evaluated or acted on
▪ echo "No of commands in history files are $HISTSIZE "
▪ echo "Value of Path environment variable is $PATH "
Try the same command with single quotes. It will preserve the text as
is without interpreting the env variable
▪ echo ' No of commands in history files are $HISTSIZE '
▪ echo ' Value of Path environment variable is $PATH '

38
Example

▪ str1='echo $USER’
▪ echo "$str1“

▪ str1=“echo $USER”
▪ echo "$str1“

▪ str1=`echo $USER`
▪ echo $str3

▪ str3=$(echo $USER)
▪ echo "$str3"

39
Quoting (\-backslash)

\ backslash – escape character, disables any special character functionality that


immediately follows it
▪ echo " You owe me $300 " – check the output
▪ Now try - echo " You own me \$300 "
It helps escape the space in a folder
▪ ls great content – will give an error because of space
▪ ls great\ content – will work well
▪ ls " great content " – fine
▪ ls ' great content ' – fine
Also, long commands can be broken using \
▪ ls \
▪ >-l

40
Viewing files – less command

less – viewing a text file without opening it, allows scrolling


▪ Shows the starting part of a file
▪ Up and Down arrow keys to navigate
▪ Also Page Up and Page Down for faster navigation
▪ Search with / and? For a particular word
▪ To quit press the “q” key
▪ Syntax – less <filename>

41
Viewing files – head and tail

head – shows the first 10 lines of a file. Helps to look at log files
for example
▪ head <filename>
▪ head –n 25 <filename> - will display first 25 lines of a file
tail – It is the opposite of the head, and shows the last 10 lines
▪ tail <filename>
▪ tail –n 25 <filename> - last 25 lines
▪ tail –f <filename> - shows the last part of the file in real-time
as the data is written to it.
▪ sudo tail –f /var/log/messages – try login and logout from another
terminal and see the effect

42
Text Analysis – redirection, cut
Notation Meaning Example
> Redirects to a file echo “Hello” > [Link]
>> Redirects & appends echo “Hello” >> [Link]
to a file
< Redirects file as input sort < [Link]
Input and output redirection
to a command
▪ Stdout – standard output – standardized stream of data that is
produced by command line programs
▪ By default bash tracks this and displays it on the screen
▪ We can redirect this information using >
▪ head –n 1 [Link] > [Link]
▪ head –n 1 [Link] >> [Link]
▪ sort < [Link] > [Link]
Cut command removes fields from the file and prints on the screen. –d –
delimiter and –f – which field to print
▪ cut -d" " -f 3- [Link] - here delimiter is space
43
Text Analysis – sort and wc

Sorting file content based on the first character of each line


▪ sort <filename>
Number of lines and word count in files
▪ wc -lw [Link] [Link]
[Link] [Link]

44
Pipes, grep and regular expression

grep (global regular expression print) – outputs the lines in a file that match
the pattern specified
▪ -i – case-insensitive search
▪ -v – gets lines without the pattern (reverse of the pattern)
▪ -r – recursive search
▪ grep DevOps [Link]
▪ grep –i DevOps [Link]
| - pipe character sends the output of one command as input to the other
command
▪ grep -i devops [Link] | less
▪ grep -oi devops [Link] | wc –w (how many time word devops appears
in output)
45
Regular expression
Regular Expressions
^ - beginning of a line
$ - end of a line
. (dot) – single character
[abc] – search for a specified character
[^abc] – search other than these
* - matches zero or more characters preceding this expression
▪ grep -i '^devops' [Link] (lines beginning with devops)
▪ grep -i 'work.$' [Link] (ending with work.)
▪ grep '^[Dd]ev[Oo]ps' [Link]
▪ grep '^[^A]' [Link] (lines not starting with A)
▪ grep '^.[e]' [Link] (lines having the second letter of the first word as ‘e’)

46
Working in vim – class assignments
▪ Install vim “sudo yum install vim”
▪ i – insert mode and ESC to come back to command mode
▪ F1 for help
▪ A (SHIFT+a) – append text at the end of the line
▪ dw – delete word under the cursor
▪ dd – delete the whole line under cursor
▪ :w – saves a file
▪ :q! – exits without saving
▪ U – for undoing the last change
▪ X – delete the current character
▪ Shitf + g – to go to the bottom of the file.
▪ gg – to go to the top of the file
▪ Search and replace :%s/Chef/Puppet/g – g means all the copies in the current
file
▪ :noh to remove the highlight of a search word
▪ Visual mode – v and SHIFT+V (y – copy(yank), p-paste d-cut/delete)
▪ vimtutor to learn at your own speed (CTRL Z to come out)
▪ Set (Shift : - se paste or nu or ic etc.)

47
Working in vim – Home Assignments
Key Usage

arrow keys To move up, down, left and right


j or <ret> To move one line down
k To move one line up
h or Backspace To move one character left
l or Space To move one character right
0 To move to beginning of line
$ To move to end of line
w To move to beginning of next word
:0 or 1G To move to beginning of file
:n or nG To move to line n
:$ or G To move to last line in file
CTRL-F or Page Down To move forward one page
CTRL-B or Page Up To move backward one page
^l To refresh and center screen

48
Working in vim – Searching - Home
Assignments
Command Usage
/pattern Search forward for pattern
?pattern Search backward for pattern

Key Usage
n Move to next occurrence of search pattern
N Move to previous occurrence of search
pattern

49
Working in vim – Keystrokes - Home
Assignments
Key Usage
a Append text after cursor; stop upon Escape key
A Append text at end of current line; stop upon Escape key
i Insert text before cursor; stop upon Escape key
I Insert text at beginning of current line; stop upon Escape key
o Start a new line below current line, insert text there; stop upon Escape
key
O Start a new line above current line, insert text there; stop upon Escape
key
r Replace character at current position
R Replace text starting with current position; stop upon Escape key
x Delete character at current position

50
Working in vim – Keystrokes - Home
Assignments

Key Usage
Nx Delete N characters, starting at current position
dw Delete the word at the current position
D Delete the rest of the current line
dd Delete the current line
Ndd or dNd Delete N lines
u Undo the previous operation
yy Yank (copy) the current line and put it in buffer
Nyy or yNy Yank (copy) N lines and put it in buffer
p Paste at the current position the yanked line or lines from the buffer.

51
Linux commands – Creating alias

▪ alias l=“ls –la”


▪ alias vlog=“sudo tail –f /var/log/messages” (short form for var log )
▪ “alias vlog” to see what it is mapped to
▪ These aliases will work only for this instance of login
▪ Adding these aliases in .bashrc or .bash_profile file in your user directory will make it permanent
▪ .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells
▪ You can also declare a variable and assign a value that can be used in a bash script or from the
command line.
▪ export <variable Name>=“<variable Value>”
▪ export DBPATH=“/user/cloud_user/db” (have this in your .bashrc to preserve it for multiple sessions)
▪ Removing an alias use the “unalias vlog” command
▪ Check /etc/profile.d folder for the default aliases set in .sh files

52
Linux commands – Changing Command
Prompt
▪ PS1 env variable controls how the command prompt will be.
▪ Default is something like this
▪ [cloud_user@ketanvj1c ~]$, if you see the PS1 env variable value for this prompt it is
‘[\u@\h \W]\$’
▪ \u – user name
▪ \h – hostname
▪ \W – last folder of the current working directory
▪ There are others you can use
▪ \t – time in hr:min:sec format
▪ \d – date
▪ \H – FQDM
▪ \j – no of jobs currently running
▪ Try changing PS1 with this PS - PROMPT STATEMENT
▪ export PS1='[\u@\H \W]\$'

53
Security and Permissions – User Types
▪ Who is logged into the system
▪ id command
UID – user ID
GID – Group ID
groups – secondary group
wheel – special group – regular accounts granted root access
▪ sudo command – executes a command as super user
▪ sudoers file containing whom all are granted permission as sudo
User account details (sudo visudo)
▪ /etc/passwd – one entry for each user
▪ User, password or not, userID, groupID comments, users home directory, and
users default login shell
▪ /etc/group – one entry for each group

54
Security and Permissions - /etc/passwd

User Name User ID Default login shell


Full Name

Password info Primary Group ID


Home Dir

Three types of users in Linux – root user, regular user (centos), system users (lower IDs)

Typically a system user will have a lower user id, and will not have a home directory
and will not have a login shell
55
First character in the file access

ls -ld /etc
drwxr-xr-x 170 root root 12288 2010-02-24 11:39 /etc
ls -l /dev/sda
brw-rw---- 1 root disk 8, 0 2010-02-24 11:32 /dev/sda
1. – Regular file.
2. b Block special file (stored in /dev).
3. c Character special file (stored in /dev).
4. d Directory.
5. l Symbolic link.
6. p FIFO.
7. s Socket.
8. w Whiteout.

56
Security and Permissions - /etc/group
Group Name Users who are members of this group
Group ID

Password for group

57
Creating Users and Groups (root permission)
▪ Creating a group
▪ sudo groupadd devops
▪ sudo useradd –G 1003 -m –c “Makarand KotaK” mak
▪ -G – creates under secondary group 1003 group id
▪ -m – creates a home directory
▪ -c – to add full user name
▪ Check with “id mak”
▪ Entry in /etc/group
▪ Check ls /home to check the home directory got created
▪ Set the password for mak by
▪ Sudo passwd mak
▪ Set password as mak123
▪ /etc/shadow file contains details about the password
▪ Login using “su – mak” and type the password
▪ To come out of this user and go back to centos, type “exit” and ENTER

58
Permissions
▪ In Linux permissions are of three categories
▪ User (u)
▪ Group (g)
▪ Other/world (o)
▪ All (a)
▪ Symbolic Permissions
▪ r – read
▪ w – write
▪ x – execute
▪ “-” - no permission
▪ Octal Permissions
▪ 4 - read
▪ 2 - write
▪ 1 - execute
▪ 0 - no permission

[Link] (775) – [Link] (664) –


User - rwx means 4 + 2 + 1 = 7 User – rw- means 4 + 2 + 0 = 6
Group – rwx again 7 and Group – rw- again 6 and
Others – r-x 4 + 0 + 1 = 5 Others – r-- 4 + 0 + 0 = 4
59
Changing ownership and permissions
▪ chmod command
▪ chmod u=rwx,g=rx,o=r [Link]
▪ chmod o-r [Link]
▪ chmod –R o-r <directoty Name>/*
▪ chmod 600 [Link] (6 for user, 0 for group and 0 for others)
▪ chmod ug+rw,o-x [Link]
▪ chmod ug=rx,o+r [Link]
▪ chmod 777 [Link] (same as chmod ugo+rwx [Link])
▪ chmod 435 [Link] (same as chmod u=r,g=wx,o=rx [Link])
▪ chmod a+rw [Link]
▪ chown command
▪ chown <owner>: <group> <filename>
▪ chown <owner> <filename>
▪ chown mak:devops [Link]
▪ chgrp <new group name> <filename>
60
Changing ownership and permissions
▪ File permission examples
Setting Numerical Meaning

-rw------- (600) Only the owner has read and write permissions.

-rw-r--r-- (644) Only the owner has read and write permissions; the group and others have read-only.

-rwx------ (700) Only the owner has read, write, and execute permissions.

-rwxr-xr-x (755) The owner has read, write, and execute permissions; the group and others have only read and execute.

-rwx--x--x (711) The owner has read, write, and execute permissions; the group and others have only execute.

-rw-rw-rw- (666) Everyone can read and write to the file. (Be careful with these permissions.)

-rwxrwxrwx (777) Everyone can read, write, and execute. (Again, this permissions setting can be hazardous.)

▪ Directory permissions examples


Setting Numerical Meaning
drwx------ (700) Only the user can read, and write in this directory.
drwxr-xr-x (755) Everyone can read the directory; users and groups have read and execute permissions.

61
Finding Files and Folders - find
Find – searches the file system for the items we provide as arguments (slower as
directly scans the file structure)
▪ Current directory – find . –name ‘[Link]’
▪ Specific directory - find HummTraining/ -name '*.txt’
▪ Ignoring case – find . –iname ‘[Link]’
▪ Find empty files – find . –empty, find . –type f –empty, find . –type d -empty
▪ Find file with specific permission – find . –perm 664
▪ Searching text within multiple files – find . -type f -name '*.txt' -exec grep
'sdasl' {} \;
▪ Find directories using name – find . –type d –name HummTraining
▪ Find directories with 777 perm and change to 755 – find . –type d -perm 777 –print –exec
chmod 755 {} \;
▪ Find and remove file – find . –type f –name “[Link]” –exec rm –f {} \;
▪ Find files with a specific owner – find . –user root
▪ Find files with a specific group – find . –group root

62
Finding Files and Folders - find
▪ Find files modified X days back – find . –mtime 8
▪ Find files accessed x days back – find . –atime 9
▪ Find files modified between x days and y days – find . –mtime +5 –
mtime -10
▪ Find changed files in last x minutes – find . –cmin -180
▪ Find files modified in last X minutes – find . -mmin -30
▪ Find files of size x – find . –size 40M
▪ Find files between 1 and 2 MB – find . –size +1M –size -2M
▪ Find and delete 20M files – find . –size +20M exec rm –rf {} \;

63
What is SSH

▪ SSH - Secure Socket Shell /Secure SHell


▪ a network protocol that provides administrators with a secure way to
access a remote computer
▪ Encrypted data communication
▪ Use of public and private keys for authentication
▪ Helps control a remote computer over a network
▪ [Link]
with-ssh-servers-clients-and-keys

64
SSH Connection

▪ Create new user on AWS box


sudo groupadd testers
sudo useradd -G testers -m -c "John Kotak" john
sudo passwd john
sudo su – john
mkdir –p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
Chmod 600 ~/.ssh/authorized_keys
▪ Window’s box run below command
ssh-keygen
Copy files id_rsa.pub data to ~/.ssh/authorized_keys location of aws box of john’s home directory
▪ Run below command from Window’s box
ssh john@awsbox

65
Hoe SSH works?
1. Generate ssh keys in the client machine
2. Copy the public key on the remote server, in ~/.ssh/authorized_keys file
3. Client informs server to connect with a specific user by “ssh user@IP of the server”.
4. As part of the above command, the client tells the server which public key to use.
5. Server checks the “authorized_keys” file for the public key.
6. Server then generates the random string and encrypts using the public key.
7. This encrypted message requires a private key to decrypt
8. Server sends this message to the client
9. Client will decrypt using the private key and combine the random string with a previously
negotiated session ID.
10. Client generates MD5 hash of this value and transmits to the server
11. Server using Session ID and original message compares the MD5 hash and verifies the
validity
12. Server grants access to client

66
Questions?

67
[Link]

Frequently used commands


▪ ps aux |grep http
▪ yum list installed | grep http
▪ systemctl list-unit-files | grep httpd (check whether httpd is enabled to start on
boot, can be used to check for any other service also)
▪ systemctl status httpd (status of the server)
▪ Create multiple directory – mkdir {dir1,dir2,dir3}
▪ Create the same set of files inside this directory – touch
{dir1,dir2,dir3}/{[Link],[Link]}
▪ See grep search before and after 10 lines
▪ cat [Link] | grep -A 10 -B 10 'email’
▪ yum whatprovides mail
▪ sudo netstat –tulpn (which apps are running on which port)
▪ sudo netstat --tcp --udp --listening --program (to get PID program names)
▪ sudo netstat -plnt | fgrep <port details you want>

68
3 ways to run multiple commands on single
line
1) Use ;
No matter the first command cmd1 run successfully or not, always run the second command cmd2:
# cmd1; cmd2
$ cd myfolder; ls # no matter cd to myfolder successfully, run ls
2) Use &&
Only when the first command cmd1 run successfully, run the second command cmd2:
# cmd1 && cmd2
$ cd myfolder && ls # run ls only after cd to myfolder
3) Use ||
Only when the first command cmd1 failed to run, run the second command cmd2:
# cmd1 || cmd2
$ cd myfolder || ls # if failed cd to myfolder, `ls` will run

69
Cron job in linux

▪ Cron service is active - systemctl status [Link]


▪ crontab –e to create cron job
▪ crontab –l to list the cron jobs for a user
▪ crontab –r to delete the cronjob
▪ E.g * * * * * /usr/bin/echo "Hi“ – to run echo command every minute. Check
the messages in /var/mail/<username> file
Field Description Allowed Value
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6
Any command to be
CMD Command
executed.
70
Startup Files
In Linux one or more startup files are used to configure the environment.
Files in the /etc directory define global settings for all users while Initialization files in the user's
home directory can include and/or override the global settings
▪ The startup files can do anything the user would like to do in every command shell, such as:
• Customizing the user's prompt
• Defining command-line shortcuts and aliases
• Setting the default text editor (export EDITOR=‘vi’)
• Setting the path for where to find executable programs

71
Startup Files – Order of Execution
When you first login to Linux, /etc/profile is read and evaluated, after which the following
files are searched (if they exist) in the listed order:
▪ ~/.bash_profile
▪ ~/.bash_login
▪ ~/.profile

The Linux login shell evaluates whatever startup file that it comes across first and ignores the rest.
This means that if it finds ~/.bash_profile, it gnores ~/.bash_login and ~/.profile
(Varies across different distributions)

72
Startup Files – Order of Execution
▪ Every time you create a new shell, or terminal window, etc., you do not perform a full system
login; only the ~/.bashrc file is read and evaluated.
▪ This file is not read and evaluated along with the login shell, most distributions and/or users
include the ~/.bashrc file from within one of the three user-owned startup [Link] must
make appropriate changes in the ~/.bash_profile file to include the ~/.bashrc file
▪ The .bash_profile will have certain extra lines, which in turn will collect the required
customization parameters from .bashrc.

73
Startup Files – Order of Execution
▪ Every time you create a new shell, or terminal window, etc., you do not perform a full system
login; only the ~/.bashrc file is read and evaluated.
▪ This file is not read and evaluated along with the login shell, most distributions and/or users
include the ~/.bashrc file from within one of the three user-owned startup [Link] must
make appropriate changes in the ~/.bash_profile file to include the ~/.bashrc file
▪ The .bash_profile will have certain extra lines, which in turn will collect the required
customization parameters from .bashrc.

74
Finding Files and Folders - locate

Locate – searches a local database (mlocate) of files and folders for


items we provide as arguments (db location -
/var/lib/mlocate/[Link])
(you need to install mlocate by “sudo yum install mlocate” then
execute “sudo updatedb” and then try the following commands
▪ locate [Link]
▪ locate “*.txt” –n 20 (limits to 20 search items)
▪ locate –c “*.java” (number of files with extension .java)
▪ locate –i “[Link]”
▪ sudo updatedb to update the database
▪ locate –S (locate DB statistics)

75
awk utility
▪ Text processor to manipulate text data which are in specific format, like tabular
▪ Operates in line by line basis and iterates through entire file
▪ White spaces (space, tabs) as separators by default
▪ Basic Format
awk '/search_pattern/ { action_to_take_on_matches; another_action; }' file_to_parse
▪ Cat equivalent
awk '{print}' favorite_animal.txt
▪ Searching for a specific text
awk ‘/zebra/' favorite_animal.txt
▪ Starting with
awk ‘/^zeb/' favorite_animal.txt
▪ Printing only specific column
awk ‘/^zebr/ {print $1;}' favorite_animal.txt
▪ Optional BEGIN and END
awk 'BEGIN { action; }
/search/ { action; }
END { action; }’ favorite_animal.txt

76
awk utility – Internal variables
To assign certain pieces of information as it processes a file
▪ FILENAME: References the current input file
▪ FNR: References the number of the current record relative to the current input file. For
instance, if you have two input files, this would tell you the record number of each file instead
of as a total
▪ FS: The current field separator used to denote each field in a record. By default, this is set to
whitespace
▪ NF: The number of fields in the current record
▪ NR: The number of the current record
▪ OFS: The field separator for the outputted data. By default, whitespace.
▪ ORS: The record separator for the outputted data. By default, this is a newline character.
▪ RS: The record separator used to distinguish separate records in the input file. By default, this
is a newline character.

77
awk utility – Internal variables
• To print out the first column of this file, execute the following command
awk 'BEGIN { FS=":"; }
{ print $1; }' /etc/passwd
• BEGIN and END to print information
awk 'BEGIN { FS=":"; print "User\t\tUID\t\tGID\t\tHome\t\tShell\n----
----------"; }
{print $1,"\t\t",$3,"\t\t",$4,"\t\t",$6,"\t\t",$7;}
END { print "---------\nFile Complete" }' /etc/passwd
▪ Match the beginning of the second column
awk '$2 ~ /^sa/' favorite_animals.txt
▪ Extraction (use ifconfig command to check what is available)
ip a s docker0 | awk -F '[\/ ]+' '/inet / {print $3}’
▪ See notes section for the explanation

78
Sed – Stream editor
▪ performs editing operations on text coming from standard input or a file
▪ sed edits line-by-line and in a non-interactive way
▪ Powerful and fast way to transform text
▪ Can be used inside a script
▪ Does not change the original file unless given –i option (avoid instead redirect)
▪ Sending output of one command to sed for processing
• Basic syntax
sed [options] commands [file-to-edit]
▪ Cat equivalent
sed ‘’ [Link] or
cat [Link] | sed ‘’
▪ Print command using ‘p’ (will print twice as sed also prints and ‘p’ prints it again)
sed 'p’ [Link]
sed –n 'p’ [Link] (-n suppresses the default printing)
sed –n ‘1,5p’ [Link] (printing first 5 line
sed -n '1~2p’ [Link] (print every other line)
sed '1~2d’ [Link] (deleting every other line)

79
Sed – Stream editor
▪ Substituting text
echo "[Link] | sed
's_com/index_org/home_’
Replaces com/index to org/home.
▪ Search and replace in a file
sed 's/on/forward/g’ [Link]

80

You might also like