.github
linux Tutorial
Learning Linux: Essential commands and foundational knowledge . We started from scratch to build a complete tutorial covering everything from commands and tools to kernel structure and all the details.And even though we're not heavily focused on LPIC exams, we've included that too.
Feel free to contribute (lets-fork-it), and if there's any problem or recommendation, I'd be happy to sort it out if you raise an issue.
Repo Contents๐๐๐(More Than You Imagine)
Advanced Mode (On)
Common commands preview
[View Linux command help information] - commands๏ผ
help
,whatis
,info
,which
,whereis
,man
,stat
[Linux file directory management] -commands๏ผ
cd
,ls
,pwd
,mkdir
,rmdir
,tree
,touch
,ln
,rename
,stat
,file
,chmod
,chown
,locate
,find
,cp
,mv
,rm
[Linux file content viewing command] - commands๏ผ
cat
,head
,tail
,more
,less
,sed
,vi
,grep
[Linux File Compression and Decompression] -commands๏ผ
tar
,gzip
,zip
,unzip
[Linux User Management] - commands๏ผ
groupadd
,groupdel
,groupmod
,useradd
,userdel
,usermod
,passwd
,su
,sudo
[Linux System Administration ] - commands๏ผ
reboot
,exit
,shutdown
,date
,mount
,umount
,ps
,kill
,systemctl
,service
,crontab
[Linux Network Administration] - commands๏ผ
curl
,wget
,telnet
,ip
,hostname
,ifconfig
,route
,ssh
,ssh-keygen
,firewalld
,iptables
,host
,nslookup
,nc
,netcat
,ping
,traceroute
,netstat
[Linux Hardware Management ] - commands๏ผ
df
,du
,top
,free
,iotop
[Linux Software Management ] - commands๏ผ
rpm
,yum
,apt-get
,dpkg
View the art of command line repo
Getting Started With linux Overview (Hardware Interaction)
Kernel operating system
Drivers
System software
Application software
Application software with graphical interface
Application software with graphical interface
More info about Linux kernel
Linux Boot Process
There are 6 stages of the Linux boot process
Stage 1: BIOS (Basic Input/Output System)
Bios
stands for basic input/output system.This is the first step of the boot process,
Bios
doing the POST (Power-on Self-test) job.POST is a process of checking the properties.
When the BIOS process is successful, the BIOS will search and boot an operating system contained in devices such as hard drives, CD/DVD, USB.
Normally, the BIOS will check the floppy or CD-ROM drives to see if they can boot from them, then the hardware, the order of checking the drives depends on the configuration in the BIOS .
If the BIOS can't find it
boot device
it will reportNo boot device found
.If the operating system
Linux
is installed on the hard disk, it will find the Master Boot Record at the first sector of the first hard drive.
Stage 2: MBR loading
MBR (Master Boot Record) is stored in the first sector of a data storage device (
/dev/sda
,/dev/hda
).MBR is very small, only
512byte
=1 sector
.The MBR contains the following information:
Primary boot loader code (
446 byte
) : provides information forboot loaderand
locationboot loaderand
the hard drive.Partition table information (
64 byte
) : stores information aboutpartition
.Magic number ( 2 byte) : used to check MBR , if MBR fails, it will restore.
Stage 3: GRUB Loader
After locating the Boot Loader , this step will execute
load
the Boot Loader into memory and read the configuration - information then display the GRUB Boot Menu for the user to choose from.If the User does not select the OS, after a preset time, GRUB will load the default Kernel into memory to boot.
Stage 4: Kernel
The operating system's kernel will be loaded into RAM -> When the kernel works, the first thing will execute the INIT process.
Stage 5: Run Level (INIT)
This is the main stage of the process
boot
.
Run level0 : halt - Shutdown system
Run level1 : single-user mode -No network configuration, start processes and allow loginuser non-root
Run level2 : multi-user mode -no network configuration, start processes.
Run level3 : multi-user mode with networking -boots the system normally on the command line interface.
Run level4 : undefined
Run level5 : X11- start the system on the graphical interface
Run level6 : reboot- reboot the system.
Stage 6: User Prompt
User login and use.
for-deeper-view-of-kernel-click
Shell
Concepts
Shell is a program that executes commands from users or applications - the utility requires passing to Kernel for processing.
Activities of Shell:
1: Command parsing.
2: Interpreting the command's request.
3: Pass message to Kernel.
4: Display the result of the command.
Types of Shell
sh(the Bourne Shell)
Do not autocomplete file names
Do not save the typed statement in memory (
history
)
bash (Bourne-Again Shell)
Linux defaults.
Support more commands.
csh (the C shell)
Written in C . programming language.
Shell Prompt (Shell Prompt)
Is a character or a character set that always starts at the beginning of any command line.
Indicates that shell is ready to receive commands from the user.
$
: ordinary user#
: userroot
Things you should know about Linux Partitions
1.Hard drives
As a data storage device, for example in Linux it
isdev/sda
/dev/sda3
is the 3rd primary partition on the first disk/dev/sdb5
is the first logical partition on the second disk/dev/sda7
is the 3rd logical partition of the first physical disk
2. Partition
Partition is a small partition (Logical Partition) divided fromHard drives
Each Hard driveshas 4 partition, in which partition includes 3 types: Primary partition , Extended partition and Logical Partition
Primary partition +The primary partition can be used to boot the operating system.
Extended partition +Is the remaining data area when we divide into Primary partitions. +Extended partition contains Logical Partitions in it. Each drive can only hold 1 Extended partition. +Logical partition : These are small partitions in the extended partition , usually used to store data.
fdisk
parted
LVM (logical volume management)
In many cases, you need to resize your partitions or even install new disks and add them to your current mount points; Increasing the total size. LVM is designed for this. + Physical Volume (PV): A whole drive or a partition. It is better to define partitions and not use whole disks - unpartitioned. + Volume Groups (VG): This is the collection of one or more PVs. OS will see the vg as one big disk. PVs in one VG, can have different sizes or even be on different physical disks. + Logical Volumes (LV): OS will see lvs as partitions. You can format an LV with your OS and use it.
To find a file with a larger size, you can use the du (Disk Usage) and sort commands. In the command line, run the following
MBR and GPT
Information about hard disk partitions will be stored on MBR (Master Boot record) or GPT (GUID Partition table).
These are two standards for configuring and managing Partitions on hard drives.
The information stored here includes the location and size of the Partitions.
Swap file
Swap is virtual memory concept used on Linux.
When VPS/Serveroperating, if the system runs out of RAM, it will automatically use part of the hard drive as memory for applications to operate.
Swap is slower than RAM because Swap is part of the hard drive.
(mostly we set swap 2_ram but less than 8 GB for hdd and set 1_ram or 2*ram )
These are some of the most used bash environment variables
USER | The name of the logged-in user |
---|---|
PATH | List of directories to search for commands, colon separated |
EDITOR | Default editor |
HISTFILE | System hostname |
HOSTNAME | Show Info |
PS1 | The Prompt! Play with it |
UID | The numeric user id of the logged-in user |
HOME | The user's home directory |
PWD | The current working directory |
SHELL | The name of the shell |
$ | The process id (or PID of the running bash shell (or other) process |
PPID | The process id of the process that started this process (that is, the id of the parent process) |
? | The exit code of the last command |
File Directory
/home โ User personal data
/boot โ Static files of the boot loader
/sbin โ System binaries
/dev โ Device files
/sys
/etc โ Configuration files
/proc โ Process and kernel files
/var โ Variable data files
/tmp โ Temporary files
/lock
/log
/usr โ User binaries and program data
/bin โ Binaries
/man
/lib โ Shared libraries
/local
/share
/opt โ Optional software
/root โ The home directory of the root
/media โ Mount point for removable media
/mnt โ Mount point for mounting a filesystem temporarily
/srv โ Service data
User Permissions and attributes
command | what it means | what it does | how to use it |
---|---|---|---|
chmod | change modifiers | edits the permissions of a file | chmod +x filename |
chattr | change attributes | edits the attributes of a file | chattr +i filename |
chown | change owner |
chmod
is used to edit the permissions on a file. There are 3 permissions and 3 groups, for a total of 9 permissions. The permissions are:
read - allows the group to read the file
write - allows the group to edit the file
execute - allows the group to run the file as a program
for example when using ls -la
you see all of the files permissions
so this file the owner has rw, the group has r and everyone has r
to make a file executable for all users you would run the following command
chmod a+x filename
character | group name | who it applies to |
---|---|---|
u | user | the owner of the file |
g | group | users who are members of the files' group |
o | others | users not included in u or g |
a | all | u, g and o |
chmod can take either a set of three octal (base 8) numbers to set the permissions across all groups, or list the groups "a+" and the permissions that you want to add or "a-" and the permissions you want to subtract.
The character representations of the permissions are:
letter | number | binary | permission |
---|---|---|---|
r | 4 | 100 | read |
w | 2 | 010 | write |
x | 1 | 001 | execute |
So chmod a+rwx filename
is the same as chmod 777 filename
examples:
chmod u+x,g+w filename
chmod u=rwx,g=rw,o=r filename
chmod 764 filename
chmod a+x filename
The chown
command changes the owner and group of a file or directory. This command can authorize a user to become the owner of the specified file or change the group to which the file belongs. User can be user or user D, user group can be group name or group id. The filename can be a list of files separated by spaces, and wildcards can be included in the filename.
Only the file owner and superuser can use this command.
Group Permissions and attributes
apt command in linux (for installing packages)
apt provides a high-level CLI (Command Line Interface) for the package management system and is intended as an interface for the end user which enables some options better suited for interactive usage by default compared to more specialized APT tools like apt-cache and apt-get.
Syntax :apt [...COMMANDS] [...PACKAGES]
update :apt update
full-upgrade :apt full-upgrade
install :apt install [...PACKAGES]
remove :apt remove [...PACKAGES]
purge :apt purge [...PACKAGES]
search :apt search [...REGEX]
This command is used when the user wants to search for the given regex term(s) in the list of available packages and display matches. For example, this command can be useful when you want to search for packages having a specific feature.
show :apt show [...PACKAGES]
APT-GET Command in Linux
Basic Syntax :sudo apt-get [options] [command] [package(s)]
If you want to download only one specific package, you can do:apt-get download [...PACKAGES]
Removing debian packages:apt-get remove [package(s)]
package information with dpkg
If you want to reconfigure a package that is already installed, you can use the:dpkg-reconfigure tzdata
dpkg [OPTIONS] ACTION PACKAGE
wildcard characters for globbing
Wildcard characters are used in globbing and regular expressions to represent patterns of text. While the exact set of wildcard characters can vary depending on the context and programming language, here are some common wildcard characters and their meanings:
Asterisk (
*
): Matches zero or more characters. It is often used as a wildcard for any sequence of characters.Example:
*.txt
matches all files ending with ".txt."
Question Mark (
?
): Matches a single character. It is used to represent any character in a specific position.Example:
file?.txt
matches "file1.txt" and "file2.txt."
Square Brackets (
[]
) and Dash (-
) for Character Ranges: Square brackets are used to define character classes, and the dash is used to specify character ranges within those classes.[aeiou]
matches any one vowel.[0-9]
matches any digit.[A-Za-z]
matches any uppercase or lowercase letter.
Curly Braces (
{}
) for Alternatives: Curly braces are used to specify alternative patterns or choices.{jpg,jpeg,png}
matches either "jpg," "jpeg," or "png."
Exclamation Mark (
!
) or Caret (^
) (Inside Square Brackets): When placed at the beginning of a character class, these symbols negate the class, matching any character not listed in the brackets.[^0-9]
matches any character that is not a digit.
Period (
.
): In regular expressions, the period is a wildcard that matches any single character.Example:
a.c
matches "abc," "adc," etc.
Pipe (
|
) (In Regular Expressions): Represents the logical OR operation and is used to specify alternatives.Example:
apple|banana
matches either "apple" or "banana."
Backslash (
\
) (Escape Character): In some contexts, the backslash is used to escape special characters, so they are treated as literal characters rather than as wildcards or metacharacters.
These wildcard characters provide flexible ways to match patterns in text and are commonly used in various programming languages, command-line utilities, and regular expressions for tasks such as text searching, text processing, and file manipulation. Keep in mind that the availability and behavior of these characters can vary depending on the specific environment or tool you're using.
more info wildcard characters for globbing
2 Ways to Download Files From Linux Terminal
how do you download a file from the terminal?
1.Download files from Linux terminal using wget command
Installing wget:
Download a file or webpage using wget:
2.Download files from Linux command line using curl
Installing curl:
Download a file or webpage using curl:
How to your find your Linux IP address
The following commands will get you the IP address list to find public IP addresses for your machine:
If you enter the command ifconfig
in the terminal, all information will be displayed. You can also use these commands:
The following commands will get you the IP address list to find public IP addresses for your machine:
curl ifconfig.me
curl -4/-6 icanhazip.com
curl ipinfo.io/ip
curl api.ipify.org
curl checkip.dyndns.org
dig +short myip.opendns.com @resolver1.opendns.com
host myip.opendns.com resolver1.opendns.com
curl ident.me
The following commands will get you the private IP address of your interfaces:
ifconfig -a
hostname -I'
ip route get 1.2.3.4'
nmcli -p device show
(best option in my opinion)
Linux Networking Tools
Firewalls
Here are the Best Firewalls for Linux for Effective System Protection(We Mostly use Iptables ,(ufw for regular usages))
iptables
iptables is a powerful configuration tool for controlling traffic to and from your system. Modern Linux kernels come with a packet filtering framework called Netfilter. Netfilter provides operations such as allow, drop, and modify to control the flow of packets into and out of the system. The user-level command-line tool iptables based on the Netfilter framework provides a powerful firewall configuration function, allowing you to add rules to build firewall policies. The richness and complexity of iptables and its baroque command syntax can be overwhelming.
main view of iptable command
chains that packets will go through in iptable
If you live in countries that are embargoed by some companies and applications, you can use some thing like DNS changing for using the apps.
If you dont know how to change and set it follow these steps
connect to your srver with ssh.
open
/etc/resolv.conf
with some editor like nano or vim.change the in front of namesrver like this:
save changes and exit.
How to Check Ubuntu Version Details and Other System Information
type the following command:
Alternate way:
Another Fun way:
Getting SSL Certificat
you can use any kind tools you know here are some of them:
learn getting free ssl with acme step by step
Some of the monitoring tools (Cli)
Viewing Running Processes in Linux
Monitor Network
Monitor Disk Usage
Monitor Memory Usage
Managing Processes in Linux
mosltly use for controlling the cpu usage (nice number is from -20 to 19 ).
How To Add Swap Space
What is Swap ? Swap is a portion of hard drive storage that has been set aside for the operating system to temporarily store data that it can no longer hold in RAM. This lets you increase the amount of information that your server can keep in its working memory, with some caveats.
Checking the System for Swap Information
We can see if the system has any configured swap by typing:sudo swapon --show
.
If you donโt get back any output, this means your system does not have swap space available currently.
You can verify that there is no active swap using the free utility:free -h
Checking Available Space on the Hard Drive Partition
Before we create our swap file, weโll check our current disk usage to make sure we have enough space:df -h
Creating a Swap File
Now that we know our available hard drive space, we can create a swap file on our filesystem. We will allocate a file of the size that we want called swapfile in our root (/) directory.
The best way of creating a swap file is with the fallocate program. This command instantly creates a file of the specified size.
Since the server in our example has 1G of RAM, we will create a 1G file in this guide. Adjust this to meet the needs of your own server:sudo fallocate -l 1G /swapfile
We can verify that the correct amount of space was reserved by typing:ls -lh /swapfile
Enabling the Swap File
Now that we have a file of the correct size available, we need to actually turn this into swap space.
First, we need to lock down the permissions of the file so that only users with root privileges can read the contents. This prevents normal users from being able to access the file, which would have significant security implications.
Make the file only accessible to root by typing:sudo chmod 600 /swapfile
Verify the permissions change by typing:ls -lh /swapfile
only the root user has the read and write flags enabled.
We can now mark the file as swap space by typing:sudo mkswap /swapfile
After marking the file, we can enable the swap file, allowing our system to start using it:sudo swapon /swapfile
Verify that the swap is available by typing:sudo swapon --show
We can check the output of the free utility again to corroborate our findings:free -h
Making the Swap File Permanent
Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically. We can change this by adding the swap file to our /etc/fstab file.
Back up the /etc/fstab file in case anything goes wrong:sudo cp /etc/fstab /etc/fstab.bak
Add the swap file information to the end of your /etc/fstab file by typing:echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Next weโll review some settings we can update to tune our swap space.
Linux as a virtualization guest
topics:Virtual machine/Linux container/Application container/Guest drivers/SSH host keys/D-Bus machine id
To check and see if your host operating system / CPU, supports using hypervisors check for the vmx
(for Intel CPUs) or svm
(for AMD CPUs) in your /proc/cpuinfo
in flags.
Based on your CPU you should have kvm
or kvm-amd
kernel modules loaded.
(If you see hypervisor in your /proc/cpuinfo it means that you are inside a virtualized Linux machine )
let's see the 2 types of hypervisors. First type 2, since it's easier to understand.
Type 2 Hypervisor
These hypervisors run on a conventional operating system (OS) just as other computer programs do. A guest operating system runs as a process on the host. Type-2 hypervisors abstract guest operating systems from the host operating system.
In other words, a type 2 hypervisor is the software between the guest and host. It completely runs on the host OS and provides virtualization to the guest.
Two of the most famous Type 2 hypervisors are VirtualBox (from Oracle) and VMware.
Type 1 Hypervisor
These hypervisors run directly on the host's hardware to control the hardware and manage guest operating systems. For this reason, they are sometimes called bare-metal hypervisors. The first hypervisors, which IBM developed in the 1960s, were native hypervisors. These included the test software SIMMON and the CP/CMS operating system, the predecessor of IBM z/VM.
Some of the most famous Type 1 hypervisors are KVM, Xen & Hyper-V. KVM is built-in since Linux Kernel version 2.6.20.
Creating a Virtual Machine
First, create the machine itself. We tell the hypervisor this machine how much RAM/disk/CPU/... needs and set a name for our machine. Then we need to install the guest OS. This can be done using:
Installing from a CD / DVD / ...
Cloning an existing machine.
Using Open Virtualization Format (OVF) to move machines between hypervisors. This is a standard format for virtual machine definition and may include several files, in this case, you can archive all of them into one Open Virtualization Archive (OVA) file.
It is also possible to create Templates that are master copies* to initiate new machines.
Guest-specific configs
Some configurations are machine specific. For example, a network card's MAC address should be unique for whole the network. If we are cloning a machine or sometimes creating them from templates, at least we need to change these on each machine before booting them:
Host Name
NIC MAC Address
NIC IP (If not using DHCP)
Machine ID (delete the
/etc/machine-id
and/var/lib/dbus/machine-id
and rundbus-uuidgen --ensure
. These two files might be soft links to each other)Encryption Keys like SSH Fingerprints and PGP keys
HDD UUIDs
Any other UUIDs on the system
Last updated