0% found this document useful (0 votes)
3 views44 pages

Linux

The document provides an extensive overview of Linux, covering its advantages, architecture, popular distributions, and package management systems. It details basic commands, file systems, user and group management, file permissions, and software installation processes for both CentOS and Ubuntu. Additionally, it includes command examples and explanations for various functionalities within the Linux operating system.

Uploaded by

Harry Dhillon
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)
3 views44 pages

Linux

The document provides an extensive overview of Linux, covering its advantages, architecture, popular distributions, and package management systems. It details basic commands, file systems, user and group management, file permissions, and software installation processes for both CentOS and Ubuntu. Additionally, it includes command examples and explanations for various functionalities within the Linux operating system.

Uploaded by

Harry Dhillon
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
You are on page 1/ 44

Introduction

Why Linux?
• Open source
• Community support
• Heavily customizable
• Most servers run on Linux
• Most of the DevOps tools implements on Linux only
• Automation
• Secure

Architecture of Linux
Different Linux Distros
Popular Desktop Linux OS
• Ubuntu Linux
• Linux Mint
• Arch Linux
• Fedora
• Debian
• openSUSE

Popular Server Linux OS


• Red Hat Enterprise Linux (RHEL)
• Ubuntu Server
• CentOS
• SUSE Enterprise Linux

Most used Linux distros currently in IT industry


• RPM based - Red Hat Enterprise Linux (RHEL) & CentOS
• Debian based - Ubuntu Server

Difference between RPM based and Debian based


From user’s point of view, there isn’t much difference in these
tools. The RPM and DEB formats are both just archive files, with
some metadata attached to them. They are both equally arcane,
have hardcoded install paths and only differ in subtle details. DEB
files are installation files for Debian based distributions. RPM files
are installation files for Red Hat based distributions.
Ubuntu is based on Debian’s package manage based on APT and
DPKG. Red Hat, CentOS and Fedora are based on the old Red Hat
Linux package management system, RPM.

DEB or .deb (Debian based software’s)


DEB is the extension of the Debian software package format and
the most often used name for such binary packages. DEB was
developed by Bedian.

Example: Google chrome software


Package name: google-chrome-stable_current_amd64.deb
.deb Installation: dpkg -i google-chrome-stable_current_amd64.deb
RPM or .rpm (Red Hat based software’s)
It is a package management system. The name RPM variously refers to the
.rpm file format, files in this format, software packaged in such files,
and the package manager itself. RPM was intended primarily for
Linux distributions; the file format is the baseline package format of
the Linux Standard Base. RPM was developed by Community & Red
Hat.

Example: Google chrome software


Package Name: google-chrome-stable-57.0.2987.133-1.x86_64.rpm
Installation: rpm -ivh google-chrome-stable-57.0.2987.133-1.x86_64.rpm

NOTE: You will also encounter different commands, packages and


service names while using both kinds of distros.
Basic Commands
The following commands will run in the Git Bash

• pwd: To check the present working directory

• mkdir: Create a directory/folder in your home directory

• cd: Change your current working directory.


NOTE: cd / is used to go to the root directory
cd .. is used to go to one step up in the directory tree
cd & cd ~ is used to go to the home directory

• ls: List all the directories & files

• touch: To create an empty file/files


Absolute path and Relative path

What is a path?
A path is a unique location to a file or a folder in a file system of an
OS. A path to a file is a combination of / and alpha-numeric
characters.

What is an absolute path?


An absolute path is defined as the specifying the location of a file or
directory from the root directory. In other words we can say absolute path is
a complete path from start of actual file system from / directory.

Some examples of absolute path:

/home/imran/linux-practices/
/var/ftp/pub
/etc/samba.smb.conf
/boot/grub/grub.conf
If you see, all these paths started from / directory which is a root
directory for every Linux/Unix machines.

What is the relative path?


Relative path is defined as path related to the present working
directory(pwd). Suppose I am located in /home/imran and I want to
change directory to
/home/imran/linux-practices. I can use relative path concept to
change directory to linux-practices and also devopsdir directory.
If you see all these paths did not start with / directory.
• Creating directories in devopsdir directory with absolute
and relative path

• cp: Copying files into directory

• Copying directories from one location to another

• mv: Moving files from one location to another. It is also used to


rename a file.
• rm: Removing files and directories
VIM Editor
• Installing vim editor

• Open up a file in vim editor (Normal mode)

• Hit i to enter into Insert mode

• type few lines & hit Esc to enter Normal mode

