ในบทความนี้
- Navigation — เดินทางใน File System
- จัดการไฟล์ — สร้าง ลบ ย้าย คัดลอก
- อ่านไฟล์ — ดูเนื้อหาแบบต่างๆ
- Search & Filter — หาของเก่ง
- Pipes & Redirection — ต่อท่อ
- Process Management — จัดการโปรเซส
- Permissions — สิทธิ์การเข้าถึง
- Package Management — ติดตั้ง software
- User Management — จัดการ users
- Networking Commands — เครือข่าย
- Shell Scripting — เขียน script อัตโนมัติ
- Cron Jobs — ตั้งเวลารันอัตโนมัติ
- คำสั่งที่ DevOps ใช้บ่อยที่สุด (Cheat Sheet)
- สรุป
In this post
- Navigation — Moving Around the File System
- Managing Files — Create, Delete, Move, Copy
- Reading Files — Several Ways to Look Inside
- Search & Filter — Good at Finding Things
- Pipes & Redirection — Connecting the Plumbing
- Process Management — Handling Processes
- Permissions — Who Can Touch What
- Package Management — Installing Software
- User Management — Handling Users
- Networking Commands — The Network
- Shell Scripting — Writing Automation
- Cron Jobs — Scheduling Automatic Runs
- The Commands DevOps Uses Most (Cheat Sheet)
- Summary
ในซีรีส์ DevOps เราพูดถึง Docker, K8s, Terraform, Ansible มาเยอะแล้ว แต่ทุกเครื่องมือเหล่านั้นล้วนรันบน Linux ทั้งนั้น!
ถ้าใช้ Linux command ไม่เป็น... เหมือนเชฟที่ถือมีดไม่เป็น 🔪
มาเรียนรู้คำสั่ง Linux ที่ DevOps ใช้ทุกวัน ตั้งแต่พื้นฐานไปจนถึง shell scripting 🐧🐕
Navigation — เดินทางใน File System
ก่อนจะทำอะไรใน Linux ต้อง "เดินทาง" ใน file system เป็นก่อน — Linux จัดโครงสร้างไฟล์เป็น tree เริ่มจาก / (root) ลงไป /home, /etc, /var ไม่มี drive letter เหมือน Windows คำสั่ง 3 ตัวที่ต้องจำ: pwd (อยู่ที่ไหน), ls (มีอะไรบ้าง), cd (ไปที่ไหน):
# อยู่ไหน?
pwd
# /home/deploy
# ไปที่ไหน?
cd /var/log # ไปตาม path
cd ~ # กลับ home
cd .. # ขึ้น 1 ระดับ
cd - # กลับไปที่เดิม (toggle)
# มีอะไรอยู่?
ls # list files
ls -la # list แบบละเอียด + hidden files
ls -lh # ขนาดอ่านง่าย (1.5M, 2.3G)
ls -lt # เรียงตามเวลา (ใหม่สุดบน)
# หา path แบบ tree
tree -L 2 # แสดงโครงสร้าง 2 ระดับ
💡 Tip: กด Tab เพื่อ auto-complete ชื่อไฟล์/โฟลเดอร์ — กด 2 ครั้งเพื่อดูตัวเลือก
จัดการไฟล์ — สร้าง ลบ ย้าย คัดลอก
การจัดการไฟล์ใน Linux ทำผ่าน command line ได้เร็วกว่า GUI มาก — สร้างโฟลเดอร์ 10 ตัวพร้อมกัน, ลบไฟล์ตาม pattern, ย้าย/copy แบบ batch ⚠️ ระวัง rm -rf — ลบแล้วลบเลย ไม่มีถังขยะ! ถ้าไม่แน่ใจ ใช้ rm -i (ถามก่อนลบ):
# สร้าง
mkdir -p projects/myapp/src # สร้างโฟลเดอร์ (nested)
touch index.js # สร้างไฟล์เปล่า
# คัดลอก
cp file.txt backup.txt # copy ไฟล์
cp -r src/ src-backup/ # copy โฟลเดอร์ (recursive)
# ย้าย / เปลี่ยนชื่อ
mv old.txt new.txt # rename
mv file.txt /tmp/ # ย้ายไป /tmp
# ลบ (ระวัง! ไม่มี Recycle Bin)
rm file.txt # ลบไฟล์
rm -r folder/ # ลบโฟลเดอร์
rm -rf node_modules/ # ลบแบบ force (ไม่ถาม)
# ลิงก์
ln -s /usr/bin/python3 /usr/bin/python # symbolic link
🐕 ระวัง!rm -rf /จะลบทุกอย่างในเครื่อง — อย่าทำเด็ดขาด! ใช้rm -ri(ถามก่อนลบ) ถ้าไม่แน่ใจ
อ่านไฟล์ — ดูเนื้อหาแบบต่างๆ
Linux มีคำสั่งอ่านไฟล์หลายตัวเหมาะกับสถานการณ์ต่างกัน — cat แสดงทั้งไฟล์ (เหมาะกับไฟล์สั้น), less เลื่อนอ่านได้ (เหมาะกับไฟล์ยาว), head/tail ดูแค่ต้น/ท้ายไฟล์, tail -f ดู log แบบ real-time — DevOps ใช้ tail -f ตลอดเวลาตอน debug:
# ดูทั้งไฟล์
cat config.yml # แสดงทั้งหมด
less /var/log/syslog # เลื่อนดูได้ (กด q ออก)
more /var/log/syslog # เหมือน less แต่ทำได้น้อยกว่า
# ดูเฉพาะส่วน
head -20 access.log # 20 บรรทัดแรก
tail -50 error.log # 50 บรรทัดท้าย
tail -f /var/log/nginx/access.log # ดู real-time! (follow)
# นับ
wc -l file.txt # จำนวนบรรทัด
wc -w file.txt # จำนวนคำ
du -sh folder/ # ขนาดโฟลเดอร์
df -h # ขนาด disk ที่เหลือ
Search & Filter — หาของเก่ง
ความสามารถในการ "หาของ" คือ superpower ของ Linux — grep หาข้อความในไฟล์ (regex supported), find หาไฟล์ตาม name/size/date, awk และ sed ประมวลผลข้อความ เมื่อ combine กับ pipes จะทำงานที่ซับซ้อนได้ในบรรทัดเดียว:
# grep — ค้นหาข้อความในไฟล์
grep "error" /var/log/syslog # หาบรรทัดที่มี "error"
grep -i "warning" app.log # case-insensitive
grep -r "TODO" src/ # ค้นหาในทุกไฟล์ (recursive)
grep -n "password" config.yml # แสดงเลขบรรทัด
grep -c "404" access.log # นับจำนวนที่เจอ
grep -v "DEBUG" app.log # แสดงบรรทัดที่ไม่มี "DEBUG"
# find — หาไฟล์
find / -name "nginx.conf" # หาไฟล์ชื่อ nginx.conf
find . -name "*.log" -mtime -7 # ไฟล์ .log ที่แก้ไขใน 7 วัน
find /var -size +100M # ไฟล์ที่ใหญ่กว่า 100MB
find . -type d -name "node_modules" # หาโฟลเดอร์ชื่อ node_modules
find . -name "*.tmp" -delete # หาแล้วลบ
# awk & sed — text processing
awk '{print $1}' access.log # แสดงคอลัมน์แรก
awk -F: '{print $1}' /etc/passwd # แสดง username ทั้งหมด
sed 's/old/new/g' file.txt # แทนที่ข้อความ
sed -i 's/8080/3000/g' config.yml # แทนที่ในไฟล์จริง
Pipes & Redirection — ต่อท่อ
พลังที่แท้จริงของ Linux คือการ ต่อคำสั่งเข้าด้วยกัน ด้วย pipe |:
# Pipe: output ของคำสั่งซ้าย → input ของคำสั่งขวา
cat access.log | grep "POST" | wc -l
# → นับจำนวน POST requests
# หา 10 IP ที่เข้ามาบ่อยสุด
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
# หา process ที่ใช้ memory เยอะสุด
ps aux | sort -k4 -rn | head -5
# Redirect: บันทึก output ลงไฟล์
echo "Hello" > file.txt # เขียนทับ
echo "World" >> file.txt # เขียนต่อท้าย
ls /fake 2> errors.txt # redirect error
ls /fake > output.txt 2>&1 # redirect ทั้ง output + error
# /dev/null — หลุมดำ (ทิ้ง output)
command > /dev/null 2>&1 # ไม่แสดงอะไรเลย
💡 DevOps Pro Tip: | (pipe) คือสิ่งที่ทำให้ Linux ทรงพลัง — คำสั่งเล็กๆ หลายตัว ต่อกันแล้วทำสิ่งซับซ้อนได้!
Process Management — จัดการโปรเซส
ทุกโปรแกรมที่รันบน Linux คือ "process" ที่มี PID (Process ID) — การจัดการ processes สำคัญมากสำหรับ DevOps: ดูว่า server ใช้ CPU/RAM เท่าไร, หา process ที่กิน resource, kill process ที่ค้าง, รัน process ใน background ให้ทำงานต่อแม้ปิด terminal:
# ดู process
ps aux # แสดงทุก process
ps aux | grep nginx # หา process nginx
top # real-time monitor (กด q ออก)
htop # top เวอร์ชันสวยกว่า
# จัดการ process
kill 1234 # ส่ง SIGTERM (ปิดอย่างสุภาพ)
kill -9 1234 # ส่ง SIGKILL (บังคับปิด)
killall nginx # kill ทุก process ชื่อ nginx
pkill -f "node server.js" # kill ตาม pattern
# Background / Foreground
./script.sh & # รันใน background
nohup ./script.sh & # รัน background + ไม่ตายเมื่อปิด terminal
jobs # ดู background jobs
fg %1 # ดึงกลับมา foreground
# systemd — จัดการ services
sudo systemctl start nginx # เริ่ม
sudo systemctl stop nginx # หยุด
sudo systemctl restart nginx # restart
sudo systemctl status nginx # ดูสถานะ
sudo systemctl enable nginx # เปิดอัตโนมัติตอน boot
sudo journalctl -u nginx -f # ดู log real-time
Permissions — สิทธิ์การเข้าถึง
Linux permissions ควบคุมว่าใครทำอะไรกับไฟล์ได้บ้าง — แบ่งเป็น 3 กลุ่ม: owner (เจ้าของ), group (กลุ่ม), others (คนอื่น) แต่ละกลุ่มมี 3 สิทธิ์: r (read), w (write), x (execute) เข้าใจ permissions เป็นพื้นฐาน security ที่สำคัญที่สุด — web server ไม่ควรรันด้วย root, config files ที่มี secrets ต้อง 600 (owner read/write เท่านั้น):
# ดู permissions
ls -la
# -rw-r--r-- 1 deploy deploy 1234 Mar 7 09:00 app.js
# │├─┤├─┤├─┤
# │ │ │ └── Others: r-- (read only)
# │ │ └───── Group: r-- (read only)
# │ └──────── Owner: rw- (read + write)
# └────────── Type: - (file) / d (directory)
# เปลี่ยน permissions
chmod 755 script.sh # rwxr-xr-x (owner ทำได้หมด, คนอื่น read+execute)
chmod 644 config.yml # rw-r--r-- (owner อ่าน+เขียน, คนอื่นอ่านอย่างเดียว)
chmod +x deploy.sh # เพิ่มสิทธิ์ execute
chmod -R 755 /var/www # recursive ทั้งโฟลเดอร์
# เปลี่ยนเจ้าของ
chown deploy:deploy app.js # เปลี่ยน owner + group
chown -R www-data:www-data /var/www # recursive
| ตัวเลข | สิทธิ์ | ใช้กับ |
|---|---|---|
755 |
rwxr-xr-x |
Scripts, โฟลเดอร์ |
644 |
rw-r--r-- |
Config files, HTML |
600 |
rw------- |
SSH keys, secrets |
700 |
rwx------ |
.ssh directory |
Package Management — ติดตั้ง software
Package manager คือ "app store" ของ Linux — ติดตั้ง, อัพเดท, ลบ software ได้ง่ายๆ แต่ละ distro ใช้คนละตัว: apt (Ubuntu/Debian), yum/dnf (CentOS/RHEL/Fedora), apk (Alpine — ที่ใช้ใน Docker) สิ่งสำคัญคือ apt update ก่อน apt install เสมอ เพื่อให้ได้ package list ล่าสุด:
# Ubuntu/Debian (apt)
sudo apt update # อัพเดท package list
sudo apt upgrade # อัพเดท software ทั้งหมด
sudo apt install nginx # ติดตั้ง
sudo apt remove nginx # ลบ
sudo apt autoremove # ลบ dependencies ที่ไม่ใช้
apt search docker # ค้นหา package
dpkg -l | grep nginx # ดู package ที่ติดตั้ง
# CentOS/RHEL (yum/dnf)
sudo dnf install nginx
sudo dnf update
sudo dnf remove nginx
# Alpine (apk) — ใช้ใน Docker images
apk add --no-cache nginx curl
User Management — จัดการ users
Linux เป็น multi-user OS — แต่ละ user มีสิทธิ์และ home directory แยกกัน root คือ superuser ที่ทำได้ทุกอย่าง (อันตราย!) ควรสร้าง regular user แล้วใช้ sudo เมื่อต้องการสิทธิ์ admin DevOps ต้องจัดการ users บน servers เป็นประจำ:
# สร้าง user
sudo adduser deploy # สร้าง user + home directory
sudo usermod -aG sudo deploy # เพิ่มเข้ากลุ่ม sudo
sudo usermod -aG docker deploy # เพิ่มเข้ากลุ่ม docker
# ข้อมูล user
whoami # เราเป็นใคร
id deploy # ดู uid, gid, groups
groups deploy # ดู groups
# สลับ user
su - deploy # สลับเป็น deploy
sudo -i # สลับเป็น root
sudo -u deploy command # รันคำสั่งในฐานะ deploy
Networking Commands — เครือข่าย
DevOps ต้อง debug network issues บ่อยมาก — server เชื่อมต่อ database ไม่ได้? DNS resolve ผิด? port ถูก firewall block? คำสั่ง networking เหล่านี้ช่วยวินิจฉัยปัญหาได้เร็ว:
# IP & Interface
ip addr show # ดู IP address
ip route show # ดู routing table
hostname -I # ดู IP อย่างย่อ
# ทดสอบ connection
ping -c 4 google.com # ping 4 ครั้ง
curl -I https://example.com # ดู HTTP headers
curl -s https://api.example.com/status | jq . # ดู JSON สวยๆ
wget https://example.com/file.zip # ดาวน์โหลดไฟล์
# Port & Connection
ss -tlnp # ดู port ที่เปิด + process
netstat -tlnp # เหมือน ss (เก่ากว่า)
lsof -i :3000 # ใครใช้ port 3000?
# DNS
dig example.com A +short # ดู IP ของ domain
nslookup example.com # ดู DNS record
Shell Scripting — เขียน script อัตโนมัติ
Shell script คือพลังสูงสุดของ Linux CLI — รวมคำสั่งหลายตัวเป็น automation!
Script พื้นฐาน
Shell script เริ่มด้วย #!/bin/bash (shebang) บอกว่าใช้ bash รัน ทำให้ executable ด้วย chmod +x แล้วรันได้เลย ตัวอย่างด้านล่างแสดง deploy script ที่ DevOps ใช้จริง — build, test, deploy ด้วยคำสั่งเดียว:
#!/bin/bash
# deploy.sh — Deploy script
set -e # หยุดทันทีถ้ามี error
APP_NAME="myapp"
DEPLOY_DIR="/var/www/$APP_NAME"
BACKUP_DIR="/var/backups/$APP_NAME"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
echo "🚀 Deploying $APP_NAME..."
# 1. Backup current version
echo "📦 Creating backup..."
mkdir -p "$BACKUP_DIR"
cp -r "$DEPLOY_DIR" "$BACKUP_DIR/backup_$TIMESTAMP"
# 2. Pull latest code
echo "⬇️ Pulling latest code..."
cd "$DEPLOY_DIR"
git pull origin main
# 3. Install dependencies
echo "📥 Installing dependencies..."
npm ci --production
# 4. Restart service
echo "🔄 Restarting service..."
sudo systemctl restart "$APP_NAME"
# 5. Health check
echo "🏥 Health check..."
sleep 3
if curl -sf http://localhost:3000/health > /dev/null; then
echo "✅ Deploy successful!"
else
echo "❌ Health check failed! Rolling back..."
cp -r "$BACKUP_DIR/backup_$TIMESTAMP"/* "$DEPLOY_DIR/"
sudo systemctl restart "$APP_NAME"
echo "⏪ Rolled back to previous version"
exit 1
fi
# ทำให้รันได้ แล้วรัน
chmod +x deploy.sh
./deploy.sh
Variables, Conditions, Loops
Shell script รองรับ variables, if/else, loops เหมือนภาษาโปรแกรมทั่วไป — แต่ syntax จะแปลกหน่อย (เช่น [[ ]] สำหรับ conditions, $VAR สำหรับอ้างอิงตัวแปร) ตัวอย่างด้านล่างแสดง patterns ที่ใช้บ่อยใน DevOps scripts:
#!/bin/bash
# ตัวแปร
NAME="World"
echo "Hello, $NAME!"
# Condition
if [ -f "/etc/nginx/nginx.conf" ]; then
echo "Nginx config exists"
else
echo "Nginx not installed"
fi
# Loop
for server in web1 web2 web3; do
echo "Checking $server..."
ssh "$server" "uptime"
done
# Loop ตามไฟล์
for log in /var/log/*.log; do
echo "$log: $(wc -l < "$log") lines"
done
# While loop
while true; do
if curl -sf http://localhost:3000/health > /dev/null; then
echo "$(date): OK"
else
echo "$(date): DOWN!"
fi
sleep 30
done
Cron Jobs — ตั้งเวลารันอัตโนมัติ
Cron คือ scheduler ของ Linux — ตั้งเวลาให้ script รันอัตโนมัติ เช่น backup ทุกคืน, cleanup logs ทุกสัปดาห์, health check ทุก 5 นาที Cron expression มี 5 ช่อง: นาที ชั่วโมง วัน เดือน วันในสัปดาห์ ดู syntax ด้วย crontab.guru:
# แก้ไข crontab
crontab -e
# รูปแบบ:
# ┌───────── minute (0-59)
# │ ┌─────── hour (0-23)
# │ │ ┌───── day of month (1-31)
# │ │ │ ┌─── month (1-12)
# │ │ │ │ ┌─ day of week (0-7, 0=Sun)
# │ │ │ │ │
# * * * * * command
# ตัวอย่าง
0 2 * * * /home/deploy/backup.sh # ทุกวัน 02:00
*/5 * * * * curl -s http://localhost:3000/health # ทุก 5 นาที
0 9 * * 1 /home/deploy/weekly-report.sh # ทุกวันจันทร์ 09:00
0 0 1 * * /home/deploy/monthly-cleanup.sh # วันที่ 1 ทุกเดือน
# ดู crontab ปัจจุบัน
crontab -l
# ดู cron logs
grep CRON /var/log/syslog
คำสั่งที่ DevOps ใช้บ่อยที่สุด (Cheat Sheet)
สรุปคำสั่งที่ DevOps engineers ใช้ทุกวัน — เก็บไว้เป็น quick reference ไม่ต้องจำทั้งหมด แค่รู้ว่ามี แล้วกลับมาดูเมื่อต้องใช้:
| หมวด | คำสั่ง | ทำอะไร |
|---|---|---|
| 📁 Files | ls -la | list ไฟล์แบบละเอียด |
| 📁 Files | find . -name "*.log" | หาไฟล์ .log |
| 🔍 Search | grep -r "error" . | ค้นหาข้อความ |
| 📊 Monitor | htop | ดู CPU/Memory real-time |
| 📊 Monitor | df -h | ดู disk space |
| 📊 Monitor | free -h | ดู memory |
| 🔄 Process | ps aux | grep app | หา process |
| 🔄 Process | systemctl status nginx | ดูสถานะ service |
| 🌐 Network | ss -tlnp | ดู port ที่เปิด |
| 🌐 Network | curl -sf URL | ทดสอบ HTTP |
| 📝 Logs | tail -f /var/log/app.log | ดู log real-time |
| 📝 Logs | journalctl -u service -f | ดู systemd log |
สรุป
Linux CLI เป็นทักษะพื้นฐานที่สำคัญที่สุดสำหรับ DevOps — ทุก server, container, CI/CD runner รันบน Linux ยิ่งใช้คล่อง ยิ่งทำงานเร็ว ไม่ต้องจำทุกคำสั่ง แค่รู้ว่า "ทำอะไรได้" แล้ว Google หรือ man command เมื่อต้องการ:
- Navigation =
cd,ls,pwd,tree - File ops =
cp,mv,rm,mkdir,chmod,chown - Search =
grep(ค้นข้อความ),find(ค้นไฟล์),awk/sed(text processing) - Pipes =
|ต่อคำสั่ง,>redirect output — พลังที่แท้จริงของ Linux! - Process =
ps,top,kill,systemctl - Permissions =
chmod 755(scripts),600(secrets),644(configs) - Shell Script = รวมคำสั่งเป็น automation — deploy, backup, health check
- Cron = ตั้งเวลารันอัตโนมัติ 🐧🐕
Across this DevOps series we have covered Docker, K8s, Terraform and Ansible at length — and every one of those tools runs on Linux!
If you cannot work the Linux command line... you are a chef who cannot handle a knife 🔪
So let us go through the Linux commands DevOps engineers use every day, from the basics all the way to shell scripting 🐧🐕
Navigation — Moving Around the File System
Before you can do anything in Linux you have to be able to "travel" through the file system — Linux arranges files as a tree starting at / (root) and descending into /home, /etc, /var, with no drive letters like Windows. Three commands you have to memorise: pwd (where am I), ls (what is here), cd (go there):
# Where am I?
pwd
# /home/deploy
# Go where?
cd /var/log # follow a path
cd ~ # back home
cd .. # up one level
cd - # back where you were (toggle)
# What is here?
ls # list files
ls -la # detailed list + hidden files
ls -lh # human-readable sizes (1.5M, 2.3G)
ls -lt # sort by time (newest on top)
# See the paths as a tree
tree -L 2 # show the structure two levels deep
💡 Tip: press Tab to auto-complete a file or folder name — press it twice to see the choices
Managing Files — Create, Delete, Move, Copy
Handling files from the command line is far faster than through a GUI — create ten folders at once, delete files by pattern, move or copy in batches. ⚠️ Be careful with rm -rf — deleted is deleted, there is no recycle bin! When you are not sure, use rm -i (asks before deleting):
# Create
mkdir -p projects/myapp/src # create a folder (nested)
touch index.js # create an empty file
# Copy
cp file.txt backup.txt # copy a file
cp -r src/ src-backup/ # copy a folder (recursive)
# Move / rename
mv old.txt new.txt # rename
mv file.txt /tmp/ # move to /tmp
# Delete (careful! there is no Recycle Bin)
rm file.txt # delete a file
rm -r folder/ # delete a folder
rm -rf node_modules/ # force delete (no questions asked)
# Links
ln -s /usr/bin/python3 /usr/bin/python # symbolic link
🐕 Careful!rm -rf /will wipe out everything on the machine — never do it! Userm -ri(asks before deleting) when you are unsure
Reading Files — Several Ways to Look Inside
Linux gives you several commands for reading files, each suited to a different situation — cat prints the whole file (good for short ones), less lets you scroll (good for long ones), head/tail show just the start or the end, and tail -f follows a log in real time — DevOps engineers live in tail -f while debugging:
# View the whole file
cat config.yml # print everything
less /var/log/syslog # scrollable (press q to quit)
more /var/log/syslog # like less, but does less
# View just part of it
head -20 access.log # first 20 lines
tail -50 error.log # last 50 lines
tail -f /var/log/nginx/access.log # real time! (follow)
# Count
wc -l file.txt # number of lines
wc -w file.txt # number of words
du -sh folder/ # size of a folder
df -h # disk space remaining
Search & Filter — Good at Finding Things
The ability to "find things" is Linux's superpower — grep searches text inside files (regex supported), find locates files by name, size or date, and awk and sed process text. Combine them with pipes and a single line will do genuinely complicated work:
# grep — search for text inside files
grep "error" /var/log/syslog # find lines containing "error"
grep -i "warning" app.log # case-insensitive
grep -r "TODO" src/ # search every file (recursive)
grep -n "password" config.yml # show line numbers
grep -c "404" access.log # count the matches
grep -v "DEBUG" app.log # show lines WITHOUT "DEBUG"
# find — locate files
find / -name "nginx.conf" # find a file named nginx.conf
find . -name "*.log" -mtime -7 # .log files changed in the last 7 days
find /var -size +100M # files larger than 100MB
find . -type d -name "node_modules" # find folders named node_modules
find . -name "*.tmp" -delete # find, then delete
# awk & sed — text processing
awk '{print $1}' access.log # print the first column
awk -F: '{print $1}' /etc/passwd # print every username
sed 's/old/new/g' file.txt # replace text
sed -i 's/8080/3000/g' config.yml # replace in the actual file
Pipes & Redirection — Connecting the Plumbing
The real power of Linux is joining commands together with the pipe |:
# Pipe: output of the left command → input of the right one
cat access.log | grep "POST" | wc -l
# → count the POST requests
# Find the 10 most frequent visiting IPs
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
# Find the processes using the most memory
ps aux | sort -k4 -rn | head -5
# Redirect: save output to a file
echo "Hello" > file.txt # overwrite
echo "World" >> file.txt # append
ls /fake 2> errors.txt # redirect errors
ls /fake > output.txt 2>&1 # redirect both output and errors
# /dev/null — the black hole (throw output away)
command > /dev/null 2>&1 # show nothing at all
💡 DevOps Pro Tip: | (the pipe) is what makes Linux powerful — several small commands, chained together, do something complicated!
Process Management — Handling Processes
Every program running on Linux is a "process" with a PID (Process ID) — and managing processes matters enormously in DevOps: seeing how much CPU/RAM a server is using, finding the process eating the resources, killing a process that has hung, running a process in the background so it keeps working after you close the terminal:
# Look at processes
ps aux # show every process
ps aux | grep nginx # find the nginx process
top # real-time monitor (press q to quit)
htop # a prettier version of top
# Manage a process
kill 1234 # send SIGTERM (a polite shutdown)
kill -9 1234 # send SIGKILL (force it closed)
killall nginx # kill every process named nginx
pkill -f "node server.js" # kill by pattern
# Background / Foreground
./script.sh & # run in the background
nohup ./script.sh & # background + survives closing the terminal
jobs # list background jobs
fg %1 # pull one back to the foreground
# systemd — managing services
sudo systemctl start nginx # start
sudo systemctl stop nginx # stop
sudo systemctl restart nginx # restart
sudo systemctl status nginx # check status
sudo systemctl enable nginx # start automatically at boot
sudo journalctl -u nginx -f # follow the log in real time
Permissions — Who Can Touch What
Linux permissions control who may do what to a file — split into three groups: owner, group and others. Each group holds three rights: r (read), w (write), x (execute). Understanding permissions is the most important security fundamental there is — a web server should never run as root, and a config file holding secrets must be 600 (owner read/write only):
# Look at permissions
ls -la
# -rw-r--r-- 1 deploy deploy 1234 Mar 7 09:00 app.js
# │├─┤├─┤├─┤
# │ │ │ └── Others: r-- (read only)
# │ │ └───── Group: r-- (read only)
# │ └──────── Owner: rw- (read + write)
# └────────── Type: - (file) / d (directory)
# Change permissions
chmod 755 script.sh # rwxr-xr-x (owner can do everything, others read+execute)
chmod 644 config.yml # rw-r--r-- (owner reads and writes, others read only)
chmod +x deploy.sh # add the execute bit
chmod -R 755 /var/www # recursive, the whole folder
# Change the owner
chown deploy:deploy app.js # change owner + group
chown -R www-data:www-data /var/www # recursive
| Number | Permissions | Used for |
|---|---|---|
755 |
rwxr-xr-x |
Scripts, folders |
644 |
rw-r--r-- |
Config files, HTML |
600 |
rw------- |
SSH keys, secrets |
700 |
rwx------ |
.ssh directory |
Package Management — Installing Software
A package manager is the "app store" of Linux — installing, updating and removing software the easy way. Each distro uses its own: apt (Ubuntu/Debian), yum/dnf (CentOS/RHEL/Fedora), apk (Alpine — the one inside Docker). The important habit is always running apt update before apt install, so you are working from the latest package list:
# Ubuntu/Debian (apt)
sudo apt update # refresh the package list
sudo apt upgrade # update all software
sudo apt install nginx # install
sudo apt remove nginx # remove
sudo apt autoremove # remove unused dependencies
apt search docker # search for a package
dpkg -l | grep nginx # list installed packages
# CentOS/RHEL (yum/dnf)
sudo dnf install nginx
sudo dnf update
sudo dnf remove nginx
# Alpine (apk) — used in Docker images
apk add --no-cache nginx curl
User Management — Handling Users
Linux is a multi-user OS — each user has separate rights and a separate home directory. root is the superuser who can do anything (dangerous!). Create a regular user instead and reach for sudo when you need admin rights. DevOps engineers manage users on servers as a matter of routine:
# Create a user
sudo adduser deploy # create the user + home directory
sudo usermod -aG sudo deploy # add to the sudo group
sudo usermod -aG docker deploy # add to the docker group
# User information
whoami # who am I
id deploy # show uid, gid, groups
groups deploy # show the groups
# Switch user
su - deploy # switch to deploy
sudo -i # switch to root
sudo -u deploy command # run a command as deploy
Networking Commands — The Network
DevOps engineers debug network problems constantly — the server cannot reach the database? DNS resolving to the wrong place? A port blocked by the firewall? These networking commands get you to a diagnosis quickly:
# IP & Interface
ip addr show # show the IP address
ip route show # show the routing table
hostname -I # show the IP, briefly
# Test a connection
ping -c 4 google.com # ping four times
curl -I https://example.com # look at the HTTP headers
curl -s https://api.example.com/status | jq . # pretty-print the JSON
wget https://example.com/file.zip # download a file
# Ports & connections
ss -tlnp # show open ports + the process
netstat -tlnp # same as ss (the older tool)
lsof -i :3000 # who is using port 3000?
# DNS
dig example.com A +short # show the domain's IP
nslookup example.com # show the DNS record
Shell Scripting — Writing Automation
A shell script is the Linux CLI at full power — many commands gathered into automation!
A Basic Script
A shell script starts with #!/bin/bash (the shebang), which says to run it with bash. Make it executable with chmod +x and it runs. The example below is a deploy script of the kind DevOps engineers actually use — build, test and deploy in a single command:
#!/bin/bash
# deploy.sh — Deploy script
set -e # stop immediately on any error
APP_NAME="myapp"
DEPLOY_DIR="/var/www/$APP_NAME"
BACKUP_DIR="/var/backups/$APP_NAME"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
echo "🚀 Deploying $APP_NAME..."
# 1. Backup current version
echo "📦 Creating backup..."
mkdir -p "$BACKUP_DIR"
cp -r "$DEPLOY_DIR" "$BACKUP_DIR/backup_$TIMESTAMP"
# 2. Pull latest code
echo "⬇️ Pulling latest code..."
cd "$DEPLOY_DIR"
git pull origin main
# 3. Install dependencies
echo "📥 Installing dependencies..."
npm ci --production
# 4. Restart service
echo "🔄 Restarting service..."
sudo systemctl restart "$APP_NAME"
# 5. Health check
echo "🏥 Health check..."
sleep 3
if curl -sf http://localhost:3000/health > /dev/null; then
echo "✅ Deploy successful!"
else
echo "❌ Health check failed! Rolling back..."
cp -r "$BACKUP_DIR/backup_$TIMESTAMP"/* "$DEPLOY_DIR/"
sudo systemctl restart "$APP_NAME"
echo "⏪ Rolled back to previous version"
exit 1
fi
# Make it executable, then run it
chmod +x deploy.sh
./deploy.sh
Variables, Conditions, Loops
Shell scripts support variables, if/else and loops like any other language — the syntax is just a little strange ([[ ]] for conditions, $VAR to reference a variable). The example below shows the patterns that come up most often in DevOps scripts:
#!/bin/bash
# Variables
NAME="World"
echo "Hello, $NAME!"
# Condition
if [ -f "/etc/nginx/nginx.conf" ]; then
echo "Nginx config exists"
else
echo "Nginx not installed"
fi
# Loop
for server in web1 web2 web3; do
echo "Checking $server..."
ssh "$server" "uptime"
done
# Loop over files
for log in /var/log/*.log; do
echo "$log: $(wc -l < "$log") lines"
done
# While loop
while true; do
if curl -sf http://localhost:3000/health > /dev/null; then
echo "$(date): OK"
else
echo "$(date): DOWN!"
fi
sleep 30
done
Cron Jobs — Scheduling Automatic Runs
Cron is the Linux scheduler — it runs a script automatically at a set time: a backup every night, a log cleanup every week, a health check every five minutes. A cron expression has five fields: minute hour day month day-of-week. Check the syntax at crontab.guru:
# Edit the crontab
crontab -e
# The format:
# ┌───────── minute (0-59)
# │ ┌─────── hour (0-23)
# │ │ ┌───── day of month (1-31)
# │ │ │ ┌─── month (1-12)
# │ │ │ │ ┌─ day of week (0-7, 0=Sun)
# │ │ │ │ │
# * * * * * command
# Examples
0 2 * * * /home/deploy/backup.sh # every day at 02:00
*/5 * * * * curl -s http://localhost:3000/health # every 5 minutes
0 9 * * 1 /home/deploy/weekly-report.sh # every Monday at 09:00
0 0 1 * * /home/deploy/monthly-cleanup.sh # the 1st of every month
# Show the current crontab
crontab -l
# Look at the cron logs
grep CRON /var/log/syslog
The Commands DevOps Uses Most (Cheat Sheet)
A summary of the commands DevOps engineers reach for every day — keep it as a quick reference. You do not need to memorise all of it; just know it exists, and come back when you need it:
| Category | Command | What it does |
|---|---|---|
| 📁 Files | ls -la | list files in detail |
| 📁 Files | find . -name "*.log" | find .log files |
| 🔍 Search | grep -r "error" . | search for text |
| 📊 Monitor | htop | watch CPU/memory in real time |
| 📊 Monitor | df -h | check disk space |
| 📊 Monitor | free -h | check memory |
| 🔄 Process | ps aux | grep app | find a process |
| 🔄 Process | systemctl status nginx | check a service's status |
| 🌐 Network | ss -tlnp | show open ports |
| 🌐 Network | curl -sf URL | test HTTP |
| 📝 Logs | tail -f /var/log/app.log | follow a log in real time |
| 📝 Logs | journalctl -u service -f | follow a systemd log |
Summary
The Linux CLI is the single most important foundational skill in DevOps — every server, container and CI/CD runner runs on Linux, and the more fluent you are, the faster you work. You do not have to memorise every command; just know what is possible, then reach for Google or the man command when you need the detail:
- Navigation =
cd,ls,pwd,tree - File ops =
cp,mv,rm,mkdir,chmod,chown - Search =
grep(find text),find(find files),awk/sed(text processing) - Pipes =
|to chain commands,>to redirect output — the real power of Linux! - Process =
ps,top,kill,systemctl - Permissions =
chmod 755(scripts),600(secrets),644(configs) - Shell Script = commands gathered into automation — deploy, backup, health check
- Cron = scheduling automatic runs 🐧🐕