π
- 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 Topics
Linux Basics
- 1οΈβ£ What is Linux?
- 2οΈβ£ Why Linux is important for AWS?
- 3οΈβ£ Linux Distributions (Very Important)
- 4οΈβ£ Linux is Case-Sensitive β
- 5οΈβ£ Linux Directory Structure (High-Level)
- 6οΈβ£ Root User vs Normal User
- 7οΈβ£ Linux Shell & Terminal
- 8οΈβ£ Linux vs Windows (Simple Comparison)
- 9οΈβ£ What you should remember from Topic 1
File & Directory Management (Linux)
- 1οΈβ£ What is a File & Directory?
- 2οΈβ£ Current Working Directory
- 3οΈβ£ Listing Files & Folders
- 4οΈβ£ Changing Directories
- 5οΈβ£ Absolute vs Relative Paths (VERY IMPORTANT)
- 6οΈβ£ Creating Files & Directories
- 7οΈβ£ Copying Files & Directories
- 8οΈβ£ Moving & Renaming
- 9οΈβ£ Deleting Files & Directories β
- π Useful Shortcuts
- 1οΈβ£1οΈβ£ What you must remember (Exam + Real AWS)
File Viewing & Editing (Linux)
- 1οΈβ£ Why this topic is important for AWS?
- 2οΈβ£ Viewing File Content (Basic)
- 3οΈβ£ Viewing Part of a File
- 4οΈβ£ Editing Files in Linux
- 5οΈβ£
nanoEditor (Beginner Friendly) - 6οΈβ£
viEditor (Basic Knowledge Enough) - 8οΈβ£ What you must remember
File Permissions & Ownership (Linux)
- 1οΈβ£ Why permissions are important in AWS?
- 2οΈβ£ Understanding File Permissions
- 3οΈβ£ Permission Structure (Very Important)
- 4οΈβ£ User, Group, Others
- 5οΈβ£ File vs Directory Permissions
- 6οΈβ£ Changing Permissions
chmod(change mode) - 7οΈβ£ Making a Script Executable (AWS COMMON)
- 8οΈβ£ File Ownership
chown(change owner) - 9οΈβ£ Why
sudois needed - π Common AWS Permission Issues
- 1οΈβ£1οΈβ£ Real AWS Example
- 1οΈβ£2οΈβ£ What you MUST remember
User & Group Management (Linux)
- 1οΈβ£ Why this topic is important for AWS?
- 2οΈβ£ What is a User?
- 3οΈβ£ Root User vs Normal User
- 4οΈβ£ What is a Group?
- 5οΈβ£ Important User Commands
- 6οΈβ£ Creating a User (Admin Task)
- 7οΈβ£ Switching Users
- 8οΈβ£ Deleting a User
- 9οΈβ£ Group Management
- π AWS Real-Life Examples
Process Management (Linux)
- 1οΈβ£ What is a Process?
- 2οΈβ£ Process ID (PID)
- 3οΈβ£ Foreground vs Background Process
- 4οΈβ£ Sending Process to Background
- 5οΈβ£ Process States (High-Level)
- 6οΈβ£ Monitoring Processes
- 7οΈβ£ Finding a Specific Process
- 8οΈβ£ Killing Processes (IMPORTANT)
- 9οΈβ£ Kill by Name
- π AWS Real-Life Examples
- 1οΈβ£1οΈβ£ Background Jobs
- 1οΈβ£3οΈβ£ Background Processes in AWS (nohup)
- 1οΈβ£2οΈβ£ What you MUST remember
Networking Basics (Linux)
- 1οΈβ£ IP Address & Hostname
- 2οΈβ£ Network Interfaces
- 3οΈβ£ Ports & Services (VERY IMPORTANT)
- 4οΈβ£ Connectivity Testing
- 5οΈβ£ Downloading from Internet
- 6οΈβ£ AWS Real-Life Debugging Scenario
- 7οΈβ£ What you MUST remember
Disk & Storage Management (Linux)
- π§± What is Disk & Storage Management in Linux?
- π΄ Types of Storage Devices in Linux
- π§ Disk Naming in Linux
- π¦ Partitions (Why needed?)
- π Partitioning Tools
- 𧬠Filesystem (VERY IMPORTANT)
- π Mounting (Make storage usable)
- π Permanent Mount (fstab)
- π Disk Usage Monitoring
- π Inodes (Often ignored but important)
- π Permissions & Ownership
- π₯ Logical Volume Management (LVM)
- βοΈ Disk Management in Cloud (AWS EC2)
- π§― Disk Errors & Health
- π¨ Common Disk Problems
- π§ Important Linux Directories
- π§ͺ Real-World Example
- π― Interview-Ready Questions
- π§Ύ Must-Know Commands (Cheat Sheet)
Package Management (Linux)
- π¦ What is Package Management?
- π§ What is a Package?
- π Why Package Management Exists
- π Package Management Components
- π§° Common Linux Package Managers
- 1οΈβ£
yumβ (Amazon Linux, RHEL, CentOS) - 2οΈβ£
dnfβ (Amazon Linux 2023) - 3οΈβ£
aptβ (Ubuntu on EC2) - 4οΈβ£ Repository Management (AWS Important)
- 5οΈβ£ Package Files (
rpm&dpkg) - 6οΈβ£ Real AWS EC2 Examples π₯
- 7οΈβ£ AWS Exam Focus (Remember This)
- π― Interview-Ready Questions
- π Final Summary
Service Management (systemd)
- π§ What is Service Management?
- π§ What is
systemd? - πΉ Main command:
systemctl - π What is a Service / Daemon?
- π systemd Unit Types
- 1οΈβ£ Start a Service
- 2οΈβ£ Stop a Service
- 3οΈβ£ Restart a Service (VERY COMMON)
- 4οΈβ£ Reload a Service
- 5οΈβ£ Check Service Status β (MOST IMPORTANT)
- 6οΈβ£ Enable Service at Boot (AWS CRITICAL)
- 7οΈβ£ Disable Service at Boot
- 8οΈβ£ Start + Enable Together
- 9οΈβ£ List All Services
- π Check if Service is Enabled
- 1οΈβ£1οΈβ£ Check if Service is Active
- 1οΈβ£2οΈβ£ View Service Logs (AWS Debugging)
- 1οΈβ£3οΈβ£ Common AWS Services & Names
- 1οΈβ£4οΈβ£ Real AWS EC2 Examples π₯
- 1οΈβ£6οΈβ£ systemd Files (High Level β Exam Only)
- 1οΈβ£7οΈβ£ systemd vs init (Interview / Exam)
- π§© Service States
- βοΈ Anatomy of a Service File
- π§ Key Sections Explained
- π Reload systemd (IMPORTANT)
- β± systemd Timers (cron replacement)
- π§― Common systemd Problems
- π§ͺ Common Admin Commands
- π― Interview-Ready Q&A
- π§Ύ Must-Know Commands Cheat Sheet
- π Final Summary
Environment Variables & Shell
- 1οΈβ£ What are Environment Variables?
- 2οΈβ£ Why Environment Variables are Important in AWS?
- 3οΈβ£ Viewing Environment Variables
- 4οΈβ£ PATH Variable (VERY IMPORTANT)
- 5οΈβ£ Setting Environment Variables (Temporary)
- 6οΈβ£ Setting Environment Variables (Permanent)
- 7οΈβ£ Shell Configuration Files
- 8οΈβ£ Using Environment Variables in Apps
- 9οΈβ£ AWS Real-Life Example
- π Common AWS Problems
- 1οΈβ£1οΈβ£ What you MUST remember
Searching & Text Processing (Linux)
- π What is Searching & Text Processing?
- π§± Core Philosophy (VERY IMPORTANT)
- π Searching Files (find & locate)
- π Searching Text Inside Files (
grep) - π Regular Expressions (Regex Basics)
- π§ͺ Filtering & Transforming Text
- π Sorting & Counting
- π Pipes (
|) β Power of Linux - π File Content Viewing
- π§― Real-World Use Cases
- π§ Common Mistakes
- π― Interview-Ready Questions
- π§Ύ Must-Know Commands Cheat Sheet
- π Final Summary
Compression & Archiving (Linux)
- 1οΈβ£ What is Archiving vs Compression?
- 2οΈβ£
tarβ Archiving Tool (MOST IMPORTANT) - 3οΈβ£ Compressed Archives (
tar.gz) - 4οΈβ£
gzip&gunzip - 5οΈβ£
zip&unzip - 6οΈβ£ Backup Basics (AWS Context)
- 7οΈβ£ AWS Real-Life Example
- 8οΈβ£ Common Mistakes
- 9οΈβ£ What you MUST remember
SSH & Remote Access
- π What is SSH?
- π Why SSH is Important
- π§ How SSH Works (Concept)
- π₯ SSH Basic Command
- π SSH Authentication Methods
- π SSH Key Files
- π File Permissions (VERY IMPORTANT)
- πͺ SSH Port & Config
- π File Transfer with SSH
- π SSH Tunneling & Port Forwarding
- π§― Common SSH Errors & Fixes
- βοΈ SSH in AWS EC2 (Real-World)
- π SSH Security Best Practices
- π§ͺ Advanced SSH Options
- π SSH Config File (Client Side)
- π― Interview-Ready Q&A
- π§Ύ Must-Know Commands Cheat Sheet
- π Final Summary
Bash Scripting Basics
- π What is Bash?
- π What is a Bash Script?
- π§± Structure of a Bash Script
- βΆοΈ Running a Bash Script
- π£ Variables in Bash
- π₯ User Input
- π Conditional Statements
- π Loops
- π§ Functions
- π File & Directory Checks
- π Command Line Arguments
- π Exit Status
- π Pipes & Redirection
- π§ͺ Debugging Bash Scripts
- π§― Common Mistakes
- βοΈ Real-World Use Case
- π― Interview-Ready Questions
- π§Ύ Must-Know Bash Commands
- π Final Summary
Linux Security Basics
- 1οΈβ£ Firewall Basics
- 2οΈβ£ File Permission Security
- 3οΈβ£ SSH Security (VERY IMPORTANT)
- 4οΈβ£ Running Services Securely
- 5οΈβ£ Principle of Least Privilege
- 6οΈβ£ AWS Security Context
- 7οΈβ£ Common AWS Security Mistakes
- 8οΈβ£ AWS Real-Life Example
- 9οΈβ£ What you MUST remember
Logs & Troubleshooting (Linux)
- 1οΈβ£ What are Logs?
- 2οΈβ£ System Logs
- 3οΈβ£ Application Logs
- 4οΈβ£ Reading Error Logs (MOST IMPORTANT)
- 5οΈβ£ Service Logs (systemd)
- 6οΈβ£ Basic Troubleshooting Flow (VERY IMPORTANT)
- 7οΈβ£ AWS Real-Life Example
- 8οΈβ£ Common AWS Errors & Logs
- 9οΈβ£ What you MUST remember
Linux for Cloud (AWS Context)
- βοΈ What Does βLinux for Cloud (AWS)β Mean?
- π§ Why Linux Is So Important in AWS?
- π₯ Linux in AWS = EC2
- π Accessing Linux EC2 (SSH)
- π Linux Filesystem in Cloud Servers
- π¦ Package Management (Cloud Must-Know)
- π§ Service Management (Production Critical)
- π Monitoring & Logs (Very Important in Cloud)
- πΎ Storage in AWS (Linux View)
- π Linux Security in AWS
- π Networking (Linux + AWS)
- π§ͺ Automation with Bash (Cloud Core Skill)
- π Scaling & Linux
- βοΈ Linux + AWS Services Mapping
- π§― Common Cloud Linux Problems
- π― Interview-Ready Q&A
- π§Ύ Must-Know Linux Commands for AWS
- π Final Summary (Very Important)
π
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:
| Distribution | Used where |
|---|---|
| Amazon Linux | Default in AWS EC2 |
| Ubuntu | Very popular |
| RHEL | Enterprise systems |
| CentOS | Older 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:
| Directory | Purpose |
|---|---|
/ | Root (starting point) |
/home | User home folders |
/etc | Configuration files |
/var | Logs & variable data |
/opt | Optional software |
/bin | Basic commands |
/usr | User 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
Normal user (Recommended)
- Limited access
- Uses
sudofor 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)
| Linux | Windows |
|---|---|
| CLI focused | GUI focused |
| Lightweight | Heavy |
| Free | Paid |
| Best for servers | Best 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:
| Part | Meaning |
|---|---|
- | File type |
rw- | Owner permissions |
rw- | Group permissions |
r-- | Others permissions |
. | SELinux context (Amazon Linux uses SELinux) |
File type (first character)
| Symbol | Meaning |
|---|---|
- | Regular file |
d | Directory |
l | Symbolic link |
πΉ Column 2: Number of links
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:
53142881bytes β 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 β best command (recommended)
less file.txt
Controls:
β ββ scrollSpaceβ next page/errorβ searchqβ 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β saveEnterβ confirmCtrl + 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β readwβ writexβ execute-β no permission
4οΈβ£ User, Group, Others
| Type | Meaning |
|---|---|
| User | File owner |
| Group | Group owner |
| Others | Everyone else |
π Linux always checks permissions in this order: User β Group β Others
5οΈβ£ File vs Directory Permissions
File
rβ read filewβ modify filexβ run file (script)
Directory
rβ list fileswβ create/delete filesxβ 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
ameans all (user, group, others)umeans user (owner)gmeans groupomeans others
Numeric mode (VERY IMPORTANT)
| Number | Permission |
|---|---|
| 4 | read |
| 2 | write |
| 1 | execute |
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
Normal user (Recommended)
- Limited permissions
- Uses
sudofor admin tasks
π On AWS:
- You login as ec2-user or ubuntu
- You use
sudowhen 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)
| State | Meaning |
|---|---|
| R | Running |
| S | Sleeping |
| D | Waiting |
| Z | Zombie |
| T | Stopped |
π 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
- β
topis your best friend - β
killstops processes - β
Use
kill -9carefully - β 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:
| Port | Service |
|---|---|
| 22 | SSH |
| 80 | HTTP |
| 443 | HTTPS |
| 3306 | MySQL |
| 8080 | App server |
Check Open Ports
netstat -tulnp # Show all TCP ports that are currently listening, with process info.
or
ss -tulnp # Modern replacement for netstat
| Option | Meaning |
|---|---|
-t | Show TCP connections only |
-o | Show timers (retransmit / keepalive info) |
-n | Show numeric addresses (no DNS lookup) |
-l | Show listening (server) ports |
-p | Show 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))
| Column | Explanation |
|---|---|
| Proto | Protocol (tcp, tcp6) |
| Recv-Q | Data waiting to be read |
| Send-Q | Data waiting to be sent |
| Local Address | IP:Port on your server |
| Foreign Address | Client side (who can connect) |
| State | Connection state |
| PID/Program name | Process using the port |
| Timer | TCP 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 -tulnpshows listening ports - β
curlis 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
| Device | Example |
|---|---|
| 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.
| Name | Meaning |
|---|---|
/dev/sda | First disk |
/dev/sda1 | First partition |
/dev/sdb | Second disk |
/dev/nvme0n1p1 | NVMe 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
| Tool | Use |
|---|---|
fdisk | MBR partitions |
cfdisk | Menu based |
parted | GPT partitions |
lsblk | Display structure |
Create partition (example):
sudo fdisk /dev/sdb
Common fdisk commands:
nβ new partitionpβ print tablewβ write changes
𧬠Filesystem (VERY IMPORTANT)
A filesystem defines how data is stored & retrieved.
| Filesystem | Use |
|---|---|
| ext4 | Default Linux |
| xfs | High performance |
| ntfs | Windows |
| vfat | USB 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
| Component | Meaning |
|---|---|
| PV | Physical Volume |
| VG | Volume Group |
| LV | Logical 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
| Issue | Reason |
|---|---|
| Disk full | Logs, backups |
| Boot failure | Wrong fstab |
| Slow IO | HDD / high load |
| Read-only FS | Disk error |
π§ Important Linux Directories
| Path | Purpose |
|---|---|
/ | Root |
/home | User data |
/var | Logs |
/tmp | Temporary |
/boot | Boot 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
| Component | Purpose |
|---|---|
| Package | Software unit |
| Repository | Software source |
| Package Manager | Tool to manage packages |
| Dependency | Required packages |
π§° Common Linux Package Managers
| Package Manager | Linux Distribution |
|---|---|
yum / dnf | Amazon Linux, RHEL, CentOS |
apt | Ubuntu, Debian |
pacman | Arch Linux |
zypper | SUSE |
π 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 softwarenginxβ 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
-yflag - β
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 loginnginxβ Web serverdockerβ Container enginemysqlβ Database
π systemd Unit Types
systemd manages units.
| Unit Type | Extension | Purpose |
|---|---|---|
| Service | .service | Background services |
| Socket | .socket | Socket activation |
| Target | .target | Group of units |
| Mount | .mount | Mount points |
| Timer | .timer | Scheduled jobs |
1οΈβ£ Start a Service
sudo systemctl start httpd
Meaning:
startβ start the service nowhttpdβ 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:
| restart | reload |
|---|---|
| Stops & starts service | Reloads config only |
| Causes short downtime | No 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:
enableddisabled
1οΈβ£1οΈβ£ Check if Service is Active
systemctl is-active httpd
Output:
activeinactivefailed
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
| Service | Name |
|---|---|
| Apache | httpd |
| Nginx | nginx |
| Docker | docker |
| SSH | sshd |
| Cron | crond |
π 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)
| init | systemd |
|---|---|
| Old | Modern |
| Slow | Fast |
| No dependency mgmt | Dependency aware |
π§ One-line Rule
Install β Start β Enable β Check Status
π§© Service States
| State | Meaning |
|---|---|
| active (running) | Service is running |
| inactive | Not running |
| failed | Crashed |
| enabled | Starts at boot |
| disabled | Manual 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
| Problem | Cause |
|---|---|
| Service fails to start | Wrong path |
| Permission denied | Wrong User |
| Starts then stops | ExecStart exits |
| Not starting at boot | Not 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
| Command | Effect |
|---|---|
systemctl reboot | Restart entire system |
systemctl poweroff | Shutdown system |
systemctl daemon-reexec | Restart 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
systemdis 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:
| File | Purpose |
|---|---|
~/.bashrc | User shell config |
~/.bash_profile | Login shell |
/etc/profile | System-wide |
/etc/environment | Global 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 β Real-time search
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 β Fast index-based search
locate nginx.conf
Update index:
sudo updatedb
β οΈ May not find newly created files until index updates
π Searching Text Inside Files (grep)
Basic search
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
| Command | Use |
|---|---|
cat | Show entire file |
less | Scrollable view |
head | First lines |
tail | Last 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
| Mistake | Fix |
|---|---|
| Using cat unnecessarily | Use grep directly |
| Not sorting before uniq | Always sort |
| Forgetting quotes | Quote patterns |
| Using locate for live search | Use 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β createvβ verbosefβ 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)
- Client sends connection request
- Server responds with public key
- Client verifies server
- Authentication happens (password or key)
- 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
| File | Purpose |
|---|---|
id_rsa | Private key (never share) |
id_rsa.pub | Public key |
authorized_keys | Allowed keys |
known_hosts | Trusted 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
| Error | Fix |
|---|---|
| Permission denied | Check key & permissions |
| Connection refused | SSH not running |
| Host key changed | Remove from known_hosts |
| Timeout | Firewall / 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
| Mistake | Fix |
|---|---|
| Missing shebang | Add #!/bin/bash |
| Spaces in variable | var=value |
| Forget chmod | chmod +x |
| Using wrong quotes | Use " |
βοΈ 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)firewalldufw(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
777permissions - Use
755for folders - Use
644for 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:
nginxappuserdocker
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
| Layer | Security |
|---|---|
| AWS | Security Groups, IAM |
| Linux | Users, permissions, firewall |
| App | Authentication, 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:
| Log | Purpose |
|---|---|
/var/log/messages | System events (Amazon Linux) |
/var/log/syslog | System logs (Ubuntu) |
/var/log/dmesg | Kernel messages |
/var/log/secure | Security & 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
| Error | Check |
|---|---|
| 403 Forbidden | File permissions |
| 502 Bad Gateway | App crash |
| Connection refused | Service down |
| Disk full | df -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?
| Reason | Explanation |
|---|---|
| EC2 runs Linux | Amazon Linux, Ubuntu, RHEL |
| Cost | Linux is free / cheaper |
| Automation | Bash, cron, systemd |
| Security | SSH, permissions, firewall |
| Performance | Lightweight & 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:
| OS | User |
|---|---|
| Amazon Linux | ec2-user |
| Ubuntu | ubuntu |
| RHEL | ec2-user |
π Linux Filesystem in Cloud Servers
Important directories:
| Path | Use |
|---|---|
/ | Root |
/home/ec2-user | User data |
/var/log | Logs |
/etc | Config files |
/opt | Custom 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
| AWS | Linux |
|---|---|
| EBS Volume | /dev/xvda, /dev/nvme0n1 |
| S3 | Mounted via tools |
| Instance Store | Temporary 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:
| AWS | Linux |
|---|---|
| Security Group | Firewall rules |
| ENI | Network interface |
| ALB | Reverse 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 Service | Linux Role |
|---|---|
| EC2 | OS & apps |
| ECS/EKS | Containers on Linux |
| Lambda | Linux runtime |
| CloudWatch | Linux logs |
| CodeDeploy | Linux deployments |
π§― Common Cloud Linux Problems
| Problem | Cause |
|---|---|
| SSH timeout | SG / NACL |
| Disk full | Logs |
| App stopped | systemd |
| Permission denied | chmod/chown |
| High CPU | runaway 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
| Option | Meaning |
|---|---|
-l | long listing |
-a | show 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
-mflag: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.txtexists β 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
Navigation commands π§
| Command | Action |
|---|---|
| Arrow keys | Move cursor |
CTRL + A | Start of line |
CTRL + E | End of line |
CTRL + Y | Page up |
CTRL + V | Page down |
Selecting text (Mark mode)
Start selection
CTRL + ^
(or CTRL + SHIFT + 6)
- Move cursor to select text
Cut selected text
CTRL + K
Undo / Redo
| Action | Command |
|---|---|
| Undo | ALT + U |
| Redo | ALT + 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 π
| Task | Command |
|---|---|
| Open file | nano file.txt |
| Save | CTRL + O |
| Exit | CTRL + X |
| Cut line | CTRL + K |
| Paste | CTRL + U |
| Search | CTRL + W |
| Replace | CTRL + \ |
| Help | CTRL + 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
vialways 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:
| Mode | Purpose |
|---|---|
| Normal mode | Navigation + commands |
| Insert mode | Typing text |
| Command mode | Save, 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 lineyyβ copy linepβ pasteggβ 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:
| Key | Meaning |
|---|---|
i | Insert before cursor |
a | Append after cursor |
o | New line below |
O | New 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:
- Open file
- Press
i - Type text
- Press
ESC - Save and quit
Letβs see commands.
πΎ Save and Exit Commands (MUST KNOW)
| Command | Meaning |
|---|---|
:w | Save (write) |
:q | Quit |
:wq | Save and quit |
:x | Save 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:
| Key | Action |
|---|---|
h | Left |
l | Right |
j | Down |
k | Up |
Why?
- Faster
- Hands stay on keyboard
Line & file movement
| Command | Meaning |
|---|---|
0 | Start of line |
$ | End of line |
gg | Go to top |
G | Go to bottom |
:10 | Go to line 10 |
βοΈ DELETE, COPY, PASTE (VERY IMPORTANT)
Delete
| Command | Action |
|---|---|
dd | Delete current line |
dw | Delete word |
d$ | Delete till end of line |
x | Delete one character |
π Delete = cut (stored in buffer)
Copy (Yank)
| Command | Action |
|---|---|
yy | Copy current line |
yw | Copy word |
y$ | Copy till end |
Paste
| Command | Action |
|---|---|
p | Paste below cursor |
P | Paste above cursor |
π Undo and Redo
| Action | Command |
|---|---|
| Undo | u |
| Redo | CTRL + R |
π Undo is life saver.
π SEARCHING (Very useful)
Search forward
/word
Search backward
?word
Press:
nβ next matchNβ 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β deleteyβ 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)
| Feature | Nano | Vi |
|---|---|---|
| Beginner friendly | β | β |
| Always installed | β | β |
| Powerful editing | β | β |
| Used in production | Rare | Very 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:
iESC:wqdd/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
.pemfile 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:
- Other person generates their own SSH key
- They send you only the public key
- You create a Linux user on EC2
- You add their public key to that user
- 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:
-
Security
- Root can delete OS, disks, users
-
Accidental damage
- One wrong command can destroy server
-
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
| Action | Result |
|---|---|
| SSH with PEM | Login as normal user |
whoami | ec2-user |
sudo command | Run as root |
sudo su - | Become root |
| Root SSH login | Disabled |
β 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 usingsudo.
π
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
devusercan 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
.sshfiles, 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:
| File | Purpose |
|---|---|
devuser.pem | PRIVATE KEY (never shared) |
devuser.pem.pub | PUBLIC KEY (shared with admin) |
π The .pem file stays ONLY with the user.
π Step 2: Optional β set passphrase (recommended)
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)
- Server sends a challenge
- User signs it with private key
- Server checks public key
- Match found βοΈ
- 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 world | SSH |
|---|---|
| House key | Private key (.pem) |
| Lock design | Public key (.pub) |
| House owner | EC2 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βsauthorized_keysfile, 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.
2οΈβ£ Check if the user is currently logged in (recommended)
who
or
ps -u username
If theyβre logged in, you can kill their session:
sudo pkill -u username
3οΈβ£ Delete the user account
πΉ Delete user and their home directory (recommended)
sudo userdel -r username
β Removes:
- User account
- Home directory (
/home/username) .ssh/authorized_keys(SSH access)-rstands 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/nologinas 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 π§ )
| Type | Example | Can login? |
|---|---|---|
| System user | sshd, systemd-* | β No |
| Service user | apache, dbus | β No |
| Human user | devuser, 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?
| OS | Status |
|---|---|
| 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
| Method | Purpose |
|---|---|
| GET | Fetch data |
| POST | Create data |
| PUT | Update full data |
| PATCH | Update partial data |
| DELETE | Remove 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)
| Option | Meaning |
|---|---|
-X | HTTP method |
-H | Header |
-d | Data |
-i | Include headers |
-I | Only headers |
-v | Verbose |
-s | Silent |
-o | Output file |
-O | Save with original name |
-L | Follow redirects |
-u | Authentication |
πΉ curl vs Postman
| curl | Postman |
|---|---|
| CLI based | GUI based |
| Lightweight | Heavy |
| Used in servers | Used in local testing |
| Automatable | Manual |
π 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 β
- Forgetting quotes around JSON
- Missing
Content-Type - Wrong HTTP method
- Sending GET with
-d - Forgetting
-Lfor 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
- Open Notepad
- Write commands
- Save as
script.bat
Run a batch file
- Doubleβclick
- Run from Command Prompt
3οΈβ£ Basic Commands
| Command | Purpose |
|---|---|
echo | Print text |
@echo off | Hide command execution |
pause | Wait for user input |
cls | Clear screen |
exit | Exit script |
rem | Comment |
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
| Command | Purpose |
|---|---|
dir | List files |
copy | Copy files |
move | Move files |
del | Delete files |
mkdir | Create folder |
rmdir | Delete folder |
xcopy | Advanced copy |
robocopy | Reliable 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
| Command | Use |
|---|---|
ping | Connectivity |
ipconfig | IP info |
net use | Network drives |
netstat | Network stats |
2οΈβ£3οΈβ£ System Commands
| Command | Purpose |
|---|---|
tasklist | Running processes |
taskkill | Kill process |
shutdown | Shutdown/restart |
systeminfo | System 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
| Batch | PowerShell |
|---|---|
| Old | Modern |
| Limited | Powerful |
| Simple | Advanced |
Batch is still useful for legacy systems & simple automation.