• type :wq to save & exit & press Enter

• cat: To read contents of a file


• VIM (Visual display editor improved) editor is an
improved version of the VI (Visual display editor) editor
already grouped with Linux. This is a command mode
editor for files. Other editors in Linux are emacs, gedit. VI
editor is most popular.

When you open the vim editor, it will be in the command mode
by default.

It has 3 modes:
• Command mode
• Insert mode (Edit mode)
• Extended Mode: (Colon mode): Extended Mode is used for
save and quit or save without quit using "Esc" Key with":".

Command mode shortcuts

gg To go to the beginning of the page


G To go to end of the page
w To move the cursor forward, word by word
b To move the cursor backward, word by word
nw To move the cursor forward to n words (SW)
nb To move the cursor backward to n words (SB)
u To undo last change
Ctrl+R To redo the changes
yy To copy a line
nyy To copy n lines (Syy or 4yy)
p To paste line below the cursor position
P To paste line above the cursor position
dw To delete the word letter by letter {like Backspace}
X To delete the world letter by letter (like DEL key)
dd To delete entire line
ndd To delete n no. of lines from cursor position{Sdd)
I To search a word in the file
Extended Command mode shortcuts
Use Esc key to enter extended command mode, then use these
commands.

:w To save the changes


:q To quit (Without saving)
:wq To save and quit
:w! To save forcefully
:wq! To save and quit forcefully
:x To save and quit
:X To give password to the file or remove password
:n To go to the nth line
:se nu To set serial numbers for lines
:se nonu To remove serial numbers for lines
File System
Types of files in Linux

Symbolic links
Symbolic links are like desktop shortcuts we use in windows.

• Create a soft link for /var/log directory in our current working


directory.
Filter Commands

• grep: global search for the regular expression shortly called


grep, is used to find texts from any text input. Passwd file:
stores information about all the users in the system. Finding
the line which the contains word as “root” from /etc/passwd
file.

Linux is case sensitive, Root is different from root. We can


ignore case in grep with -i option.

To display things except the given word use -v option

• less: Displays file content page wise or line wise. Ex: less /etc/passwd
• more

• head

• tail

tail -f {file-name} can be used to see dynamic content from the file i.e.
recently made changes in a file. To quit from this use CTRL + Z if using
newer versions of Git Bash or use CTRL + C to use older versions of Git
Bash. Use tail -f var/log/ directory structure in root folder of the root
user to see use of this command.
• cut

• awk: Similar to cut is awk. It is a more advanced version of cut


used for advanced searches.

• sed
The /g makes the changes global. If we do not use /g & a line has more
than one instance of the word to be changed than it will only change the
word at the first instance & move to the next line.

To change the original file use option -i. For eg: sed -i ‘s/Tech/Techologies/g’
ktfile will make changes in the original file.

Similar search & replace can be done using the VIM editor but in that
case we can only change a single file.
To search & replace using VIM we need to enter the extended command
mode using esc key & then use the following command.
For eg - :%s/Tech/Techologies
To make the changes global. Use the following
command. For eg - :%s/Tech/Techologies/g

• find
I/O Redirection Commands

• Create a file named devopstools with below content

• Search for text “tech” replace it with “tools” and redirect


output to a new file newtools.txt

• Appending another output in same file “>>”


• Redirecting only error to a file “2>>”

• Redirecting all the output to a file “&>>

Piping
So, far we've dealt with sending data to and from files. Now we'll
take a look at a mechanism for sending data from one program to
another. It's called piping and the operator we use is (|). What this
operator does is feed the output from the program on the left as
input to the program on the right.
Users & Groups
Users

Types of users

TYPE EXAMPLE USER ID GROUP ID HOME SHELL


(ID) (GID) DIRECTORY
ROOT root 0 0 /root /bin/bash
REGULAR imran, 1000 to 1000 to 60000 /home/userna /bin/bash
vagrant 60000 me
SERVICE ftp, ssh, 1 to 999 1 to 999 /var/ftp etc /sbin/nologi n
apache
• /etc/passwd

Add user, set password & switch user

• /etc/group: The file /etc/group stores group


information. Each line in this file stores one group
entry.

Group name, group password, GID, group members


Add group & add user to group

Delete user & group

• /etc/shadow file: This file stores users password and


password related information. Just like /etc/passwd file,
this file also uses an individual line for each entry.
User & Group Commands Cheat Sheet

