πŸ‘ˆ

Linux Topics

Linux Basics

File & Directory Management (Linux)

File Viewing & Editing (Linux)

File Permissions & Ownership (Linux)

User & Group Management (Linux)

Process Management (Linux)

Networking Basics (Linux)

Disk & Storage Management (Linux)

Package Management (Linux)

Service Management (systemd)

Environment Variables & Shell

Searching & Text Processing (Linux)

Compression & Archiving (Linux)

SSH & Remote Access

Bash Scripting Basics

Linux Security Basics

Logs & Troubleshooting (Linux)

Linux for Cloud (AWS Context)

πŸ‘ˆ

Linux Basics

1️⃣ What is Linux?

Linux is an operating system, just like:

  • Windows
  • macOS

But Linux is:

  • Free
  • Open source
  • Very powerful
  • Mostly used on servers

πŸ‘‰ AWS uses Linux on most servers (EC2)

So, when you use AWS, you are actually using Linux machines.


2️⃣ Why Linux is important for AWS?

In AWS:

  • EC2 instances mostly run Linux
  • Docker runs on Linux
  • Kubernetes runs on Linux
  • CI/CD servers run on Linux

πŸ’‘ If you know Linux:

  • You can manage EC2 easily
  • You can debug issues faster
  • You can deploy applications confidently

3️⃣ Linux Distributions (Very Important)

Linux has many versions. These are called distributions (distros).

Common Linux distros in AWS:

DistributionUsed where
Amazon LinuxDefault in AWS EC2
UbuntuVery popular
RHELEnterprise systems
CentOSOlder AWS setups

πŸ‘‰ For AWS learning, focus on:

  • Amazon Linux
  • Ubuntu

4️⃣ Linux is Case-Sensitive ⚠

Linux treats:

File.txt β‰  file.txt
DEV β‰  dev

This is very important in AWS, especially:

  • File names
  • Paths
  • Scripts

5️⃣ Linux Directory Structure (High-Level)

Linux files are organized like a tree.

Important folders you must know:

DirectoryPurpose
/Root (starting point)
/homeUser home folders
/etcConfiguration files
/varLogs & variable data
/optOptional software
/binBasic commands
/usrUser programs

Example:

/home/ec2-user
/etc/nginx/nginx.conf
/var/log/messages

πŸ‘‰ In AWS, logs are often in /var/log


6️⃣ Root User vs Normal User

Root user

  • Full access
  • Can do anything
  • Very dangerous if misused
  • Limited access
  • Uses sudo for admin tasks

Example:

sudo yum install nginx

πŸ‘‰ On AWS EC2:

  • Default user is NOT root

  • Example users:

    • ec2-user (Amazon Linux)
    • ubuntu (Ubuntu)

7️⃣ Linux Shell & Terminal

Shell

  • A program that understands your commands

Common shell:

  • bash (most common)

Terminal

  • The place where you type commands

In AWS:

  • You connect to EC2
  • You get a terminal
  • You run Linux commands

8️⃣ Linux vs Windows (Simple Comparison)

LinuxWindows
CLI focusedGUI focused
LightweightHeavy
FreePaid
Best for serversBest for desktop

AWS = Linux-first


9️⃣ What you should remember from Topic 1

βœ… Linux is the backbone of AWS βœ… AWS EC2 mostly runs Linux βœ… Linux is case-sensitive βœ… Know basic directories βœ… Root vs normal user concept


File & Directory Management (Linux)

1️⃣ What is a File & Directory?

  • File β†’ Stores data Example: app.js, config.yml, log.txt

  • Directory (folder) β†’ Stores files and other folders Example: /home/ec2-user, /var/log

πŸ‘‰ On AWS EC2, everything you do is inside files & directories.


2️⃣ Current Working Directory

When you open a terminal, you are inside a directory.

Command:

pwd

πŸ“Œ Shows where you are now

Example output:

/home/ec2-user

3️⃣ Listing Files & Folders

ls        # list files
ls -l     # detailed list
ls -a     # show hidden files
ls -la    # detailed + hidden
  • l β†’ long listing format (Shows detailed information about files)
  • a β†’ all files, including hidden files (those starting with .)

Columns explanation (left to right)

Example line:

-rw-rw-r--. 1 ec2-user ec2-user 53142881 Jan  9 13:41 EWA-1.0.0.jar

πŸ”Ή Column 1: File type + Permissions

-rw-rw-r--.

Breakdown:

PartMeaning
-File type
rw-Owner permissions
rw-Group permissions
r--Others permissions
.SELinux context (Amazon Linux uses SELinux)

File type (first character)

SymbolMeaning
-Regular file
dDirectory
lSymbolic link

1
  • For files β†’ usually 1
  • For directories β†’ number of subdirectories + itself

πŸ”Ή Column 3: Owner (User)

ec2-user
  • The user who owns the file

πŸ”Ή Column 4: Group

ec2-user
  • The group that owns the file

πŸ”Ή Column 5: File size (in bytes)

53142881
  • Size is always in bytes
  • Example: 53142881 bytes β‰ˆ 50.7 MB

πŸ”Ή Column 6–8: Date & Time (Last Modified)

Jan  9 13:41
  • This shows last modification time
  • If the file is old, you’ll see year instead of time.

πŸ”Ή Column 9: File / Directory name

EWA-1.0.0.jar
  • Actual name of the file or directory
  • Hidden files start with .

Example: .bashrc, .ssh


Special entries in your output

πŸ”Έ . (current directory)

drwx------. 3 ec2-user ec2-user 131 Jan  9 15:37 .
  • Refers to current directory

πŸ”Έ .. (parent directory)

drwxr-xr-x. 3 root root 22 Jan  9 13:15 ..
  • Refers to parent directory

4️⃣ Changing Directories

cd folder-name     # go inside folder
cd ..              # go back one level
cd ~               # go to home directory
cd /               # go to root

Example:

cd /var/log

5️⃣ Absolute vs Relative Paths (VERY IMPORTANT)

Absolute Path

  • Starts from /
  • Works from anywhere

Example:

/home/ec2-user/app/config.yml

Relative Path

  • Based on current directory

Example:

config.yml
../logs/app.log

πŸ‘‰ In AWS scripts, absolute paths are safer.


6️⃣ Creating Files & Directories

touch file.txt         # create empty file
mkdir folder           # create folder
mkdir -p a/b/c         # create nested folders
  • -p β†’ Parents (Create parent directories as needed.)

7️⃣ Copying Files & Directories

cp file1 file2         # copy file
cp file folder/        # copy into folder
cp -r dir1 dir2        # copy directory
  • First = OLD (source)
  • Second = NEW (destination)

Example:

cp config.yml backup.yml

8️⃣ Moving & Renaming

mv oldname newname     # rename
mv file folder/        # move

Example:

mv app.log app-old.log

9️⃣ Deleting Files & Directories ⚠

rm file               # delete file
rm -r folder          # delete folder
rm -rf folder         # force delete (DANGEROUS)
  • -r β†’ Recursive (Remove the folder and everything inside it, recursively)
  • -f β†’ Force (Forcefully remove the folder and all its contents recursively, without asking for confirmation)

⚠ Important warning

  • Linux has NO recycle bin
  • rm -rf / can destroy the system

πŸ‘‰ Be extra careful on production EC2 servers.


πŸ”Ÿ Useful Shortcuts

cd .     # current directory
cd ..    # parent directory
cd ~     # home directory
cd /     # root directory


1️⃣1️⃣ What you must remember (Exam + Real AWS)

βœ… Linux has no GUI on EC2 βœ… You manage everything via files & folders βœ… Paths matter (case-sensitive) βœ… rm -rf is dangerous βœ… Absolute paths are safer in AWS scripts


File Viewing & Editing (Linux)

1️⃣ Why this topic is important for AWS?

On AWS EC2, you will:

  • Read log files
  • Edit configuration files
  • Check application output
  • Debug errors

2️⃣ Viewing File Content (Basic)

cat – show entire file

cat file.txt

Use when:

  • File is small

⚠ Not good for large log files.


less file.txt

Controls:

  • ↑ ↓ β†’ scroll
  • Space β†’ next page
  • /error β†’ search
  • q β†’ quit

more – simple pager

more file.txt

Less powerful than less.


3️⃣ Viewing Part of a File

head – beginning of file

head file.txt
head -n 20 file.txt
  • -n β†’ Number of lines (Show N number of lines, by defult 10).

tail – end of file

tail file.txt
tail -n 50 file.txt

πŸ”₯ tail -f – LIVE log monitoring (VERY IMPORTANT)

tail -f /var/log/nginx/access.log
  • -f β†’ Follow (keep following the file and show new lines as they are added)

πŸ‘‰ Used to:

  • Monitor running applications
  • Debug live issues on EC2

Press Ctrl + C to stop.


4️⃣ Editing Files in Linux

On AWS EC2, you usually edit files using:

  • nano (easy)
  • vi / vim (powerful)

5️⃣ nano Editor (Beginner Friendly)

nano file.txt

Controls:

  • Type β†’ edit
  • Ctrl + O β†’ save
  • Enter β†’ confirm
  • Ctrl + X β†’ exit

πŸ‘‰ Recommended when you are starting AWS.


6️⃣ vi Editor (Basic Knowledge Enough)

Open file:

vi file.txt

Modes in vi

  • Normal mode β†’ navigation
  • Insert mode β†’ editing
  • Command mode β†’ save/exit

Important commands:

i       β†’ insert mode
Esc     β†’ normal mode
:w      β†’ save
:q      β†’ quit
:wq     β†’ save & quit
:q!     β†’ quit without saving

8️⃣ What you must remember

βœ… Use less for reading files βœ… Use tail -f for live logs βœ… Use nano for easy editing βœ… Know basic vi commands βœ… Use sudo for system files


Excellent πŸ‘ Now we are at Topic 4: File Permissions & Ownership. This topic is CRITICAL for AWS. Many EC2 issues happen only because of permissions.

I’ll explain slowly, clearly, and with real AWS examples.


File Permissions & Ownership (Linux)

1️⃣ Why permissions are important in AWS?

On AWS EC2:

  • Applications fail to start ❌
  • Scripts don’t run ❌
  • Logs can’t be written ❌

Most of the time the reason is: πŸ‘‰ Wrong file permissions


2️⃣ Understanding File Permissions

Run:

ls -l

Example output:

-rwxr-xr-- 1 ec2-user ec2-user  512 app.sh

Let’s break it down.


3️⃣ Permission Structure (Very Important)

-rwx r-x r--
  β”‚    β”‚   β”‚
  β”‚    β”‚   └── Others
  β”‚    └────── Group
  └────────── User (Owner)

Permission letters:

  • r β†’ read
  • w β†’ write
  • x β†’ execute
  • - β†’ no permission

4️⃣ User, Group, Others

TypeMeaning
UserFile owner
GroupGroup owner
OthersEveryone else

πŸ‘‰ Linux always checks permissions in this order: User β†’ Group β†’ Others


5️⃣ File vs Directory Permissions

File

  • r β†’ read file
  • w β†’ modify file
  • x β†’ run file (script)

Directory

  • r β†’ list files
  • w β†’ create/delete files
  • x β†’ enter directory

πŸ‘‰ Without x on a directory, you cannot cd into it.


6️⃣ Changing Permissions chmod (change mode)

Symbolic mode

chmod u+x file.sh              # add execute to user
chmod g-w file.txt             # remove write from group
chmod o+r file.txt             # add read to others
chmod u=rwx,g=rx,o=r file.txt  # set exact permissions
chmod a+x script.sh            # add execute to all
chmod a-r file.txt             # remove read from all
chmod a=r file.txt             # set read for all
chmod +x script.sh             # add execute to all
  • a means all (user, group, others)
  • u means user (owner)
  • g means group
  • o means others

Numeric mode (VERY IMPORTANT)

NumberPermission
4read
2write
1execute

Example:

chmod 755 app.sh

Meaning:

User   β†’ 7 (rwx)
Group  β†’ 5 (r-x)
Others β†’ 5 (r-x)

7️⃣ Making a Script Executable (AWS COMMON)

If you see:

permission denied

Fix:

chmod +x script.sh
./script.sh

πŸ‘‰ Common in user-data scripts.


8️⃣ File Ownership chown (change owner)

