π
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.