COMMANDS DESCRIPTION
useradd Creates user in RedHat
adduser Creates user in Ubuntu
id Shows user info
groupadd Creates group
usermod -G grpnam {user-name} Adds user to group
passwd Set/Reset password
userdel -r Removes user with home directory
groupdel Removes group
last Shows last login in the system
who Who is logged into the system
whoami Username
lsof -u {user-name} List of files opened by the user
File Permissions
Changing Permissions - Symbolic Method

chmod [-OPTION] ... mode[,mode] filel directory ...

• mode includes:
u, g or o for user, group and other
o +, - or = for grant, deny or set
r, w or x for read, write or execute

• Options include:
-R Recursive
-v Verbose
--reference Reference another file for its mode

Examples:
chmod ugo+r file: Grant read access to all for file
chmod o-wx dir: Deny write and execute to others for di
Changing Permissions - Numeric Method

Uses a three-digit mode number


• first digit specifies owner's permissions
• second digit specifies group permissions
• third digit represents others permissions

Permissions are calculated by


adding: 4 (for read)
2 (for write)
1 (for execute)

Example:
chmod 640 myfile
Sudo

Sudo gives power to a normal user to execute commands which is


owned by root user. Example shown below:
If a user already has full sudoers privilege, it can become a root
user anytime. sudo -i changes from normal user to root user

User imran was already a sudo user with full privilege.

Adding user sam in sudoers list.

Like a user a group can also be added into sudoers list.


Every time you enter sudo command it asks your own password. To
turn that off use NOPASSWD in sudoers file.

Changing to any other user with “su -” command.

Switching to root user from sam user.


Package/ Software Management
For CentOS

To install telnet
curl
https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/htt
pd- 2.4.6-95.el7.centos.x86_64.rpm -o httpd- 2.4.6-
95.el7.centos.x86_64.rpm

rpm -ivh httpd-2.4.6-95.el7.centos.x86_64.rpm

To install httpd
curl
https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/http
d- 2.4.6-95.el7.centos.x86_64.rpm -o httpd- 2.4.6-
95.el7.centos.x86_64.rpm

rpm -ivh httpd-2.4.6-95.el7.centos.x86_64.rpm

Due to dependencies its failing and it will be installed one we install


all the dependencies. But what if we have hundreds of
dependencies. That can be solved easily by package managers like
YUM.
• repos. d/: It reads each YUM repository configuration file to
get the information required to download and install new
software, resolves software dependencies and installs the
required RPM package files. YUM repository configuration
files must be located in
/etc/yum.repos.d/

• yum --help: Shows the usage of YUM Command with options

• yum update: To update all your packages


• yum install httpd -y: To install httpd

• yum remove httpd -y: To remove httpd


For Ubuntu
curl http://archive.ubuntu.com/ubuntu/pool/universe/t/tree/tree_1.6.0-
1_amd64.deb -o tree_1.6.0-1_amd64.deb

dpkg -i tree_1.6.0-1_amd64.deb

Like YUM for CentOS we have a package manager named apt for Ubuntu.
• sources.list: sources.list file is a key factor in adding or
upgrading applications to on your Ubuntu VM. The file is
basically the roadmap for your system to know where it may
download programs for installation or upgrade.
• apt help: Shows the usage of apt command with options

• apt update: To update all your package lists

• apt search apache2: To search for a package named apache2


• apt install apache2 -y: To install apache2

• apt remove apache2 -y: To remove apache2


Package Management Commands Cheat Sheet

• wget {link}: To download file from a link


• curl {link}: Access file from a link. Similar to wget but has more use
cases
• curl {link} -o {outputfile}: Access file & store the output in a file

Redhat RPM commands


• rpm -ivh {rpm-file}: Install the package
Example: rpm -ivh mozilla-mail-1.7.5-17.i586.rpm
rpm -ivh --test mozilla-mail-1.7.5-17.i586.rpm

• rpm -Uvh {rpm-file}: Upgrade package


Example: rpm -Uvh mozilla-mail-1.7.6-12.i586.rpm
rpm -Uvh --test mozilla-mail-1.7.6-12.i586.rpm

• rpm -ev {package-name}: Erase/Remove an installed package


Example: rpm -ev mozilla-mail

• rpm -ev nodeps {package-name}: Erase/ Remove an


installed package without checking for dependencies
Example: rpm -ev --nodeps mozilla-mail

• rpm -qa: Display list of all installed packages


Example: rpm -ev --nodeps mozilla-mail

• rpm -qa: Display list of all installed packages


Example: rpm -qa
rpm -qa | less