Change owner:

chown user file.txt  # user β†’ new owner (user)

Change owner & group:

chown user:group file.txt  # user β†’ new owner, group β†’ new group

Example (AWS):

sudo chown ec2-user:ec2-user app.log 

9️⃣ Why sudo is needed

System files are owned by root.

Example:

sudo chmod 644 /etc/nginx/nginx.conf

Without sudo:

Permission denied

πŸ”Ÿ Common AWS Permission Issues

❌ App cannot write logs ❌ Script not executing ❌ Nginx cannot access files ❌ Docker volume permission issues


1️⃣1️⃣ Real AWS Example

ls -l /var/www/html
sudo chown -R nginx:nginx /var/www/html
sudo chmod -R 755 /var/www/html
  • -R means recursive (apply the command to the folder AND everything inside it)

1️⃣2️⃣ What you MUST remember

  • Permissions = User | Group | Others
  • chmod controls access
  • chown controls ownership
  • 755 is very common in AWS
  • sudo is required for system files

User & Group Management (Linux)

1️⃣ Why this topic is important for AWS?

On AWS EC2:

  • Multiple people may access one server
  • Applications should not run as root
  • Security is very important

πŸ‘‰ Linux users & groups help control access


2️⃣ What is a User?

A user is an account that can:

  • Login to Linux
  • Run commands
  • Own files
  • Run applications

3️⃣ Root User vs Normal User

Root user

  • Superuser
  • Full access
  • Can break the system
  • Limited permissions
  • Uses sudo for admin tasks

πŸ‘‰ On AWS:

  • You login as ec2-user or ubuntu
  • You use sudo when needed

4️⃣ What is a Group?

A group is a collection of users.

Why groups?

  • Easier permission management
  • Used by applications (nginx, docker, etc.)

5️⃣ Important User Commands

Check current user

whoami

User details

id

6️⃣ Creating a User (Admin Task)

sudo useradd devuser
sudo passwd devuser

7️⃣ Switching Users

su devuser

Switch to root:

sudo su -

⚠ Be careful with root.


8️⃣ Deleting a User

sudo userdel devuser

9️⃣ Group Management

Create group

sudo groupadd devgroup

Add user to group

sudo usermod -aG devgroup devuser

Example (AWS common):

sudo usermod -aG docker ec2-user

πŸ”Ÿ AWS Real-Life Examples

  • Add user for SSH access
  • Add user to docker group
  • Restrict access to logs
  • Avoid using root

Process Management (Linux)

1️⃣ What is a Process?

A process is: πŸ‘‰ A running program in Linux

Examples:

  • Nginx running
  • Java application running
  • Node.js server running
  • SSH session running

If a program is running β†’ it has a process ID (PID).


2️⃣ Process ID (PID)

Every process has a unique number called PID.

Example:

nginx β†’ PID 1345
java  β†’ PID 2098

πŸ‘‰ PID is used to monitor or kill a process.


3️⃣ Foreground vs Background Process

Foreground Process

  • Runs in terminal
  • Blocks the terminal

Example:

sleep 30  # sleep for 30 seconds

Terminal is busy until process stops.


Background Process

  • Runs in background
  • Terminal is free

Example:

sleep 30 &  # run in background

4️⃣ Sending Process to Background

If a process is running:

  • Press Ctrl + Z β†’ pause
  • Then run:
bg

5️⃣ Process States (High-Level)

StateMeaning
RRunning
SSleeping
DWaiting
ZZombie
TStopped

πŸ‘‰ Zombie processes = badly written apps.


6️⃣ Monitoring Processes

ps – process snapshot

ps
output: 
    PID TTY          TIME CMD
  48615 pts/7    00:00:00 bash
  49301 pts/7    00:00:00 sleep
  49303 pts/7    00:00:00 ps
  • PID (Process ID): Unique identifier for each running process.
  • TTY (Terminal Type): The terminal associated with the process.
  • TIME: The amount of CPU time the process has used.
  • CMD (Command): The command that started the process.
ps aux

What this command means

  • ps β†’ show running processes
  • a β†’ processes for all users
  • u β†’ show user-oriented format (CPU, memory, etc.)
  • x β†’ include processes not attached to a terminal

Most common:

ps aux | less
ps -p PID # show specific process by PID

top – live monitoring (VERY IMPORTANT)

top

Shows:

  • CPU usage
  • Memory usage
  • Running processes

Press q to quit.


7️⃣ Finding a Specific Process

ps aux | grep nginx
ps aux | grep java

Used when:

  • App is running but not responding

8️⃣ Killing Processes (IMPORTANT)

Graceful stop

kill PID

Force stop (DANGEROUS)

kill -9 PID

⚠ Use -9 only if normal kill fails.


9️⃣ Kill by Name

pkill nginx
killall node

Useful when PID is unknown.


πŸ”Ÿ AWS Real-Life Examples

Nginx consuming high CPU

top
ps aux | grep nginx
sudo kill PID

1️⃣1️⃣ Background Jobs

List jobs:

jobs

Bring to foreground:

fg  

The fg command is used in Linux/Unix shells to resume a stopped or background job and bring it to the foreground.

1️⃣3️⃣ Background Processes in AWS (nohup)

What happens with & (background only)

  • & runs the command in the background Example:
java -jar EWA-1.0.0.jar > app.log 2>&1 &

βœ… What this does:

  • Runs the app in the background
  • You can still use the same terminal

❌ What happens when you:

  • Close SSH
  • Network disconnects
  • Session times out

πŸ‘‰ Process gets killed

Why this happens (important)

Processes started with & still receive SIGHUP (hangup signal) when the terminal closes.

Linux says:

β€œTerminal gone β†’ kill child processes”

What nohup does

Example nohup java -jar EWA-1.0.0.jar > app.log 2>&1 &

βœ… What this does:

  • Ignores SIGHUP
  • Process keeps running after logout
  • Fully detached from terminal

That’s why nohup = NO HANG UP

Feature&nohup &
Runs in backgroundβœ…βœ…
Survives SSH logoutβŒβœ…
Survives network dropβŒβœ…
Used on servers❌ riskyβœ… common
Creates nohup.outβŒβœ… (if not redirected)

1️⃣2️⃣ What you MUST remember

  • βœ… Process = running program
  • βœ… Every process has PID
  • βœ… top is your best friend
  • βœ… kill stops processes
  • βœ… Use kill -9 carefully
  • βœ… Background processes are common in AWS

Networking Basics (Linux)

1️⃣ IP Address & Hostname

IP Address

An IP address uniquely identifies a machine on a network.

Example:

172.31.12.45

In AWS EC2:

  • Private IP β†’ inside VPC
  • Public IP β†’ access from internet

Check IP Address

ip a   # show IP addresses

or

ifconfig  # older command

Hostname

A hostname is the name of the machine.

Check hostname:

hostname

Set hostname:

sudo hostnamectl set-hostname my-ec2-server

2️⃣ Network Interfaces

A network interface connects your server to the network.

Example:

eth0
ens5

Check interfaces:

ip link

3️⃣ Ports & Services (VERY IMPORTANT)

Port

A port is a communication endpoint.

Common ports:

PortService
22SSH
80HTTP
443HTTPS
3306MySQL
8080App server

Check Open Ports

netstat -tulnp  # Show all TCP ports that are currently listening, with process info.
or
ss -tulnp  # Modern replacement for netstat
OptionMeaning
-tShow TCP connections only
-oShow timers (retransmit / keepalive info)
-nShow numeric addresses (no DNS lookup)
-lShow listening (server) ports
-pShow PID / program name using the port

output:

State   Recv-Q  Send-Q  Local Address:Port  Peer Address:Port  Process
LISTEN  0       128           0.0.0.0:22         0.0.0.0:*
LISTEN  0       128              [::]:22            [::]:*
LISTEN  0       511                 *:80               *:*
LISTEN  0       100                 *:8080             *:*      users:(("java",pid=51635,fd=19))
ColumnExplanation
ProtoProtocol (tcp, tcp6)
Recv-QData waiting to be read
Send-QData waiting to be sent
Local AddressIP:Port on your server
Foreign AddressClient side (who can connect)
StateConnection state
PID/Program nameProcess using the port
TimerTCP timer info

4️⃣ Connectivity Testing

ping – test network reachability

ping google.com
ping 8.8.8.8

Stop with Ctrl + C.

⚠ AWS Security Groups may block ping.


curl – test web & APIs (VERY IMPORTANT)

curl http://localhost
curl http://public-ip
curl https://api.example.com

Used to:

  • Test APIs
  • Check if app is running

telnet / nc – test port connectivity

telnet localhost 80
nc -zv localhost 8080

5️⃣ Downloading from Internet

wget

wget https://example.com/file.zip

curl download

curl -O https://example.com/file.zip

Used in:

  • App setup
  • User-data scripts
  • CI/CD pipelines

6️⃣ AWS Real-Life Debugging Scenario

❌ App not accessible from browser

Steps:

ip a
ss -tulnp
curl localhost

If works locally but not from browser: πŸ‘‰ Issue is Security Group / NACL, not Linux.


7️⃣ What you MUST remember

  • βœ… IP identifies the server
  • βœ… Ports expose services
  • βœ… ss -tulnp shows listening ports
  • βœ… curl is essential for AWS
  • βœ… Download tools are used everywhere

Disk & Storage Management (Linux)

🧱 What is Disk & Storage Management in Linux?

Disk & Storage Management means:

  • Detecting disks
  • Creating partitions
  • Formatting filesystems
  • Mounting disks
  • Managing space
  • Monitoring disk usage
  • Handling permissions & performance

πŸ‘‰ In simple words:

It is how Linux stores, organizes, and accesses your data on disks


πŸ–΄ Types of Storage Devices in Linux

DeviceExample
Hard Disk (HDD)/dev/sda
Solid State Drive (SSD)/dev/nvme0n1
USB Drive/dev/sdb
Virtual Disk (VM / EC2)/dev/xvda

🧭 Disk Naming in Linux

Linux treats everything as a file.

NameMeaning
/dev/sdaFirst disk
/dev/sda1First partition
/dev/sdbSecond disk
/dev/nvme0n1p1NVMe partition

Check disks:

lsblk

πŸ“¦ Partitions (Why needed?)

A partition divides a disk into logical sections.

Example:

  • Disk: 1 TB
  • Partition 1: OS
  • Partition 2: Home
  • Partition 3: Backup

View partitions:

lsblk
fdisk -l

πŸ›  Partitioning Tools

ToolUse
fdiskMBR partitions
cfdiskMenu based
partedGPT partitions
lsblkDisplay structure

Create partition (example):

sudo fdisk /dev/sdb

Common fdisk commands:

  • n β†’ new partition
  • p β†’ print table
  • w β†’ write changes

🧬 Filesystem (VERY IMPORTANT)

A filesystem defines how data is stored & retrieved.

FilesystemUse
ext4Default Linux
xfsHigh performance
ntfsWindows
vfatUSB drives

Format partition:

sudo mkfs.ext4 /dev/sdb1

πŸ“Œ Mounting (Make storage usable)

Linux needs to mount a disk to access it.

Temporary Mount

sudo mount /dev/sdb1 /mnt/data

Check mounts:

df -h
mount

Unmount:

sudo umount /mnt/data

πŸ“Œ Permanent Mount (fstab)

File:

/etc/fstab

Example entry:

/dev/sdb1  /data  ext4  defaults  0  2

Apply:

sudo mount -a

⚠️ Wrong fstab = system boot failure


πŸ“Š Disk Usage Monitoring

Check disk space

df -h

Check folder size

du -sh /var/log

Find large files

find / -size +1G

πŸ“‚ Inodes (Often ignored but important)

Each file uses an inode.

Check inode usage:

df -i

Problem:

Disk shows free space but still β€œNo space left”

Cause:

Inodes exhausted


πŸ” Permissions & Ownership

Check:

ls -l

Format:

-rwxr-xr--

Change owner:

sudo chown user:group file

Change permission:

chmod 755 file

πŸ”₯ Logical Volume Management (LVM)

LVM allows:

  • Resize disks
  • Combine multiple disks
  • Snapshot backups

LVM Components

ComponentMeaning
PVPhysical Volume
VGVolume Group
LVLogical Volume

Commands:

pvcreate /dev/sdb
vgcreate vg_data /dev/sdb
lvcreate -L 10G -n lv_data vg_data

Format & mount:

mkfs.ext4 /dev/vg_data/lv_data
mount /dev/vg_data/lv_data /data

☁️ Disk Management in Cloud (AWS EC2)

List disks:

lsblk

Extend EBS volume:

growpart /dev/xvda 1
resize2fs /dev/xvda1

🧯 Disk Errors & Health

Check filesystem:

fsck /dev/sdb1

SMART health:

smartctl -a /dev/sda

🚨 Common Disk Problems

IssueReason
Disk fullLogs, backups
Boot failureWrong fstab
Slow IOHDD / high load
Read-only FSDisk error

🧠 Important Linux Directories

PathPurpose
/Root
/homeUser data
/varLogs
/tmpTemporary
/bootBoot files

πŸ§ͺ Real-World Example

Production server is down due to disk full

Steps:

df -h
du -sh /var/*
rm -rf /var/log/*.gz

🎯 Interview-Ready Questions

Q: What is the difference between partition & filesystem?

  • Partition divides disk
  • Filesystem organizes data

Q: What is LVM?

  • Logical Volume Manager used to resize and manage disks dynamically

Q: What happens if fstab is wrong?

  • System may fail to boot

🧾 Must-Know Commands (Cheat Sheet)

lsblk
df -h
du -sh
mount
umount
fdisk
mkfs
fsck
chmod
chown

Package Management (Linux)

πŸ“¦ What is Package Management?

Package Management is the system used to:

  • Install software
  • Update software
  • Remove software
  • Manage dependencies
  • Keep software secure and up to date

πŸ‘‰ In simple words:

It is how Linux installs and manages software safely


🧠 What is a Package?

A package is:

  • A compiled application
  • With configuration files
  • With dependency information
  • With version details

πŸ— Why Package Management Exists

Without package management:

  • Manual downloads
  • Dependency hell
  • Broken systems

With package management:

  • Automatic dependency resolution
  • Central repositories
  • Security updates

πŸ—‚ Package Management Components

ComponentPurpose
PackageSoftware unit
RepositorySoftware source
Package ManagerTool to manage packages
DependencyRequired packages

🧰 Common Linux Package Managers

Package ManagerLinux Distribution
yum / dnfAmazon Linux, RHEL, CentOS
aptUbuntu, Debian
pacmanArch Linux
zypperSUSE

πŸ‘‰ Amazon Linux is most common in AWS, so focus mainly on yum / dnf, but know apt basics.


1️⃣ yum – (Amazon Linux, RHEL, CentOS)

πŸ”Ή What is yum?

yum = Yellowdog Updater Modified It downloads software from repositories, resolves dependencies, and installs packages.


πŸ”Ή Update package list

sudo yum check-update

What it does:

  • Checks for available updates
  • Does NOT install anything

πŸ“Œ Use before upgrading servers in AWS.


πŸ”Ή Install a package

sudo yum install nginx

Meaning:

  • install β†’ install software
  • nginx β†’ package name

AWS examples:

sudo yum install httpd      # Apache web server
sudo yum install docker    # Docker
sudo yum install git       # Git

πŸ”Ή Install without confirmation

sudo yum install nginx -y

-y = yes automatically

πŸ“Œ Very common in EC2 user-data scripts


πŸ”Ή Remove a package

sudo yum remove nginx

Remove unused deps

sudo yum autoremove

Removes software (not always config files).


πŸ”Ή Update all packages

sudo yum update

or

sudo yum update -y

πŸ“Œ Used during EC2 patching & security updates.


πŸ”Ή Search for a package

yum search docker

Finds package names from repositories.


πŸ”Ή Show package info

yum info nginx

Shows:

  • Version
  • Size
  • Repository
  • Description

πŸ”Ή List installed packages

yum list installed 

To check what you installed recently:

yum history

πŸ”Ή Check if a package is installed

yum list installed nginx

πŸ”Ή Clean cache

sudo yum clean all

Clears downloaded package cache.

πŸ“Œ Used when yum behaves incorrectly.


2️⃣ dnf – (Amazon Linux 2023)

πŸ”Ή What is dnf?

dnf = Dandified YUM It is the modern replacement for yum.

πŸ‘‰ Commands are almost the same.

sudo dnf install nginx
sudo dnf remove nginx
sudo dnf update -y
dnf search docker
dnf info nginx

πŸ“Œ If AWS exam mentions Amazon Linux 2023 β†’ think dnf.


3️⃣ apt – (Ubuntu on EC2)

πŸ”Ή Update package index (VERY IMPORTANT)

sudo apt update

πŸ“Œ Always run this before installing anything.


πŸ”Ή Install a package

sudo apt install nginx

πŸ”Ή Install with auto yes

sudo apt install nginx -y

πŸ”Ή Remove a package

sudo apt remove nginx

Remove + config:

sudo apt purge nginx

πŸ”Ή Upgrade installed packages

sudo apt upgrade

or

sudo apt upgrade -y

πŸ”Ή Full upgrade (handles dependencies)

sudo apt full-upgrade

πŸ”Ή Search package

apt search docker

πŸ”Ή Show package info

apt show nginx

πŸ”Ή List installed packages

apt list --installed

4️⃣ Repository Management (AWS Important)

πŸ”Ή What is a repository?

A repo is a server that stores packages.

AWS examples:

  • Amazon Linux repos
  • EPEL (Extra Packages for Enterprise Linux)

πŸ”Ή List yum repositories

yum repolist

or

dnf repolist

πŸ”Ή Enable Amazon Linux extras

sudo amazon-linux-extras list

Install from extras:

sudo amazon-linux-extras install docker

πŸ“Œ VERY important for AWS EC2


5️⃣ Package Files (rpm & dpkg)

πŸ”Ή RPM (Amazon Linux / RHEL)

rpm -qa

List all installed rpm packages.

rpm -q nginx

Check if nginx installed.


πŸ”Ή DPKG (Ubuntu)

dpkg -l
dpkg -l nginx

6️⃣ Real AWS EC2 Examples πŸ”₯

πŸ”Ή Install web server on EC2 (Amazon Linux)

sudo yum update -y
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd

πŸ”Ή Install Docker on EC2

sudo yum install docker -y
sudo systemctl start docker
sudo systemctl enable docker

πŸ”Ή EC2 User-Data Script Example

#!/bin/bash
yum update -y
yum install httpd -y
systemctl start httpd

πŸ“Œ Package commands are heavily used in user-data.


7️⃣ AWS Exam Focus (Remember This)

  • βœ… Know which OS uses which package manager
  • βœ… Understand install, remove, update, search
  • βœ… Know -y flag
  • βœ… Know amazon-linux-extras
  • βœ… Know difference between yum / dnf / apt

🧠 Simple Rule to Remember

Amazon Linux β†’ yum / dnf Ubuntu β†’ apt


🎯 Interview-Ready Questions

Q: What is a package manager?

A tool that installs, updates, removes, and manages software along with its dependencies.

Q: Difference between apt & yum?

  • apt β†’ Debian based
  • yum/dnf β†’ RHEL based

Q: What is a repository?

A centralized storage location for packages.


🏁 Final Summary

Package management in Linux ensures safe, fast, and dependency-aware software installation and maintenance, making Linux systems stable and secure.


Service Management (systemd)

πŸ”§ What is Service Management?

Service Management means:

  • Starting services (like nginx, mysql, ssh)
  • Stopping services
  • Restarting services
  • Enabling services at boot
  • Checking service health

πŸ‘‰ In simple words:

It is how Linux runs and controls background programs (services/daemons)


🧠 What is systemd?

systemd is the service manager in Linux.

  • The init system of modern Linux
  • The first process started by the kernel
  • Runs with PID 1

It:

  • Starts services
  • Stops services
  • Restarts services
  • Starts services automatically at boot Check:
ps -p 1

Output:

systemd

πŸ”Ή Main command: systemctl

systemctl = system control

This is the only command you need to manage services.


πŸ”„ What is a Service / Daemon?

A service (daemon) is a program that:

  • Runs in background
  • Starts at boot (optional)
  • Provides functionality

Examples:

  • ssh β†’ Remote login
  • nginx β†’ Web server
  • docker β†’ Container engine
  • mysql β†’ Database

πŸ—‚ systemd Unit Types

systemd manages units.

Unit TypeExtensionPurpose
Service.serviceBackground services
Socket.socketSocket activation
Target.targetGroup of units
Mount.mountMount points
Timer.timerScheduled jobs

1️⃣ Start a Service

sudo systemctl start httpd

Meaning:

  • start β†’ start the service now
  • httpd β†’ service name

πŸ“Œ After starting, service runs until reboot (unless enabled).


2️⃣ Stop a Service

sudo systemctl stop httpd

Stops the service immediately.

πŸ“Œ Website goes down if web server stops.


3️⃣ Restart a Service (VERY COMMON)

sudo systemctl restart httpd

Used when:

  • Config file changes
  • App update
  • Debugging issues

πŸ“Œ Most used command in real AWS work.


4️⃣ Reload a Service

sudo systemctl reload httpd

Difference:

restartreload
Stops & starts serviceReloads config only
Causes short downtimeNo downtime

πŸ“Œ Not all services support reload.


5️⃣ Check Service Status ⭐ (MOST IMPORTANT)

systemctl status httpd

Shows:

  • Is service running?
  • Error messages
  • Logs (last lines)
  • PID

πŸ“Œ First command to run when something breaks


6️⃣ Enable Service at Boot (AWS CRITICAL)

sudo systemctl enable httpd

Meaning:

  • Service starts automatically when EC2 reboots

πŸ“Œ Without this:

  • After EC2 restart β†’ website DOWN ❌

7️⃣ Disable Service at Boot

sudo systemctl disable httpd

Service will NOT start on reboot.


8️⃣ Start + Enable Together

sudo systemctl enable --now httpd

Meaning:

  • Start service now
  • Enable it for boot

πŸ“Œ Clean & professional command.


9️⃣ List All Services

πŸ”Ή All running services

systemctl list-units --type=service

πŸ”Ή All services (enabled + disabled)

systemctl list-unit-files --type=service

πŸ”Ÿ Check if Service is Enabled

systemctl is-enabled httpd

Output:

  • enabled
  • disabled

1️⃣1️⃣ Check if Service is Active

systemctl is-active httpd

Output:

  • active
  • inactive
  • failed

1️⃣2️⃣ View Service Logs (AWS Debugging)

journalctl -u httpd

Last logs only:

journalctl -u httpd -n 20

Live logs:

journalctl -u httpd -f

πŸ“Œ Used when:

  • Service fails to start
  • Port issues
  • Permission issues

1️⃣3️⃣ Common AWS Services & Names

ServiceName
Apachehttpd
Nginxnginx
Dockerdocker
SSHsshd
Croncrond

πŸ“Œ Service name β‰  package name sometimes.


1️⃣4️⃣ Real AWS EC2 Examples πŸ”₯

βœ… Install & run Apache (Amazon Linux)

sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd

βœ… After config change

sudo vi /etc/httpd/conf/httpd.conf
sudo systemctl restart httpd

βœ… Debug service failure

systemctl status httpd
journalctl -u httpd

1️⃣5️⃣ User-Data Script Example (AWS)

#!/bin/bash
yum install httpd -y
systemctl start httpd
systemctl enable httpd

πŸ“Œ systemctl works perfectly in user-data


1️⃣6️⃣ systemd Files (High Level – Exam Only)

Service files location:

/etc/systemd/system/
/usr/lib/systemd/system/

Example file:

httpd.service

You usually DON’T edit these in AWS, just manage them.


1️⃣7️⃣ systemd vs init (Interview / Exam)

initsystemd
OldModern
SlowFast
No dependency mgmtDependency aware

🧠 One-line Rule

Install β†’ Start β†’ Enable β†’ Check Status

🧩 Service States

StateMeaning
active (running)Service is running
inactiveNot running
failedCrashed
enabledStarts at boot
disabledManual start

βš™οΈ Anatomy of a Service File

Location:

/lib/systemd/system/

or

/etc/systemd/system/

Example: myapp.service

[Unit]
Description=My Custom App
After=network.target

[Service]
ExecStart=/usr/bin/python3 /app/main.py
Restart=always
User=ec2-user

[Install]
WantedBy=multi-user.target

🧠 Key Sections Explained

[Unit]

  • Dependencies
  • Startup order

[Service]

  • Command to run
  • Restart policy
  • User/group

[Install]

  • Boot target

πŸ” Reload systemd (IMPORTANT)

After editing service file:

sudo systemctl daemon-reload
sudo systemctl restart myapp

⏱ systemd Timers (cron replacement)

Timer example

[Timer]
OnBootSec=5min
OnUnitActiveSec=1h

Enable timer:

sudo systemctl enable backup.timer
sudo systemctl start backup.timer

🧯 Common systemd Problems

ProblemCause
Service fails to startWrong path
Permission deniedWrong User
Starts then stopsExecStart exits
Not starting at bootNot enabled

Debug:

journalctl -xe

πŸ§ͺ Common Admin Commands

πŸ”Ή 1️⃣ systemctl reboot

systemctl reboot

What it does

πŸ‘‰ Reboots (restarts) the entire system

It is the same as:

  • Turning the server OFF
  • Then turning it ON again

What happens internally

  • All running services are stopped safely
  • Files are synced to disk
  • Kernel restarts
  • System boots again

When to use

  • After kernel updates
  • System stuck or unstable
  • Configuration changes that need reboot

⚠️ Warning

  • SSH connection will be lost
  • On EC2 β†’ instance restarts

πŸ”Ή 2️⃣ systemctl poweroff

systemctl poweroff

What it does

πŸ‘‰ Shuts down the system completely

Think of it as:

β€œTurn off the server”

What happens

  • Services stop
  • System shuts down
  • Power is off

On AWS EC2

⚠️ Important:

  • Instance goes to stopped state
  • You must start it again manually
  • Public IP may change (if not elastic IP)

When to use

  • Maintenance
  • Cost saving (EC2)
  • Permanent shutdown

πŸ”Ή 3️⃣ systemctl daemon-reexec

systemctl daemon-reexec

This one is special 🧠 (often confusing)

What it does

πŸ‘‰ Restarts systemd itself, NOT the system

  • Reloads the systemd binary
  • Keeps services running
  • Does NOT reboot

Why is this needed?

Used when:

  • systemd package is updated
  • systemd configuration changes
  • systemctl behaves strangely

What does NOT happen

❌ No reboot ❌ No shutdown ❌ No service stop


πŸ§ͺ Simple Comparison Table

CommandEffect
systemctl rebootRestart entire system
systemctl poweroffShutdown system
systemctl daemon-reexecRestart systemd only

⚠️ Permissions

All these commands need root access:

sudo systemctl reboot
sudo systemctl poweroff
sudo systemctl daemon-reexec

🎯 Interview-Ready Q&A

Q: What is systemd?

systemd is a system and service manager used in modern Linux distributions that initializes the system and manages services.

Q: What is PID 1?

The first process started by the kernel, responsible for starting all other services.

Q: Difference between reload & restart?

  • Reload: re-read config
  • Restart: stop and start

🧾 Must-Know Commands Cheat Sheet

systemctl status
systemctl start
systemctl stop
systemctl restart
systemctl enable
systemctl disable
journalctl

🏁 Final Summary

systemd is the backbone of modern Linux service management, controlling how services start, stop, restart, log, and behave during system boot and runtime.


Environment Variables & Shell

1️⃣ What are Environment Variables?

Environment variables are: πŸ‘‰ Key–value pairs used by the system and applications.

Example:

JAVA_HOME=/usr/lib/jvm/java-17
PORT=8080
DB_HOST=localhost

πŸ‘‰ In AWS, environment variables are used everywhere.


2️⃣ Why Environment Variables are Important in AWS?

Used for:

  • App configuration
  • Database credentials
  • Ports
  • AWS SDK settings
  • CI/CD pipelines

πŸ‘‰ Avoid hard-coding values in code.


3️⃣ Viewing Environment Variables

View all

env

or

printenv

View specific variable

echo $HOME
echo $PATH

4️⃣ PATH Variable (VERY IMPORTANT)

PATH tells Linux where to find commands.

Check PATH:

echo $PATH

Example output:

/usr/local/bin:/usr/bin:/bin

πŸ‘‰ If a command is not in PATH β†’ β€œcommand not found”.


5️⃣ Setting Environment Variables (Temporary)

export APP_ENV=prod
export PORT=8080

Valid only:

  • For current session
  • Until terminal is closed

6️⃣ Setting Environment Variables (Permanent)

For current user

Edit:

nano ~/.bashrc

Add:

export APP_ENV=prod
export JAVA_HOME=/usr/lib/jvm/java-17

Apply changes:

source ~/.bashrc

System-wide (All users)

Edit:

sudo nano /etc/environment

Example:

APP_ENV=prod

7️⃣ Shell Configuration Files

Important shell files:

FilePurpose
~/.bashrcUser shell config
~/.bash_profileLogin shell
/etc/profileSystem-wide
/etc/environmentGlobal variables

πŸ‘‰ AWS EC2 user config is usually in .bashrc.


8️⃣ Using Environment Variables in Apps

Example:

echo $DB_HOST

In shell script:

echo "Running in $APP_ENV mode"

9️⃣ AWS Real-Life Example

Java App on EC2

export DB_URL=jdbc:mysql://localhost:3306/appdb
export DB_USER=admin
export DB_PASS=secret

App reads these values at runtime.


πŸ”Ÿ Common AWS Problems

❌ App not picking config βœ” Check env variable βœ” Check .bashrc βœ” Restart service


1️⃣1️⃣ What you MUST remember

βœ… Environment variables store config βœ… PATH controls command access βœ… export sets variables βœ… .bashrc is very important βœ… Restart shell or service to apply changes


Searching & Text Processing (Linux)

πŸ” What is Searching & Text Processing?

Searching & Text Processing means:

  • Finding files
  • Searching text inside files
  • Filtering output
  • Modifying text streams
  • Analyzing logs & data

πŸ‘‰ In simple words:

It is how Linux reads, searches, filters, and transforms text efficiently

Linux treats almost everything as text (logs, configs, outputs).


🧱 Core Philosophy (VERY IMPORTANT)

Linux tools follow:

Do one thing and do it well

Then you combine tools using pipes (|)

Example:

cat log.txt | grep ERROR | sort | uniq -c

πŸ“ Searching Files (find & locate)

find /var/log -name "*.log"

By size:

find / -size +100M

By modified time:

find /home -mtime -1

Delete files:

find /tmp -type f -name "*.tmp" -delete

locate nginx.conf

Update index:

sudo updatedb

⚠️ May not find newly created files until index updates


πŸ“„ Searching Text Inside Files (grep)

grep "error" app.log

Case-insensitive:

grep -i error app.log

Recursive:

grep -R "password" /etc

Line numbers:

grep -n "failed" auth.log

Invert match:

grep -v "INFO" app.log

πŸ” Regular Expressions (Regex Basics)

grep "^ERROR" app.log   ## line starts with ERROR
grep "failed$" app.log  ## line ends with failed
grep "[0-9]\{3\}" file  ## 3 digits

πŸ§ͺ Filtering & Transforming Text

πŸ”Ή cut – Column extraction

cut -d: -f1 /etc/passwd

πŸ”Ή awk – Powerful text processing

awk '{print $1, $3}' file.txt

Condition:

awk '$3 > 100 {print $1}' data.txt

Sum column:

awk '{sum+=$2} END {print sum}' sales.txt

πŸ”Ή sed – Stream editor (modify text)

Replace:

sed 's/error/ERROR/g' app.log

Edit file:

sed -i 's/8080/9090/g' server.conf

Delete lines:

sed '5d' file.txt

πŸ“Š Sorting & Counting

πŸ”Ή sort

sort file.txt
sort -n numbers.txt
sort -r file.txt

πŸ”Ή uniq

uniq file.txt
sort file.txt | uniq
sort file.txt | uniq -c

πŸ”Ή wc – Word count

wc file.txt
wc -l file.txt

πŸ”— Pipes (|) – Power of Linux

grep ERROR app.log | wc -l
ps aux | grep nginx

πŸ“‚ File Content Viewing

CommandUse
catShow entire file
lessScrollable view
headFirst lines
tailLast lines

Live logs:

tail -f app.log

🧯 Real-World Use Cases

πŸ”₯ Find errors in logs

grep -i error /var/log/syslog

πŸ”₯ Top IPs hitting server

awk '{print $1}' access.log | sort | uniq -c | sort -nr | head

πŸ”₯ Kill process using port

netstat -tulpn | grep 8080

🧠 Common Mistakes

MistakeFix
Using cat unnecessarilyUse grep directly
Not sorting before uniqAlways sort
Forgetting quotesQuote patterns
Using locate for live searchUse find

🎯 Interview-Ready Questions

Q: Difference between grep & find?

  • grep β†’ search text
  • find β†’ search files

Q: Why awk is powerful?

  • It processes structured text & columns

Q: What is a pipe?

  • Connects output of one command to input of another

🧾 Must-Know Commands Cheat Sheet

find
locate
grep
awk
sed
cut
sort
uniq
wc
head
tail

🏁 Final Summary

Searching & text processing tools allow Linux users to quickly find information, analyze logs, filter outputs, and automate data handling using simple yet powerful commands.


Compression & Archiving (Linux)

1️⃣ What is Archiving vs Compression?

Archiving

  • Combines multiple files into one
  • Example: .tar

Compression

  • Reduces file size
  • Example: .gz, .zip

πŸ‘‰ Very often used together:

.tar.gz

2️⃣ tar – Archiving Tool (MOST IMPORTANT)

Create archive

tar -cvf backup.tar folder/

Options:

  • c β†’ create
  • v β†’ verbose
  • f β†’ file name

Extract archive

tar -xvf backup.tar

3️⃣ Compressed Archives (tar.gz)

Create compressed archive

tar -czvf backup.tar.gz folder/

Extract compressed archive

tar -xzvf backup.tar.gz

4️⃣ gzip & gunzip

Compress file

gzip file.log

Creates:

file.log.gz

Decompress

gunzip file.log.gz

5️⃣ zip & unzip

Create zip

zip -r backup.zip folder/

Extract zip

unzip backup.zip

πŸ‘‰ zip is useful when sharing files with Windows users.


6️⃣ Backup Basics (AWS Context)

Common backup targets:

  • Application files
  • Logs
  • Database dumps

Example:

tar -czvf app-backup.tar.gz /opt/app

Upload to S3:

aws s3 cp app-backup.tar.gz s3://my-bucket/

7️⃣ AWS Real-Life Example

Before EC2 termination

tar -czvf logs-backup.tar.gz /var/log

8️⃣ Common Mistakes

❌ Forgetting f option ❌ Extracting in wrong directory ❌ Overwriting files


9️⃣ What you MUST remember

βœ… tar is most important βœ… .tar.gz is very common βœ… zip for cross-platform βœ… Backups are essential in AWS βœ… Always verify archive


SSH & Remote Access

πŸ” What is SSH?

SSH (Secure Shell) is a protocol used to:

  • Connect to remote servers securely
  • Execute commands remotely
  • Transfer files safely
  • Manage servers over the network

πŸ‘‰ In simple words:

SSH lets you control another computer securely from your terminal


🌍 Why SSH is Important

Without SSH:

  • Passwords sent in plain text
  • Easy hacking

With SSH:

  • Encrypted communication
  • Secure authentication
  • Industry standard for server access

🧠 How SSH Works (Concept)

  1. Client sends connection request
  2. Server responds with public key
  3. Client verifies server
  4. Authentication happens (password or key)
  5. Encrypted session starts

πŸ–₯ SSH Basic Command

ssh user@server_ip

Example:

ssh ec2-user@13.233.45.10

πŸ”‘ SSH Authentication Methods

1️⃣ Password Authentication

ssh user@server

❌ Less secure


2️⃣ Key-Based Authentication (BEST PRACTICE)

Generate key

ssh-keygen

Copy key to server

ssh-copy-id user@server_ip

Or manually:

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

πŸ” SSH Key Files

FilePurpose
id_rsaPrivate key (never share)
id_rsa.pubPublic key
authorized_keysAllowed keys
known_hostsTrusted servers

πŸ”’ File Permissions (VERY IMPORTANT)

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Wrong permissions = SSH login fails ❌


πŸšͺ SSH Port & Config

Default port: 22

Config file:

/etc/ssh/sshd_config

Common settings:

PermitRootLogin no
PasswordAuthentication no
Port 2222

Restart SSH:

sudo systemctl restart sshd

πŸ“ File Transfer with SSH

πŸ”Ή SCP

scp file.txt user@server:/path

Download:

scp user@server:/path/file.txt .

πŸ”Ή RSYNC (Preferred)

rsync -avz file.txt user@server:/path

πŸ”„ SSH Tunneling & Port Forwarding

Local Port Forwarding

ssh -L 8080:localhost:80 user@server

Access remote port locally


Remote Command Execution

ssh user@server "df -h"

🧯 Common SSH Errors & Fixes

ErrorFix
Permission deniedCheck key & permissions
Connection refusedSSH not running
Host key changedRemove from known_hosts
TimeoutFirewall / port blocked

Fix host key:

ssh-keygen -R server_ip

☁️ SSH in AWS EC2 (Real-World)

Connect:

ssh -i mykey.pem ec2-user@ec2-ip

Permissions:

chmod 400 mykey.pem

πŸ” SSH Security Best Practices

βœ” Use key-based auth βœ” Disable root login βœ” Change default port βœ” Use firewall βœ” Use fail2ban


πŸ§ͺ Advanced SSH Options

ssh -v user@server
ssh -i key.pem user@server
ssh -o StrictHostKeyChecking=no user@server

πŸ” SSH Config File (Client Side)

~/.ssh/config

Example:

Host myserver
  HostName 13.233.45.10
  User ec2-user
  IdentityFile ~/.ssh/mykey.pem

Connect:

ssh myserver

🎯 Interview-Ready Q&A

Q: What is SSH?

SSH is a secure protocol used for remote access and command execution over encrypted connections.

Q: Password vs Key authentication?

  • Password β†’ weaker
  • Key β†’ stronger & secure

Q: What is known_hosts?

Stores server public keys to prevent MITM attacks


🧾 Must-Know Commands Cheat Sheet

ssh
ssh-keygen
ssh-copy-id
scp
rsync
chmod

🏁 Final Summary

SSH is the backbone of secure remote server management, enabling encrypted access, file transfers, and automation across Linux and cloud systems.


Bash Scripting Basics

🐚 What is Bash?

Bash (Bourne Again Shell) is:

  • A command-line shell
  • A scripting language
  • The default shell on most Linux systems

πŸ‘‰ In simple words:

Bash lets you automate tasks by writing commands in a file


πŸ“œ What is a Bash Script?

A Bash script is:

  • A text file
  • Contains Linux commands
  • Executed line by line

File extension:

.sh

Example:

backup.sh

🧱 Structure of a Bash Script

1️⃣ Shebang (VERY IMPORTANT)

#!/bin/bash

Tells system which interpreter to use


2️⃣ Commands

echo "Hello World"

▢️ Running a Bash Script

Make executable

chmod +x script.sh

Run

./script.sh

OR

bash script.sh

πŸ”£ Variables in Bash

Declare variable

name="Dev"

Use variable

echo "Hello $name"

⚠️ No space around =


πŸ“₯ User Input

read -p "Enter name: " name
echo "Hello $name"

πŸ” Conditional Statements

if-else

if [ $age -gt 18 ]; then
  echo "Adult"
else
  echo "Minor"
fi

Common operators:

  • -eq β†’ equal
  • -ne β†’ not equal
  • -gt β†’ greater than
  • -lt β†’ less than

πŸ”„ Loops

for loop

for i in 1 2 3
do
  echo $i
done

while loop

while true
do
  echo "Running..."
  sleep 1
done

🧠 Functions

greet() {
  echo "Hello $1"
}

greet Dev

πŸ“ File & Directory Checks

if [ -f file.txt ]; then
  echo "File exists"
fi
if [ -d /var/log ]; then
  echo "Directory exists"
fi

πŸ”Ž Command Line Arguments

echo "Script name: $0"
echo "First arg: $1"
echo "All args: $@"

Run:

./script.sh arg1 arg2

πŸ”„ Exit Status

ls /tmp
echo $?

0 β†’ success Non-zero β†’ error


πŸ”— Pipes & Redirection

Redirect output

ls > file.txt

Append

ls >> file.txt

Redirect error

ls invalid 2> error.txt

πŸ§ͺ Debugging Bash Scripts

bash -x script.sh

Inside script:

set -x

Stop on error:

set -e

🧯 Common Mistakes

MistakeFix
Missing shebangAdd #!/bin/bash
Spaces in variablevar=value
Forget chmodchmod +x
Using wrong quotesUse "

☁️ Real-World Use Case

Backup Script Example

#!/bin/bash
tar -czf backup.tar.gz /home/dev
echo "Backup completed"

🎯 Interview-Ready Questions

Q: What is Bash?

Bash is a Unix shell and scripting language used to automate system tasks.

Q: Difference between $@ and $*?

  • $@ β†’ separate arguments
  • $* β†’ single string

Q: What is shebang?

It specifies the interpreter for the script.


🧾 Must-Know Bash Commands

echo
read
if
for
while
case
function

🏁 Final Summary

Bash scripting allows you to automate repetitive Linux tasks, manage servers efficiently, and build powerful command-line tools using simple scripts.


Linux Security Basics

1️⃣ Firewall Basics

A firewall controls: πŸ‘‰ Which traffic is allowed or blocked.

Linux firewalls:

  • iptables (low-level)
  • firewalld
  • ufw (Ubuntu)

Check firewall status

Ubuntu:

sudo ufw status

Amazon Linux:

sudo systemctl status firewalld

πŸ‘‰ In AWS, Security Groups act as the main firewall, but Linux firewall still matters.


2️⃣ File Permission Security

Permissions prevent:

  • Unauthorized access
  • Accidental deletion
  • Data leaks

Best practices:

  • No 777 permissions
  • Use 755 for folders
  • Use 644 for files

Example:

chmod 600 id_rsa

3️⃣ SSH Security (VERY IMPORTANT)

Best practices:

  • Use key-based authentication
  • Disable password login
  • Avoid root login

SSH config file:

/etc/ssh/sshd_config

Important settings:

PermitRootLogin no
PasswordAuthentication no

Restart SSH:

sudo systemctl restart sshd

4️⃣ Running Services Securely

Never run apps as root ❌

Run as:

  • nginx
  • appuser
  • docker

Check service user:

ps aux | grep nginx

5️⃣ Principle of Least Privilege

Give:

  • Only required access
  • Only required permissions

Example:

  • App user β†’ app folder only
  • Log user β†’ logs only

6️⃣ AWS Security Context

LayerSecurity
AWSSecurity Groups, IAM
LinuxUsers, permissions, firewall
AppAuthentication, authorization

πŸ‘‰ All layers matter.


7️⃣ Common AWS Security Mistakes

❌ SSH open to 0.0.0.0/0 ❌ Root login enabled ❌ World-writable files ❌ Hard-coded secrets


8️⃣ AWS Real-Life Example

Secure SSH:

chmod 400 mykey.pem
sudo nano /etc/ssh/sshd_config
sudo systemctl restart sshd

9️⃣ What you MUST remember

βœ… Security is layered βœ… Use key-based SSH βœ… Restrict file permissions βœ… Don’t run apps as root βœ… AWS + Linux security together


Logs & Troubleshooting (Linux)

1️⃣ What are Logs?

Logs are files that record:

  • System activity
  • Errors
  • Application events

πŸ‘‰ Logs tell you what went wrong and why.


2️⃣ System Logs

System logs show:

  • Boot issues
  • Service failures
  • OS-level errors

Common locations:

LogPurpose
/var/log/messagesSystem events (Amazon Linux)
/var/log/syslogSystem logs (Ubuntu)
/var/log/dmesgKernel messages
/var/log/secureSecurity & SSH logs

View logs:

less /var/log/messages

3️⃣ Application Logs

Each application has its own logs.

Examples:

  • Nginx:

    /var/log/nginx/access.log
    /var/log/nginx/error.log
    
  • Docker:

    docker logs container_id
    
  • Java App:

    app.log
    

4️⃣ Reading Error Logs (MOST IMPORTANT)

View last errors

tail -n 50 error.log

Live error tracking

tail -f error.log

Search errors

grep "ERROR" error.log

5️⃣ Service Logs (systemd)

journalctl -u nginx
journalctl -u nginx -f

πŸ‘‰ Used when service fails to start.


6️⃣ Basic Troubleshooting Flow (VERY IMPORTANT)

When something breaks:

Step 1: Check service

systemctl status nginx

Step 2: Check logs

journalctl -u nginx

Step 3: Check port

ss -tulnp

Step 4: Test locally

curl localhost

Step 5: Check permissions

ls -l

Step 6: Check disk

df -h

7️⃣ AWS Real-Life Example

❌ Website not loading

Checklist:

  • Is service running?
  • Is port open?
  • Is app listening?
  • Are logs showing errors?
  • Is Security Group correct?

8️⃣ Common AWS Errors & Logs

ErrorCheck
403 ForbiddenFile permissions
502 Bad GatewayApp crash
Connection refusedService down
Disk fulldf -h

9️⃣ What you MUST remember

βœ… Logs explain failures βœ… Always check logs first βœ… tail -f for live issues βœ… Follow troubleshooting steps βœ… AWS issues = Linux + AWS configs


Linux for Cloud (AWS Context)

☁️ What Does β€œLinux for Cloud (AWS)” Mean?

It means:

Using Linux to run, manage, secure, and scale cloud resources on AWS

In AWS:

  • Most servers (EC2) run Linux
  • Most services assume Linux knowledge
  • Cloud automation = Linux + scripting

πŸ‘‰ If you know Linux well, AWS becomes easy


🧠 Why Linux Is So Important in AWS?

ReasonExplanation
EC2 runs LinuxAmazon Linux, Ubuntu, RHEL
CostLinux is free / cheaper
AutomationBash, cron, systemd
SecuritySSH, permissions, firewall
PerformanceLightweight & stable

πŸ–₯ Linux in AWS = EC2

EC2 = Virtual Linux Server

Popular Linux AMIs:

  • Amazon Linux 2 / Amazon Linux 2023
  • Ubuntu 20.04 / 22.04
  • RHEL
  • CentOS (legacy)

πŸ”‘ Accessing Linux EC2 (SSH)

ssh -i key.pem ec2-user@EC2_PUBLIC_IP

Permissions:

chmod 400 key.pem

Default users:

OSUser
Amazon Linuxec2-user
Ubuntuubuntu
RHELec2-user

πŸ“ Linux Filesystem in Cloud Servers

Important directories:

PathUse
/Root
/home/ec2-userUser data
/var/logLogs
/etcConfig files
/optCustom apps

Cloud admins live inside /var and /etc


πŸ“¦ Package Management (Cloud Must-Know)

Amazon Linux:

sudo yum install nginx

Ubuntu:

sudo apt install nginx

Update system:

sudo yum update -y

πŸ”§ Service Management (Production Critical)

AWS apps run as services

sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx

Logs:

journalctl -u nginx

πŸ“Š Monitoring & Logs (Very Important in Cloud)

Check system usage:

top
htop
free -m
df -h

View logs:

tail -f /var/log/messages
tail -f /var/log/syslog

AWS CloudWatch often reads these logs.


πŸ’Ύ Storage in AWS (Linux View)

AWS Storage β†’ Linux Disk

AWSLinux
EBS Volume/dev/xvda, /dev/nvme0n1
S3Mounted via tools
Instance StoreTemporary disk

Check disks:

lsblk
df -h

Mount EBS:

mount /dev/xvdf /data

πŸ” Linux Security in AWS

πŸ”Ή SSH Security

  • Key-based auth
  • Disable root login
  • Change SSH port

πŸ”Ή Permissions

chmod 600 key.pem
chown ec2-user file

πŸ”Ή Firewall

  • Security Groups (AWS)
  • iptables / firewalld (Linux)

🌐 Networking (Linux + AWS)

Check IP:

ip a

Check ports:

ss -tuln

Test connectivity:

curl localhost
ping google.com

AWS Networking concepts mapped to Linux:

AWSLinux
Security GroupFirewall rules
ENINetwork interface
ALBReverse proxy

πŸ§ͺ Automation with Bash (Cloud Core Skill)

Example startup script:

#!/bin/bash
yum install -y nginx
systemctl start nginx
systemctl enable nginx

Used in:

  • EC2 User Data
  • CI/CD pipelines
  • Auto Scaling

πŸ”„ Scaling & Linux

In Auto Scaling:

  • Linux boots
  • User-data runs
  • App starts automatically

Your Linux setup must be:

  • Idempotent
  • Fast
  • Error-free

☁️ Linux + AWS Services Mapping

AWS ServiceLinux Role
EC2OS & apps
ECS/EKSContainers on Linux
LambdaLinux runtime
CloudWatchLinux logs
CodeDeployLinux deployments

🧯 Common Cloud Linux Problems

ProblemCause
SSH timeoutSG / NACL
Disk fullLogs
App stoppedsystemd
Permission deniedchmod/chown
High CPUrunaway process

🎯 Interview-Ready Q&A

Q: Why Linux is preferred in AWS?

Linux is lightweight, secure, open-source, and well suited for cloud scalability and automation.

Q: How do you secure EC2?

  • SSH keys
  • Security Groups
  • OS hardening
  • Patch updates

Q: What is user-data?

A script executed at first boot to configure the Linux instance.


🧾 Must-Know Linux Commands for AWS

ssh
lsblk
df -h
top
systemctl
journalctl
curl
chmod
chown

🏁 Final Summary (Very Important)

Linux is the backbone of AWS cloud computing. Mastering Linux means you can deploy, secure, scale, and troubleshoot cloud infrastructure confidently.


🐧 Linux Basics You MUST Know Before AWS

Think of Linux commands in 8 core groups.


1️⃣ File & Directory Commands (MOST USED)

πŸ“‚ pwd

pwd

Shows current directory

πŸ“Œ Used when navigating EC2.


πŸ“‚ ls

ls
ls -l
ls -la
OptionMeaning
-llong listing
-ashow hidden files

πŸ“Œ Used all the time.


πŸ“‚ cd

cd /var/log
cd ~
cd ..

πŸ“Œ Move between directories.


πŸ“„ touch

touch file.txt

Creates empty file.


πŸ“„ cat

cat file.txt

Show file content.


πŸ“„ less (IMPORTANT)

less file.txt

Scroll large files (logs).

Keys:

  • q β†’ quit
  • / β†’ search

πŸ“„ head / tail

head file.txt
tail file.txt
tail -f /var/log/messages

πŸ“Œ tail -f = live logs (AWS debugging).


πŸ“‚ mkdir

mkdir logs
mkdir -p app/data

πŸ“‚ rm

rm file.txt
rm -r folder
rm -rf folder

⚠ Dangerous:

  • -r β†’ recursive
  • -f β†’ force

πŸ“‚ cp

cp file1 file2
cp -r dir1 dir2

πŸ“‚ mv

mv old.txt new.txt
mv file.txt /tmp/

Rename OR move.


2️⃣ File Permissions & Ownership (AWS CRITICAL)

πŸ” chmod

chmod +x script.sh
chmod 755 script.sh

πŸ“Œ Used for:

  • Scripts
  • User-data
  • Deployments

πŸ‘€ chown

sudo chown ec2-user:ec2-user file.txt

πŸ“Œ Fix permission errors in EC2.


πŸ‘€ whoami

whoami

Shows current user.


πŸ‘€ id

id

Shows UID, GID.


3️⃣ User & Privilege Commands

πŸ”‘ sudo

sudo yum install nginx

πŸ“Œ Required for:

  • Installing packages
  • Starting services
  • Editing system files

πŸ‘₯ su

su -

Switch to root (not recommended often).


4️⃣ Package Management (Already Covered – Quick List)

Amazon Linux

yum install
yum remove
yum update
dnf install

Ubuntu

apt update
apt install
apt remove

πŸ“Œ Used in user-data scripts.


5️⃣ Service Management (systemd)

πŸ”§ systemctl

systemctl start httpd
systemctl stop httpd
systemctl restart httpd
systemctl enable httpd
systemctl status httpd

πŸ“Œ AWS = services must survive reboot.


πŸ“œ journalctl

journalctl -u httpd
journalctl -u httpd -f

πŸ“Œ Debug EC2 issues.


6️⃣ Networking Commands (AWS IMPORTANT)

🌐 ip

ip a

Shows IP address.


🌐 ss

ss -tuln

Shows listening ports.


🌐 curl

curl http://localhost

πŸ“Œ Test web server.


🌐 ping

ping google.com

πŸ“Œ Check connectivity.


🌐 netstat (older)

netstat -tuln

7️⃣ Process Management

βš™ ps

ps aux

βš™ top

top

πŸ“Œ Monitor CPU & memory.


βš™ kill

kill PID
kill -9 PID

πŸ“Œ Stop stuck processes.


8️⃣ Disk & Memory Commands

πŸ’Ύ df β€” Disk Free (EBS usage)

df -h

What df means

  • df β†’ Disk Free
  • shows disk space per filesystem

-h flag

  • -h β†’ human-readable
  • Shows sizes in GB / MB instead of bytes

πŸ’Ύ du β€” Directory size

du -sh /var/log # shows size of /var/log

What du means

  • du β†’ Disk Usage
  • Shows how much space files/directories consume

Flags explained

  • -s β†’ summary only (not every file)
  • -h β†’ human-readable

πŸ’Ύ free β€” RAM usage

free -m 
  • Shows memory (RAM) usage
  • -m flag:Displays values in MB

πŸ’Ύ lsblk β€” List block devices (EBS volumes)

lsblk   # lists all block devices

πŸ“Œ Used when attaching EBS volume.


9️⃣ Search & Text Processing (Logs)

πŸ” grep

grep error app.log
grep -i error app.log

πŸ“Œ MOST used for logs.


πŸ” find

find / -name app.log

πŸ”Ÿ SSH & Remote Access

πŸ” ssh

ssh -i key.pem ec2-user@IP

πŸ” scp

scp file.txt ec2-user@IP:/home/ec2-user/

1️⃣1️⃣ Environment & Shell Basics

🌱 env

env

🌱 export

export APP_ENV=prod

πŸ“Œ Used in deployments.


🌱 echo

echo $APP_ENV

1️⃣2️⃣ AWS User-Data Script Essentials

#!/bin/bash
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd

πŸ“Œ Requires:

  • chmod +x
  • Correct commands

πŸ‘ˆ

πŸ“ Nano Editor (Beginner Friendly)

What is Nano?

Nano is a terminal-based text editor in Linux.

πŸ“Œ Think of Nano like:

  • Notepad, but inside the terminal
  • Very easy compared to vi / vim

How to open Nano

Open a file (or create if not exists)

nano file.txt
  • If file.txt exists β†’ it opens
  • If not β†’ Nano creates it

Nano screen layout

When Nano opens, you’ll see:

  • Top β†’ File name
  • Middle β†’ Text area (where you type)
  • Bottom β†’ Shortcut keys

Example at bottom:

^G Get Help   ^O Write Out   ^X Exit

πŸ‘‰ ^ means CTRL

So:

  • ^X = CTRL + X
  • ^O = CTRL + O

MOST IMPORTANT Nano commands ⭐

1️⃣ Save file

CTRL + O
  • Write Out (save)
  • Press Enter to confirm file name

2️⃣ Exit Nano

CTRL + X

If file is not saved:

  • Nano will ask β†’ Save? (Y/N)

3️⃣ Cut a line

CTRL + K
  • Cuts (removes) the current line
  • Works like cut, not copy

4️⃣ Paste a line

CTRL + U
  • Pastes the last cut line

5️⃣ Search text

CTRL + W
  • Search for a word in the file

6️⃣ Replace text

CTRL + \
  • Search and replace

CommandAction
Arrow keysMove cursor
CTRL + AStart of line
CTRL + EEnd of line
CTRL + YPage up
CTRL + VPage down

Selecting text (Mark mode)

Start selection

CTRL + ^

(or CTRL + SHIFT + 6)

  • Move cursor to select text

Cut selected text

CTRL + K

Undo / Redo

ActionCommand
UndoALT + U
RedoALT + E

Show line numbers (very useful)

Inside Nano:

ALT + N

Or open with line numbers:

nano -l file.txt

Help inside Nano πŸ†˜

CTRL + G

Shows:

  • All commands
  • Key combinations

Real-world usage examples (VERY COMMON)

Edit config file

nano /etc/nginx/nginx.conf

Edit app config

nano application.properties

Edit crontab file

crontab -e

(Default editor is often Nano)


Why Nano is beginner-friendly ❀️

βœ” No modes (unlike vim) βœ” Commands always visible βœ” Easy save & exit βœ” Simple shortcuts


Quick cheat sheet πŸ“Œ

TaskCommand
Open filenano file.txt
SaveCTRL + O
ExitCTRL + X
Cut lineCTRL + K
PasteCTRL + U
SearchCTRL + W
ReplaceCTRL + \
HelpCTRL + G

πŸ‘ˆ

πŸ“ VI Editor (Powerful but Confusing at First)

What is vi?

vi is a terminal-based text editor in Linux/Unix systems.

  • Old
  • Extremely powerful
  • Available on almost every Linux server
  • Default editor on many servers (EC2, production machines)

πŸ“Œ Important:

Even if Nano is easier, you MUST know vi Because sometimes Nano is not installed, but vi always is.


Why vi feels difficult for beginners 😡

Because vi has MODES.

Nano β†’ type directly Vi β†’ depends on which mode you are in

πŸ‘‰ This is the most important concept.


πŸ” VI MODES (MOST IMPORTANT PART)

vi works in three main modes:

ModePurpose
Normal modeNavigation + commands
Insert modeTyping text
Command modeSave, quit, search

When you open vi, you are NOT in typing mode.

You start in Normal mode ❗


1️⃣ Normal Mode (Default mode)

This is the command mode for movement and actions.

  • You cannot type text
  • Every key is treated as a command

Examples:

  • dd β†’ delete line
  • yy β†’ copy line
  • p β†’ paste
  • gg β†’ go to top

πŸ“Œ If something weird happens β†’ You are probably in Normal mode πŸ˜„


2️⃣ Insert Mode (Typing mode)

This is where you can type text normally.

To enter Insert mode, press:

KeyMeaning
iInsert before cursor
aAppend after cursor
oNew line below
ONew line above

When Insert mode is active:

  • Bottom shows: -- INSERT --

To exit Insert mode:

ESC

πŸ‘‰ ESC is your best friend in vi.


3️⃣ Command Mode (Colon : mode)

Used for:

  • Save
  • Quit
  • Search
  • Replace
  • Line numbers

You enter command mode by typing:

:

(while in Normal mode)

Example:

:w
:q
:wq

πŸš€ Opening a file in vi

vi file.txt

If file doesn’t exist:

  • It will be created when you save

✍️ BASIC WORKFLOW (VERY IMPORTANT)

This is how 90% of people use vi:

  1. Open file
  2. Press i
  3. Type text
  4. Press ESC
  5. Save and quit

Let’s see commands.


πŸ’Ύ Save and Exit Commands (MUST KNOW)

CommandMeaning
:wSave (write)
:qQuit
:wqSave and quit
:xSave and quit
:q!Quit without saving (force)
:w!Force save

πŸ“Œ Examples:

Save only:

:w

Save and exit:

:wq

Exit without saving:

:q!

🧭 MOVEMENT (Navigation) – Core vi skill

You can use arrow keys, BUT real vi users use:

KeyAction
hLeft
lRight
jDown
kUp

Why?

  • Faster
  • Hands stay on keyboard

Line & file movement

CommandMeaning
0Start of line
$End of line
ggGo to top
GGo to bottom
:10Go to line 10

βœ‚οΈ DELETE, COPY, PASTE (VERY IMPORTANT)

Delete

CommandAction
ddDelete current line
dwDelete word
d$Delete till end of line
xDelete one character

πŸ“Œ Delete = cut (stored in buffer)


Copy (Yank)

CommandAction
yyCopy current line
ywCopy word
y$Copy till end

Paste

CommandAction
pPaste below cursor
PPaste above cursor

πŸ” Undo and Redo

ActionCommand
Undou
RedoCTRL + R

πŸ“Œ Undo is life saver.


πŸ” SEARCHING (Very useful)

Search forward

/word

Search backward

?word

Press:

  • n β†’ next match
  • N β†’ previous match

πŸ”„ SEARCH & REPLACE (POWER FEATURE)

Format:

:s/old/new/

Examples:

Replace first match in line:

:s/java/spring/

Replace all in line:

:s/java/spring/g

Replace in entire file:

:%s/java/spring/g

Replace with confirmation:

:%s/java/spring/gc

πŸ”’ Line Numbers (VERY IMPORTANT IN DEBUGGING)

Show line numbers:

:set number

Hide line numbers:

:set nonumber

Shortcut:

:set nu

πŸ“Œ VISUAL MODE (Text selection)

Enter Visual mode:

v
  • Move cursor to select text

Commands after selection:

  • d β†’ delete
  • y β†’ copy

Visual line mode:

V

βš™οΈ REAL PRODUCTION USAGE EXAMPLES

Edit config file on EC2

vi /etc/nginx/nginx.conf

Edit environment variables

vi ~/.bashrc

Edit application properties

vi application.properties

⚠️ COMMON BEGINNER MISTAKES

❌ Typing without entering Insert mode ❌ Forgetting to press ESC ❌ Not knowing how to quit ❌ Panic when screen freezes

βœ… Solution:

ESC
:q!

Always works πŸ˜„


🧠 MENTAL MODEL (IMPORTANT)

Think like this:

  • Normal mode β†’ THINK
  • Insert mode β†’ TYPE
  • Command mode β†’ CONTROL

πŸ†š VI vs NANO (Quick Comparison)

FeatureNanoVi
Beginner friendlyβœ…βŒ
Always installedβŒβœ…
Powerful editingβŒβœ…
Used in productionRareVery common

🧾 MUST-MEMORIZE COMMANDS (Cheat Sheet)

i        β†’ insert
ESC      β†’ normal mode
:w       β†’ save
:q       β†’ quit
:wq      β†’ save & quit
:q!      β†’ quit force
dd       β†’ delete line
yy       β†’ copy line
p        β†’ paste
u        β†’ undo
/word    β†’ search
:set nu  β†’ line numbers

Final Advice ❀️

Don’t try to memorize everything at once.

Start with:

  1. i
  2. ESC
  3. :wq
  4. dd
  5. /search

That’s enough for real-world server work.


πŸ‘ˆ

πŸ”‘ Big Picture: EC2 Access + Linux Users (ONE CLEAR MODEL)

When you create an EC2 instance, there are two separate systems involved:

1️⃣ AWS level (IAM & Key Pair)

Controls:

  • Who can connect to the EC2
  • Uses SSH keys (.pem)

2️⃣ Linux level (Users & Groups)

Controls:

  • What a person can do inside the server
  • Uses users, groups, permissions

πŸ‘‰ These two are independent but work together.


πŸ” What the .pem file really does

  • A .pem file is NOT a user
  • It is NOT root access
  • It only proves:

β€œI am allowed to connect to this server”

When you SSH using a PEM file:

ssh -i key.pem ec2-user@IP

You log in as:

ec2-user

βœ” Not root βœ” Normal Linux user βœ” Has sudo access


πŸ‘€ Are you root after login?

❌ No

AWS disables direct root login for security. When you log in, you are a normal user Root access is temporary and controlled.


πŸ‘₯ Giving EC2 access to another person (CORRECT WAY)

You do NOT share your PEM file.

Correct process:

  1. Other person generates their own SSH key
  2. They send you only the public key
  3. You create a Linux user on EC2
  4. You add their public key to that user
  5. You set permissions for what they can access

Why AWS does NOT log you in as root

AWS disables direct root login by default.

Reasons:

  1. Security

    • Root can delete OS, disks, users
  2. Accidental damage

    • One wrong command can destroy server
  3. Industry best practice

    • Least privilege principle

So AWS says:

β€œLogin as a normal user, become root only when needed.”


πŸ” Then how can ec2-user do admin work?

Because ec2-user has sudo access.

Check:

groups ec2-user

Output:

ec2-user wheel

The wheel group allows:

sudo

πŸš€ How to access root (IMPORTANT)

There are two correct and safe ways.


βœ… Method 1: Run a single command as root

sudo command

Example:

sudo yum install nginx

What happens:

  • Only this command runs as root
  • You remain ec2-user

Check:

sudo whoami

Output:

root

βœ… Method 2: Switch to full root shell

sudo su -

OR

sudo -i

Now your prompt changes:

[root@ip-172-31-x-x ~]#

Now:

  • You are root
  • You have full control

To exit root:

exit

⚠️ Mental model (VERY IMPORTANT)

Think like this:

  • PEM file β†’ proves who you are
  • Linux user β†’ decides what you can do
  • sudo β†’ temporary root power
PEM file β‰  root

πŸ“Œ One-command summary

ActionResult
SSH with PEMLogin as normal user
whoamiec2-user
sudo commandRun as root
sudo su -Become root
Root SSH loginDisabled

βœ… Final clear statement (remember this)

When you log in using a PEM file, you are logged in as a normal Linux user (like ec2-user), not as root. AWS disables direct root login for security reasons. To perform administrative tasks, you temporarily become root using sudo.


πŸ‘ˆ

How to create a new user and how to do proper setup for that user

This is exactly how it is done on real EC2 servers.


🧠 First understand the goal

When you create a new user, you want:

βœ” The user to log in with their own SSH key βœ” The user to have their own home directory βœ” The user to access only allowed files βœ” The user to NOT be root by default


πŸ‘€ Step 1: Login as admin user

First, you (admin) log in to EC2:

ssh -i admin.pem ec2-user@EC2-IP

You are logged in as:

ec2-user

This user has sudo access.


πŸ‘€ Step 2: Create a new Linux user

Create a new user (example: devuser):

sudo useradd devuser

What this does:

  • Creates a Linux user named devuser
  • Assigns a UID (user ID)
  • Creates entry in /etc/passwd

When you create a user with useradd, no password is set.

Set password:

sudo passwd devuser

Enter a new password β†’ done βœ…

To list all users in Linux, use:

cat /etc/passwd

If you want only usernames (clean list):

cut -d: -f1 /etc/passwd

To see currently logged-in users:

who

To switch to another user in Linux:

su - username

OR no password needed, using sudo (recommended):

sudo su - username

To go back to your previous user:

exit

πŸ“‚ Step 3: Create home directory (IMPORTANT)

Some systems create it automatically, but to be safe:

sudo mkdir /home/devuser

Set ownership:

sudo chown devuser:devuser /home/devuser

Now:

/home/devuser

belongs only to devuser.


πŸ” Step 4: Create .ssh directory

This is required for SSH key login.

sudo mkdir /home/devuser/.ssh

Set correct permissions:

sudo chmod 700 /home/devuser/.ssh
sudo chown devuser:devuser /home/devuser/.ssh

Why 700?

  • Only the user can read/write/enter
  • SSH will refuse login if permissions are wrong

πŸ”‘ Step 5: Add public key (MOST IMPORTANT)

Create the authorized keys file:

sudo nano /home/devuser/.ssh/authorized_keys

Paste the public key of the user here.

⚠️ Only public key, never private key.

Set permissions:

sudo chmod 600 /home/devuser/.ssh/authorized_keys
sudo chown devuser:devuser /home/devuser/.ssh/authorized_keys

πŸ§ͺ Step 6: Test user login

Now the user can log in from their own machine:

ssh -i devuser.pem devuser@EC2-IP

Check:

whoami

Output:

devuser

βœ… User is logged in βœ… Not root βœ… Isolated from other users


πŸ” Step 7: Decide sudo access (VERY IMPORTANT)

❓ Should the user be root-capable?

❌ If NO (most secure):

Do nothing.

User:

  • Cannot install software
  • Cannot change system config
  • Cannot access /etc, /var, etc.

βœ… If YES (admin / senior dev):

Add user to sudo group.

On Amazon Linux:

sudo usermod -aG wheel devuser

On Ubuntu:

sudo usermod -aG sudo devuser

Now user can run:

sudo command

πŸ“‚ Step 8: Folder & file access control

Example: You have a folder:

/app-data

You want:

  • devuser β†’ access
  • Others β†’ no access
sudo chown devuser:devuser /app-data
sudo chmod 700 /app-data

Now:

  • Only devuser can access
  • Others are blocked

πŸ‘₯ Step 9: Groups (Professional setup)

Create a group:

sudo groupadd devteam

Add user to group:

sudo usermod -aG devteam devuser

Assign folder to group:

sudo chown :devteam /shared-data
sudo chmod 770 /shared-data

Now:

  • All devteam members can access
  • Others cannot

❌ Common mistakes (IMPORTANT)

❌ Forgetting .ssh permissions ❌ Putting private key on server ❌ Logging everyone in as ec2-user ❌ Giving sudo to everyone


🧠 Mental model (REMEMBER THIS)

User = identity
Group = role
SSH key = login method
Permissions = access control

βœ… Final summary (Task 2)

To set up a new user on an EC2 instance, you create a Linux user, create a home directory, configure SSH access using the user’s public key, set strict permissions on .ssh files, and optionally grant sudo access using groups. This ensures secure, controlled access for each individual user.


How the other user creates their PEM file and shares it with you

This task is about what the OTHER PERSON does, not you.


🧠 First, clear one very important misunderstanding

πŸ‘‰ The admin does NOT create the PEM file for the user. πŸ‘‰ The user creates their OWN PEM file on their OWN machine.

This is how it works in real companies.


πŸ‘€ Who is β€œthe other user”?

  • Developer
  • Teammate
  • Intern
  • Ops engineer

They want access to your EC2 server.


πŸ”‘ Step 1: User creates SSH key pair on their own machine

On their laptop / system, they run:

ssh-keygen

They will see something like:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):

They can press Enter OR give a custom name:

devuser.pem

What files are created?

Two files are created:

FilePurpose
devuser.pemPRIVATE KEY (never shared)
devuser.pem.pubPUBLIC KEY (shared with admin)

πŸ“Œ The .pem file stays ONLY with the user.


User may be asked:

Enter passphrase (empty for no passphrase):
  • Passphrase = extra password protection
  • If someone steals the key β†’ still cannot use it

In companies: βœ”οΈ Passphrase is recommended


πŸ“€ Step 3: User shares ONLY the public key

The user sends ONLY this file (or its content):

devuser.pem.pub

Example content:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7...

⚠️ They NEVER share:

devuser.pem

πŸ”’ Why private key is never shared

If someone has the private key:

  • They can login as that user
  • No password needed
  • Full access

So:

Private key = identity

πŸ–₯️ Step 4: How the user logs in

From their machine, the user runs:

ssh -i devuser.pem devuser@EC2-IP

Important points:

  • Command runs on their laptop
  • Uses their private key
  • Logs in as devuser

πŸ” Step 5: How SSH verifies identity (Simple explanation)

  1. Server sends a challenge
  2. User signs it with private key
  3. Server checks public key
  4. Match found βœ”οΈ
  5. Login allowed πŸŽ‰

The private key is never sent to the server.


❌ Common beginner mistakes (VERY IMPORTANT)

❌ Admin generates key and shares PEM ❌ User sends private key via WhatsApp 😬 ❌ Storing private keys on server ❌ One key used by multiple users


🧠 Simple analogy (remember this)

Real worldSSH
House keyPrivate key (.pem)
Lock designPublic key (.pub)
House ownerEC2 admin

βœ… Final summary (Task 3)

The other user generates their own SSH key pair on their own system using ssh-keygen. They keep the private key (.pem) securely with them and share only the public key (.pub) with the EC2 admin. The admin adds this public key to the user’s authorized_keys file, allowing secure SSH access without sharing secrets.


πŸ‘ˆ

πŸ—‘οΈ How to Safely Delete a User in Linux

When you need to delete a user from your Linux server, follow these steps to ensure it's done safely and completely.

1️⃣ Login as a sudo user (NOT the user you want to delete)

whoami

Make sure this is not the user you’re deleting.


who

or

ps -u username

If they’re logged in, you can kill their session:

sudo pkill -u username

3️⃣ Delete the user account

sudo userdel -r username

βœ” Removes:

  • User account
  • Home directory (/home/username)
  • .ssh/authorized_keys (SSH access)
  • -r stands for remove the user’s files.

πŸ”Ή Delete user without removing home directory

sudo userdel username

(Not recommended unless you want to keep files)


4️⃣ (Important) Remove from sudo group if added earlier

If you added the user to sudo:

sudo deluser username sudo

or

sudo gpasswd -d username sudo

5️⃣ Verify user is deleted

id username

You should see:

id: β€˜username’: no such user

6️⃣ Double-check SSH access is gone

ls /home/username

You should get:

No such file or directory

⚠️ Extra Security Check (Very Important on EC2)

If this was an AWS EC2 server, also check:

πŸ”Ή authorized_keys manually (just in case)

sudo find /home -name authorized_keys

πŸ” What this command does (in simple words)

  • πŸ‘‰ It searches for all SSH public key files named authorized_keys
  • πŸ‘‰ inside /home directory
  • πŸ‘‰ for every user
  • πŸ‘‰ using admin (sudo) permission

πŸ”Ή /etc/ssh/sshd_config

Make sure no forced user is configured:

AllowUsers
AllowGroups

Restart SSH if you changed anything:

sudo systemctl restart sshd

πŸ‘ˆ

Linux has three types of users:

πŸ”Ή 1. System users

Created automatically by Linux during OS installation.

Examples from your list:

root
bin
daemon
adm
mail
sshd
systemd-network
chrony
apache

βœ… These users:

  • Are used by the OS and services
  • Cannot log in
  • Usually have /sbin/nologin as their shell

Example:

sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin

➑ SSH service runs as sshd user ➑ No human login allowed


πŸ”Ή 2. Service / Application users

Created when software is installed.

Examples:

apache
dbus
rpc
tcpdump
ec2-instance-connect

Why they exist:

  • For security
  • Each service runs with limited permissions

Example:

apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

➑ Apache web server runs as apache user


πŸ”Ή 3. Normal (human) users πŸ‘€

These are real login users.

From your system:

ec2-user
devuser

βœ” These users:

  • Have /bin/bash
  • Have home directories
  • Can log in

Example:

devuser:x:1002:1002::/home/devuser:/bin/bash

2️⃣ You created only these users πŸ‘‡

You said:

i create only two user devuser, rijuldev

That is correct βœ…

These are the users you created:

rijuldev
devuser

Additionally:

ec2-user

➑ Created automatically by AWS EC2


πŸ” who means:

"Show users currently logged in"

Right now:

  • Only you are connected
  • Probably via one SSH session
  • No other users are logged in

So who output is empty or minimal πŸ‘‰ This is normal

Try this:

whoami

You’ll see:

ec2-user

4️⃣ How to list only real login users?

πŸ”Ή Show users with /bin/bash

grep "/bin/bash" /etc/passwd

Output will be something like:

root
ec2-user
rijuldev
devuser

βœ” These are actual login users


5️⃣ Summary (easy to remember 🧠)

TypeExampleCan login?
System usersshd, systemd-*❌ No
Service userapache, dbus❌ No
Human userdevuser, rijuldev, ec2-userβœ… Yes

πŸ‘ˆ

πŸ”Ή What is curl?

curl stands for Client URL.

It is a command-line tool used to send requests to URLs and get responses from servers.

πŸ‘‰ In simple words:

curl is used to talk to servers from the terminal

You can:

  • Call APIs (REST APIs)
  • Download files
  • Upload files
  • Send GET, POST, PUT, DELETE requests
  • Send headers, tokens, cookies
  • Test backend APIs (Django, Spring Boot, Node, etc.)

πŸ”Ή Where is curl used?

  • Backend developers (API testing)
  • DevOps (health checks, automation)
  • Cloud engineers (AWS, servers)
  • CI/CD pipelines
  • Debugging production APIs

πŸ”Ή Is curl installed by default?

OSStatus
Linuxβœ… Yes
macOSβœ… Yes
Windows 10+βœ… Yes (new versions)

Check:

curl --version

πŸ”Ή Basic curl syntax

curl [options] URL

Example:

curl https://example.com

πŸ‘‰ This sends a GET request to the server.


πŸ”Ή HTTP Methods in curl

MethodPurpose
GETFetch data
POSTCreate data
PUTUpdate full data
PATCHUpdate partial data
DELETERemove data

πŸ”Ή GET Request (Most common)

curl https://api.example.com/users

With query params:

curl "https://api.example.com/users?id=10"

πŸ”Ή Show full response (headers + body)

curl -i https://example.com

πŸ”Ή Show only headers

curl -I https://example.com

πŸ”Ή Pretty print JSON response

curl https://api.example.com/users | jq

(jq is a JSON formatter tool)


πŸ”Ή POST Request (Send data)

Send JSON data

curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name":"Dev","age":22}'

Explanation:

  • -X POST β†’ HTTP method
  • -H β†’ Header
  • -d β†’ Data (body)

πŸ”Ή PUT Request (Update)

curl -X PUT https://api.example.com/users/1 \
-H "Content-Type: application/json" \
-d '{"name":"Dev","age":23}'

πŸ”Ή PATCH Request (Partial update)

curl -X PATCH https://api.example.com/users/1 \
-H "Content-Type: application/json" \
-d '{"age":24}'

πŸ”Ή DELETE Request

curl -X DELETE https://api.example.com/users/1

πŸ”Ή Sending Headers (Auth tokens, etc.)

curl https://api.example.com/profile \
-H "Authorization: Bearer YOUR_TOKEN"

Multiple headers:

curl https://api.example.com \
-H "Accept: application/json" \
-H "User-Agent: curl-test"

πŸ”Ή Authentication with curl

Basic Auth

curl -u username:password https://api.example.com

Bearer Token

curl -H "Authorization: Bearer TOKEN" https://api.example.com

πŸ”Ή Upload file using curl

curl -X POST https://api.example.com/upload \
-F "file=@image.png"

πŸ”Ή Download file using curl

curl -O https://example.com/file.zip

Rename file:

curl -o myfile.zip https://example.com/file.zip

πŸ”Ή Follow redirects

curl -L https://short.url

πŸ”Ή Save response to file

curl https://api.example.com/data -o data.json

πŸ”Ή Timeout in curl

curl --max-time 10 https://example.com

πŸ”Ή Silent mode (no progress)

curl -s https://example.com

πŸ”Ή Verbose / Debug mode (VERY IMPORTANT)

curl -v https://example.com

πŸ‘‰ Shows:

  • DNS lookup
  • TLS handshake
  • Request headers
  • Response headers

πŸ”Ή Common curl options (Must know)

OptionMeaning
-XHTTP method
-HHeader
-dData
-iInclude headers
-IOnly headers
-vVerbose
-sSilent
-oOutput file
-OSave with original name
-LFollow redirects
-uAuthentication

πŸ”Ή curl vs Postman

curlPostman
CLI basedGUI based
LightweightHeavy
Used in serversUsed in local testing
AutomatableManual

πŸ‘‰ In real projects, both are used


πŸ”Ή curl in Backend Development (Your case)

Since you work with Django APIs & Angular, curl is useful for:

  • Testing API endpoints without frontend
  • Checking auth headers
  • Debugging POST/GET issues
  • Server health checks on EC2

Example for Django:

curl http://127.0.0.1:8000/api/expenses/

πŸ”Ή Common curl mistakes ❌

  1. Forgetting quotes around JSON
  2. Missing Content-Type
  3. Wrong HTTP method
  4. Sending GET with -d
  5. Forgetting -L for redirects

πŸ”Ή Interview-ready answer

Q: What is curl?

curl is a command-line tool used to transfer data between a client and a server using protocols like HTTP, HTTPS, FTP, etc. It is commonly used for testing APIs, downloading/uploading files, and debugging network requests.


πŸ”Ή When should you use curl?

  • When you don’t have frontend
  • When debugging APIs on server
  • When automating API calls
  • When Postman is not available

πŸ‘ˆ

Batch Scripting (Windows CMD) – Complete Guide

This guide covers everything you need to know about Batch scripting, from absolute basics to advanced concepts. It is structured step‑by‑step so you can learn, practice, and apply it in real projects.


1️⃣ What is Batch Scripting?

Batch scripting is a way to automate tasks in Windows using .bat or .cmd files.

Why Batch Scripts are used

  • Automate repetitive tasks
  • System administration
  • File & folder management
  • Application startup automation
  • Basic DevOps & CI tasks (Windows servers)

Batch File Extensions

  • .bat – Traditional batch file
  • .cmd – Modern version (recommended)

2️⃣ Creating & Running a Batch File

Create a batch file

  1. Open Notepad
  2. Write commands
  3. Save as script.bat

Run a batch file

  • Double‑click
  • Run from Command Prompt

3️⃣ Basic Commands

CommandPurpose
echoPrint text
@echo offHide command execution
pauseWait for user input
clsClear screen
exitExit script
remComment

4️⃣ Variables in Batch

Define a variable

set name=Dev

Access variable

%name%

Enable delayed expansion

setlocal enabledelayedexpansion
!variable!

Environment variables

%USERNAME%
%OS%
%DATE%

5️⃣ User Input

set /p name=Enter your name:

6️⃣ Conditional Statements (IF)

Basic IF

if %a%==10 echo Equal

IF ELSE

if %a%==10 (
  echo Yes
) else (
  echo No
)

IF EXIST

if exist file.txt echo Found

Comparison Operators

  • EQU – Equal
  • NEQ – Not equal
  • GTR – Greater than
  • LSS – Less than

7️⃣ Loops in Batch

FOR loop (files)

for %%f in (*.txt) do echo %%f

FOR loop (numbers)

for /L %%i in (1,1,5) do echo %%i

FOR /F (command output)

for /f "tokens=1" %%a in ('date /t') do echo %%a

8️⃣ GOTO & Labels

:START
echo Hello
goto END
:END
echo Done

⚠ Avoid excessive goto – makes scripts messy


9️⃣ Functions (CALL)

call :myFunc
exit /b

:myFunc
echo Function called
exit /b

πŸ”Ÿ Error Handling

ERRORLEVEL

command
if %errorlevel% neq 0 echo Error occurred

Exit codes

exit /b 1

1️⃣1️⃣ File & Folder Operations

CommandPurpose
dirList files
copyCopy files
moveMove files
delDelete files
mkdirCreate folder
rmdirDelete folder
xcopyAdvanced copy
robocopyReliable copy

1️⃣2️⃣ Working with Paths

%~dp0   β†’ Script directory
%~nx0   β†’ Script name

1️⃣3️⃣ Command Line Arguments

script.bat arg1 arg2
%1 %2 %3

1️⃣4️⃣ Date & Time Handling

%DATE%
%TIME%

Custom format using FOR loop


1️⃣5️⃣ String Operations

  • Substring
%var:~0,4%
  • Replace
%var:old=new%

1️⃣6️⃣ Arithmetic Operations

set /a sum=5+3

Supported:

    • βˆ’ * / %

1️⃣7️⃣ Delayed Expansion (Important)

Used inside loops

setlocal enabledelayedexpansion

1️⃣8️⃣ Logging Output

script.bat > output.log
script.bat >> output.log

1️⃣9️⃣ Calling Other Scripts

call another.bat

2️⃣0️⃣ Scheduled Tasks

Run batch files using:

  • Task Scheduler
  • Startup folder

2️⃣1️⃣ Registry Operations

reg add
reg delete
reg query

⚠ Use carefully


2️⃣2️⃣ Networking Commands

CommandUse
pingConnectivity
ipconfigIP info
net useNetwork drives
netstatNetwork stats

2️⃣3️⃣ System Commands

CommandPurpose
tasklistRunning processes
taskkillKill process
shutdownShutdown/restart
systeminfoSystem details

2️⃣4️⃣ Best Practices

  • Always use @echo off
  • Use comments
  • Validate inputs
  • Handle errors
  • Avoid hard‑coded paths
  • Use functions

2️⃣5️⃣ Real‑World Use Cases

  • Backup scripts
  • Log cleanup
  • App startup automation
  • Build pipelines (Windows)
  • Server maintenance

2️⃣6️⃣ Batch vs PowerShell

BatchPowerShell
OldModern
LimitedPowerful
SimpleAdvanced

Batch is still useful for legacy systems & simple automation.