• rpm -qi {package-name}: Display installed information


along with the package version & small description
Example: rpm -qi mozilla-mail

• rpm -qf {/pathtofile/}: Find out what package a file


belongs to i.e. find which package owns the file
Example: rpm -qf /etc/passwd
rpm -qf /bin/bash
• rpm -qc {package-name}: Display list of configuration
file(s) for a package
Example: rpm -qc httpd

• rpm -qcf{/pathtofile/}: Display list of configuration files for a command


Example: rpm -qcf/usr/X11R6/bin/xeyes

• rpm -qa --last: Display list of all recently installed RPMs


Example: rpm -qa –last
rpm -qa --last | less

• rpm -qpR {.rpm-file} rpm -qR {package-name}:


Find out what dependencies a rpm file has
Example: rpm -qpR mediawiki-1.4rc1-4.i5 86.rpm rpm -qR bash

CentOS Commands
dnf Commands: https://www.linuxtechi.com/dnf-command-
examples- rpm- management-fedora-linux/
• dnf --help: Show the help
• dnf search {package-name}: Search from available repositories
• dnf install {package-name} -y: To install the package
Example: dnfinstall httpd -y
• dnf install vim -y: To install VIM editor
• dnf reinstall {package-name}: To reinstall package
• dnf remove {package-name}: To remove package
• dnf update: To update all packages
• dnf update {package-name}: To update package
• dnf grouplist: List all available group packages
• dnf groupinstall {group-name}: Installs all the packages in a group
• dnf repolist: List enabled dnf repositories
• dnf clean all: Clean dnf cache
• dnf install epel- release: Additional package repository that
provides easy access to install packages for commonly used
software.
• dnf history: View history of dnf
• dnf info {package-name}: Shows the information of
package like version, size, source, repository etc
YUM Commands:
https://access.redhat.com/sites/default/files/attachments/rh_yu
m_cheatsheet_1214_jcs_print-1.pdf
• yum --help: Show the help
• yum search {package-name}: Search from available repositories
• yum install {package-name} -y: To install the package
Example: dnfinstall httpd -y
• yum install vim -y: To install VIM editor
• yum reinstall {package-name}: To reinstall package
• yum remove {package-name}: To remove package
• yum update: To update all packages
• yum update {package-name}: To update package
• yum grouplist: List all available group packages
• yum groupinstall {group-name}: Installs all the packages in a group
• yum repolist: List enabled dnf repositories
• yum clean all: Clean dnf cache
• yum install epel- release: Additional package
repository that provides easy access to install
packages for commonly used software.
• yum history: View history of dnf
• yum info {package-name}: Shows the information of
package like version, size, source, repository etc

Ubuntu
Commands
apt
Commands
• apt search {package-name}: Search from available repositories
• apt install {package-name} -y: To install package
Example: apt install apache2 -y
• apt reinstall {package-name}: To reinstall package
• apt remove {package-name}: To remove package
• apt update: To update all packages
• apt update {package-name}: To update package
• apt grouplist: List all available group packages
• apt groupinstall {group-name}: Installs all the packages in a group
• apt repolist: List enabled apt repositories
• apt clean all: Clean apt cache
• apt history: View history of apt
• apt show {package-name}: Shows the information of
package like version, size, source, repository etc
Services
CentOS
Commands Description
sudo systemctl start {service-name} Starts the service
sudo systemctl stop {service-name} Stops the service
sudo systemctl restart {service-name} Restart the service
sudo systemctl status {service-name} Shows the current status
sudo systemctl reload {service-name} Reload the service
sudo systemctl enable {service-name} Starts the service at boot time
sudo systemctl disable {service-name} Stops the service at boot time
sudo systemctl is-active {service-name} Shows whether the service is active
or not
sudo systemctl is-enabled {service-name} Shows whether the service is
enabled or not

Ubuntu
Commands Description
sudo systemctl start {service-name} Starts the service

sudo systemctl stop {service-name} Stops the service

sudo systemctl restart {service-name} Restart the service

sudo systemctl reload {service-name} Reload the service

sudo systemctl enable {service-name} Starts the service at boot time

sudo systemctl disable {service-name} Stops the service at boot time

sudo systemctl is-active {service-name} Shows whether the service is active


or not
sudo systemctl is-enabled {service-name} Shows whether the service is
enabled or not
Processes

Archiving/ Compression
Search

File Transfer
Login (SSH & TELNET)

Disk Usage
System
Hardware
Statistics

You might also like