
ทำไม IT ต้องรู้ Linux?
Linux เป็นระบบปฏิบัติการ (Operating System) ที่ทำงานอยู่เบื้องหลังโครงสร้างพื้นฐานของอินเทอร์เน็ตและระบบ IT ทั่วโลก Server มากกว่า 96% บน Cloud (AWS, Azure, GCP) รัน Linux รวมถึง Web Server, Database Server, Container (Docker/Kubernetes) และอุปกรณ์ IoT ทั้งหมดล้วนใช้ Linux เป็นหลัก
สำหรับคนที่ทำงาน IT ไม่ว่าจะเป็น System Admin, DevOps Engineer, Backend Developer, Security Engineer หรือ Network Engineer ความรู้ Linux เป็น ทักษะพื้นฐาน ที่ขาดไม่ได้ เพราะคุณจะต้องเข้าไปจัดการ Server ผ่าน Command Line ทุกวัน
Linux vs Windows Server — เปรียบเทียบ
| เปรียบเทียบ | Linux Server | Windows Server |
|---|---|---|
| ค่าลิขสิทธิ์ | ฟรี (Open Source) | มีค่าใช้จ่าย (License) |
| ส่วนแบ่งตลาด Server | ~96% (Cloud/Web) | ~30% (Enterprise/AD) |
| การจัดการ | CLI เป็นหลัก (SSH) | GUI + CLI (RDP/PowerShell) |
| ความเสถียร | สูงมาก (Uptime หลายปี) | ดี (ต้อง Reboot บ่อยกว่า) |
| ทรัพยากร | ใช้น้อย (ไม่มี GUI) | ใช้มาก (GUI + Services) |
| Security | แข็งแกร่ง (Permission model) | มี Patch บ่อย (ตลาดใหญ่) |
| Container/Cloud | Native support | Support แต่จำกัดกว่า |
| Learning Curve | สูง (ต้องเรียน CLI) | ต่ำกว่า (GUI ช่วย) |
เลือก Linux Distro ไหนดี?
Linux มีหลาย Distribution (Distro) ให้เลือก แต่ละตัวเหมาะกับการใช้งานต่างกัน สำหรับ IT มือใหม่แนะนำให้เริ่มจาก Distro ที่มี Community ใหญ่และมี Documentation เยอะ
| Distro | Based On | Package Manager | เหมาะกับ | ความนิยม |
|---|---|---|---|---|
| Ubuntu Server | Debian | apt | เริ่มต้น, Cloud, Web Server | สูงมาก |
| Rocky Linux | RHEL | dnf/yum | Enterprise, Production | สูง |
| AlmaLinux | RHEL | dnf/yum | Enterprise, CentOS ทดแทน | สูง |
| Debian | – | apt | Server ที่ต้องการเสถียร | สูง |
| Fedora Server | – | dnf | ทดสอบ Technology ใหม่ | กลาง |
| openSUSE Leap | SUSE | zypper | Enterprise (ยุโรป) | กลาง |
คำแนะนำ: เริ่มต้นด้วย Ubuntu Server 24.04 LTS เพราะ Documentation เยอะที่สุด Community ใหญ่สุด และมีในทุก Cloud Provider ถ้าต้องทำงานกับ Enterprise/Production ค่อยเรียนรู้ Rocky Linux (สาย RHEL) เพิ่ม
ติดตั้ง Linux — วิธีที่แนะนำ
# วิธีที่ 1: Virtual Machine (แนะนำสำหรับมือใหม่!)
# ติดตั้ง VirtualBox หรือ VMware
# → สร้าง VM → ใส่ ISO Ubuntu Server → ติดตั้ง
# ข้อดี: ผิดพลาดก็ลบแล้วสร้างใหม่ได้
# วิธีที่ 2: WSL2 (Windows Subsystem for Linux)
# สำหรับผู้ใช้ Windows 10/11
wsl --install # ติดตั้ง WSL2 + Ubuntu
wsl --install -d Ubuntu-24.04 # เลือก Distro
# ข้อดี: ใช้ Linux บน Windows ได้เลย ไม่ต้อง VM
# วิธีที่ 3: Cloud Server (ทดสอบจริง!)
# สมัคร AWS Free Tier / DigitalOcean / Linode
# สร้าง VPS → เลือก Ubuntu 24.04
# SSH เข้าไปใช้งาน
ssh user@your-server-ip
# วิธีที่ 4: Docker Container (เร็วที่สุด!)
docker run -it ubuntu:24.04 bash
# ได้ Ubuntu CLI ทันที ลบง่ายสร้างใหม่ง่าย
Linux File System — โครงสร้างไดเรกทอรี
# โครงสร้าง Linux File System
# ทุกอย่างเริ่มจาก / (Root)
#
# /
# ├── bin/ → Binary (คำสั่งพื้นฐาน: ls, cp, mv)
# ├── boot/ → Boot loader (kernel, grub)
# ├── dev/ → Device files (disk, USB, terminal)
# ├── etc/ → Configuration files (ตั้งค่าทุกอย่าง!)
# │ ├── nginx/ → Nginx config
# │ ├── ssh/ → SSH config
# │ └── cron.d/ → Cron jobs
# ├── home/ → Home directories ของ Users
# │ └── bom/ → User "bom" home directory
# ├── lib/ → Shared libraries
# ├── media/ → Mount point (USB, CD)
# ├── mnt/ → Mount point (manual mount)
# ├── opt/ → Optional software (third-party)
# ├── proc/ → Process information (virtual)
# ├── root/ → Root user home directory
# ├── run/ → Runtime data (PID files)
# ├── sbin/ → System binaries (admin commands)
# ├── srv/ → Service data (web, FTP)
# ├── sys/ → System information (virtual)
# ├── tmp/ → Temporary files (ถูกลบเมื่อ reboot)
# ├── usr/ → User programs
# │ ├── bin/ → User commands
# │ ├── lib/ → Libraries
# │ └── share/ → Shared data
# └── var/ → Variable data
# ├── log/ → Log files (สำคัญมาก!)
# ├── www/ → Web server files
# └── lib/ → Database, package data
#
# จำง่าย ๆ:
# /etc = Config, /var/log = Logs, /home = User data
# /tmp = Temporary, /opt = Third-party apps
คำสั่งพื้นฐาน — Navigation & File Operations
pwd, ls, cd — รู้อยู่ไหน ดูมีอะไร ไปไหน
# pwd (Print Working Directory) — อยู่ไหน?
pwd
# /home/bom
# ls (List) — มีอะไรบ้าง?
ls # แสดงไฟล์/โฟลเดอร์
ls -l # แบบละเอียด (long format)
ls -la # รวม Hidden files (ขึ้นต้นด้วย .)
ls -lh # ขนาดไฟล์แบบอ่านง่าย (KB, MB, GB)
ls -lt # เรียงตามเวลาแก้ไข (ใหม่สุดก่อน)
ls -lS # เรียงตามขนาด (ใหญ่สุดก่อน)
ls -R # แสดง Recursive (รวม subdirectories)
# ตัวอย่าง ls -la output:
# drwxr-xr-x 5 bom bom 4096 Apr 16 10:00 .
# drwxr-xr-x 3 root root 4096 Jan 01 00:00 ..
# -rw-r--r-- 1 bom bom 220 Jan 01 00:00 .bashrc
# -rw-r--r-- 1 bom bom 1024 Apr 16 09:30 report.txt
# drwxr-xr-x 2 bom bom 4096 Apr 15 14:00 projects
#
# d = directory, - = file
# rwx = read/write/execute (owner/group/others)
# cd (Change Directory) — ไปไหน?
cd /var/log # ไปที่ /var/log
cd ~ # กลับ Home directory
cd .. # ขึ้นไป 1 ระดับ (Parent directory)
cd ../.. # ขึ้นไป 2 ระดับ
cd - # กลับไปที่เดิม (Previous directory)
cd # กลับ Home (เหมือน cd ~)
mkdir, touch, cp, mv, rm — สร้าง คัดลอก ย้าย ลบ
# mkdir (Make Directory) — สร้างโฟลเดอร์
mkdir myproject # สร้างโฟลเดอร์
mkdir -p project/src/lib # สร้างทั้ง Path (-p = parents)
mkdir -p {logs,config,data} # สร้างหลายโฟลเดอร์พร้อมกัน
# touch — สร้างไฟล์เปล่า / อัปเดต timestamp
touch newfile.txt # สร้างไฟล์เปล่า
touch file1.txt file2.txt # สร้างหลายไฟล์
# cp (Copy) — คัดลอก
cp file.txt backup.txt # Copy ไฟล์
cp file.txt /tmp/ # Copy ไปอีกที่
cp -r mydir/ /tmp/mydir/ # Copy โฟลเดอร์ (-r = recursive)
cp -p file.txt backup.txt # Copy รักษา Permission/Timestamp
# mv (Move) — ย้าย / เปลี่ยนชื่อ
mv oldname.txt newname.txt # เปลี่ยนชื่อ
mv file.txt /tmp/ # ย้ายไฟล์
mv mydir/ /opt/ # ย้ายโฟลเดอร์
# rm (Remove) — ลบ ⚠️ ระวัง! ไม่มี Trash Bin!
rm file.txt # ลบไฟล์
rm -r mydir/ # ลบโฟลเดอร์ (-r = recursive)
rm -rf mydir/ # ลบโดยไม่ถาม (-f = force)
rm -i file.txt # ถามก่อนลบ (-i = interactive)
# ⚠️ อย่า rm -rf / หรือ rm -rf * เด็ดขาด!
อ่านไฟล์ — cat, head, tail, less, grep
# cat (Concatenate) — แสดงเนื้อหาทั้งไฟล์
cat file.txt # แสดงทั้งไฟล์
cat -n file.txt # แสดงพร้อมเลขบรรทัด
cat file1.txt file2.txt # แสดงหลายไฟล์ต่อกัน
# head / tail — ดูต้น/ท้ายไฟล์
head file.txt # 10 บรรทัดแรก (default)
head -20 file.txt # 20 บรรทัดแรก
tail file.txt # 10 บรรทัดสุดท้าย
tail -50 file.txt # 50 บรรทัดสุดท้าย
tail -f /var/log/syslog # Follow! ดู Log แบบ Real-time ⭐
tail -f /var/log/nginx/access.log # ดู Nginx log สด
# less — อ่านไฟล์แบบ Scroll (เหมาะกับไฟล์ใหญ่)
less /var/log/syslog
# Navigation: Space=หน้าถัดไป, b=หน้าก่อน, /=ค้นหา, q=ออก
# wc (Word Count) — นับ
wc -l file.txt # นับบรรทัด
wc -w file.txt # นับคำ
wc -c file.txt # นับ Bytes
grep — ค้นหาข้อความ (สำคัญมาก!)
# grep — ค้นหาข้อความในไฟล์ (ใช้ทุกวัน!)
grep "error" /var/log/syslog # ค้นหาคำว่า "error"
grep -i "error" /var/log/syslog # -i = Case insensitive
grep -n "error" /var/log/syslog # -n = แสดงเลขบรรทัด
grep -c "error" /var/log/syslog # -c = นับจำนวนที่เจอ
grep -r "password" /etc/ # -r = ค้นหา Recursive
grep -v "debug" /var/log/syslog # -v = แสดงบรรทัดที่ไม่ตรง
grep -A 3 "error" syslog # -A 3 = แสดง 3 บรรทัดหลัง match
grep -B 2 "error" syslog # -B 2 = แสดง 2 บรรทัดก่อน match
grep -C 2 "error" syslog # -C 2 = แสดง 2 บรรทัดรอบ match
# grep + Regex
grep "^root" /etc/passwd # บรรทัดที่ขึ้นต้นด้วย root
grep "bash$" /etc/passwd # บรรทัดที่ลงท้ายด้วย bash
grep -E "[0-9]{3}\.[0-9]{3}" file.txt # Extended Regex
# grep + Pipe (ใช้บ่อยมาก!)
cat /var/log/syslog | grep "error"
ps aux | grep nginx # หา Process nginx
dpkg -l | grep mysql # หา Package ที่ติดตั้ง
netstat -tulpn | grep :80 # หา Port 80
find — ค้นหาไฟล์
# find — ค้นหาไฟล์ในระบบ
find /home -name "*.txt" # ค้นหาไฟล์ .txt ใน /home
find /var/log -name "*.log" -mtime -7 # Log ที่แก้ไขใน 7 วัน
find / -name "nginx.conf" # หาไฟล์ชื่อ nginx.conf ทั้งระบบ
find /tmp -size +100M # ไฟล์ใหญ่กว่า 100MB
find /home -type d # ค้นหาเฉพาะ Directory
find /home -type f # ค้นหาเฉพาะ File
find /home -empty # ค้นหาไฟล์/โฟลเดอร์ว่าง
find /var/log -name "*.log" -size +50M -exec ls -lh {} \; # หา + แสดง
find /tmp -mtime +30 -delete # ลบไฟล์เก่ากว่า 30 วัน
Permission & Ownership — chmod, chown
# Permission ใน Linux
# แต่ละไฟล์มี 3 ระดับ Permission:
# - Owner (u) = เจ้าของ
# - Group (g) = กลุ่ม
# - Others (o) = คนอื่น
#
# แต่ละระดับมี 3 สิทธิ์:
# - r (Read) = อ่าน = 4
# - w (Write) = เขียน = 2
# - x (Execute) = รัน = 1
#
# ตัวอย่าง: -rwxr-xr--
# Owner: rwx (7) = อ่าน+เขียน+รัน
# Group: r-x (5) = อ่าน+รัน
# Others: r-- (4) = อ่านอย่างเดียว
# → chmod 754 file.txt
# chmod (Change Mode) — เปลี่ยน Permission
chmod 755 script.sh # rwxr-xr-x (ปกติสำหรับ script)
chmod 644 file.txt # rw-r--r-- (ปกติสำหรับ config)
chmod 600 secret.key # rw------- (เฉพาะเจ้าของ)
chmod +x script.sh # เพิ่มสิทธิ์ Execute
chmod -w file.txt # ลบสิทธิ์ Write
chmod -R 755 mydir/ # เปลี่ยนทั้ง Directory (-R = recursive)
# chown (Change Owner) — เปลี่ยนเจ้าของ
chown bom file.txt # เปลี่ยนเจ้าของเป็น bom
chown bom:www-data file.txt # เปลี่ยนทั้ง Owner และ Group
chown -R www-data:www-data /var/www/ # เปลี่ยนทั้ง Directory
# Permission ที่ใช้บ่อย:
# 755 = rwxr-xr-x → Directory, Script
# 644 = rw-r--r-- → Config, HTML, CSS
# 600 = rw------- → SSH Key, Password file
# 700 = rwx------ → .ssh directory
# 777 = rwxrwxrwx → อย่าใช้! (ไม่ปลอดภัย)
User Management — จัดการผู้ใช้
# สร้าง/จัดการ User
sudo useradd -m -s /bin/bash newuser # สร้าง User + Home + Shell
sudo useradd -m -G sudo,www-data deploy # สร้าง User + Groups
sudo passwd newuser # ตั้ง Password
sudo usermod -aG docker bom # เพิ่ม User เข้า Group
sudo userdel -r olduser # ลบ User + Home directory
# ดูข้อมูล User
whoami # ฉันเป็นใคร?
id # UID, GID, Groups
id bom # ดูข้อมูล User อื่น
groups bom # ดู Groups ที่ User อยู่
cat /etc/passwd # รายชื่อ User ทั้งหมด
cat /etc/group # รายชื่อ Group ทั้งหมด
last # ดูประวัติ Login
w # ดูว่าใคร Login อยู่
# sudo — รัน Command ด้วยสิทธิ์ Root
sudo apt update # รัน apt ด้วยสิทธิ์ Root
sudo -i # เปลี่ยนเป็น Root Shell (ระวัง!)
sudo su - otheruser # เปลี่ยนเป็น User อื่น
Package Management — ติดตั้ง/จัดการโปรแกรม
apt (Ubuntu/Debian)
# apt — Package Manager สำหรับ Ubuntu/Debian
sudo apt update # อัปเดตรายการ Package
sudo apt upgrade # อัปเกรด Package ทั้งหมด
sudo apt install nginx # ติดตั้ง Nginx
sudo apt install -y git curl wget # ติดตั้งหลายตัว (-y = ไม่ถาม)
sudo apt remove nginx # ลบ Package
sudo apt purge nginx # ลบ Package + Config
sudo apt autoremove # ลบ Package ที่ไม่ใช้
apt search mysql # ค้นหา Package
apt show nginx # ดูข้อมูล Package
dpkg -l | grep nginx # ดู Package ที่ติดตั้ง
dnf (Rocky Linux/RHEL/Fedora)
# dnf — Package Manager สำหรับ RHEL-based
sudo dnf update # อัปเดต Package ทั้งหมด
sudo dnf install nginx # ติดตั้ง
sudo dnf install -y git curl wget # ติดตั้งหลายตัว
sudo dnf remove nginx # ลบ
sudo dnf search mysql # ค้นหา
sudo dnf info nginx # ดูข้อมูล
sudo dnf list installed # ดู Package ที่ติดตั้ง
sudo dnf group install "Development Tools" # ติดตั้ง Group
systemd — จัดการ Services
# systemctl — จัดการ Services (สำคัญมาก!)
sudo systemctl start nginx # เริ่ม Service
sudo systemctl stop nginx # หยุด Service
sudo systemctl restart nginx # Restart
sudo systemctl reload nginx # Reload Config (ไม่ Downtime)
sudo systemctl status nginx # ดูสถานะ
sudo systemctl enable nginx # เปิด Auto-start ตอน Boot
sudo systemctl disable nginx # ปิด Auto-start
sudo systemctl is-active nginx # ตรวจว่ารันอยู่ไหม
sudo systemctl is-enabled nginx # ตรวจว่า Auto-start ไหม
# ดู Services ทั้งหมด
systemctl list-units --type=service # Service ที่รันอยู่
systemctl list-units --type=service --state=running # เฉพาะที่ Running
# journalctl — ดู Log ของ Services
journalctl -u nginx # ดู Log ของ Nginx
journalctl -u nginx -f # Follow (ดู Real-time)
journalctl -u nginx --since today # Log วันนี้
journalctl -u nginx --since "1 hour ago" # 1 ชั่วโมงที่แล้ว
journalctl -u nginx -n 50 # 50 บรรทัดล่าสุด
journalctl --disk-usage # ดูว่า Log ใช้เนื้อที่เท่าไร
sudo journalctl --vacuum-size=500M # จำกัด Log ไม่เกิน 500MB
Networking Commands — คำสั่งเครือข่าย
# ip — ดูข้อมูล Network (แทน ifconfig)
ip addr show # ดู IP Address ทุก Interface
ip addr show eth0 # ดู IP ของ Interface เฉพาะ
ip route show # ดู Routing Table
ip link show # ดู Network Interfaces
# ss — ดู Socket/Port (แทน netstat)
ss -tulpn # ดู Port ที่เปิดอยู่ (TCP/UDP)
ss -tulpn | grep :80 # หา Port 80
ss -s # สรุปสถานะ Connection
# ping — ทดสอบ Connectivity
ping google.com # Ping (Ctrl+C หยุด)
ping -c 5 google.com # Ping 5 ครั้ง
ping -c 3 192.168.1.1 # Ping Gateway
# traceroute — ดูเส้นทาง Network
traceroute google.com # ดูเส้นทางไปถึง Google
traceroute -n 8.8.8.8 # ไม่ Resolve DNS
# curl — ส่ง HTTP Request (ใช้ทดสอบ API/Web)
curl https://example.com # GET request
curl -I https://example.com # ดู Headers อย่างเดียว
curl -o file.html https://example.com # Download
curl -X POST -d "name=bom" https://api.example.com # POST request
curl -s https://api.example.com/health | jq . # ดู JSON สวย ๆ
# wget — Download ไฟล์
wget https://example.com/file.tar.gz # Download
wget -O output.tar.gz https://example.com/f # ตั้งชื่อ Output
# DNS
dig example.com # ดู DNS Record
nslookup example.com # ค้นหา DNS
host example.com # ค้นหา DNS (สั้นกว่า)
cat /etc/resolv.conf # ดู DNS Server ที่ใช้
Text Editors — nano และ vim
nano — ง่ายสำหรับมือใหม่
# nano — Text Editor ที่ง่ายที่สุด (แนะนำสำหรับมือใหม่!)
nano file.txt # เปิด/สร้างไฟล์
sudo nano /etc/nginx/nginx.conf # แก้ไข Config
# Shortcuts ที่ต้องจำ:
# Ctrl+O → Save (Write Out)
# Ctrl+X → Exit
# Ctrl+K → Cut line
# Ctrl+U → Paste line
# Ctrl+W → Search
# Ctrl+G → Help
vim — ทรงพลังสำหรับ Pro
# vim — Text Editor ที่ทรงพลังที่สุด (Learning Curve สูง!)
vim file.txt # เปิดไฟล์
# vim มี 3 Mode:
# 1. Normal Mode (default) — ใช้ Navigation
# 2. Insert Mode (กด i) — พิมพ์ข้อความ
# 3. Command Mode (กด :) — สั่งงาน
# คำสั่งพื้นฐาน vim:
# i → เข้า Insert Mode (เริ่มพิมพ์)
# Esc → กลับ Normal Mode
# :w → Save
# :q → Quit
# :wq → Save + Quit
# :q! → Quit ไม่ Save (Force)
# dd → Delete บรรทัด
# yy → Copy บรรทัด
# p → Paste
# /keyword → ค้นหา
# u → Undo
# Ctrl+r → Redo
# gg → ไปบรรทัดแรก
# G → ไปบรรทัดสุดท้าย
# สำหรับมือใหม่: เริ่มจาก nano ก่อน แล้วค่อยเรียน vim ทีหลัง
SSH — Remote Access
# SSH (Secure Shell) — เชื่อมต่อ Server ระยะไกล
ssh user@server-ip # เชื่อมต่อ
ssh -p 2222 user@server-ip # เชื่อมต่อ Port อื่น
ssh -i ~/.ssh/mykey.pem user@ip # ใช้ SSH Key
# สร้าง SSH Key (แนะนำ Ed25519!)
ssh-keygen -t ed25519 -C "bom@company"
# → สร้าง ~/.ssh/id_ed25519 (Private Key)
# → สร้าง ~/.ssh/id_ed25519.pub (Public Key)
# คัดลอก Public Key ไปยัง Server
ssh-copy-id user@server-ip
# หรือ Copy เอง:
cat ~/.ssh/id_ed25519.pub | ssh user@server-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
# SCP — Copy ไฟล์ผ่าน SSH
scp file.txt user@server:/tmp/ # Upload
scp user@server:/var/log/app.log ./ # Download
scp -r mydir/ user@server:/opt/ # Upload โฟลเดอร์
# SSH Config — ตั้งค่าลัด
# ~/.ssh/config
# Host myserver
# HostName 192.168.1.100
# User deploy
# Port 2222
# IdentityFile ~/.ssh/id_ed25519
#
# ใช้: ssh myserver (แทน ssh -p 2222 -i ~/.ssh/id_ed25519 [email protected])
Process Management — จัดการ Process
# ps — ดู Process ที่รันอยู่
ps aux # ดูทุก Process
ps aux | grep nginx # หา Process เฉพาะ
ps -ef --forest # แสดงแบบ Tree
# top / htop — Monitor แบบ Real-time
top # Monitor CPU/RAM/Process
htop # Monitor แบบสวย (ต้องติดตั้ง)
# Navigation: q=ออก, k=kill process, F6=sort
# kill — หยุด Process
kill PID # ส่ง SIGTERM (หยุดอย่างนุ่มนวล)
kill -9 PID # ส่ง SIGKILL (บังคับหยุด!)
kill -HUP PID # Reload (เช่น Nginx)
killall nginx # Kill ทุก Process ชื่อ nginx
pkill -f "python3 app.py" # Kill ตามชื่อคำสั่ง
# ดูทรัพยากรระบบ
free -h # ดู RAM
df -h # ดู Disk Space
du -sh /var/log/ # ดูขนาด Directory
du -sh /var/log/* | sort -rh | head # Top 10 โฟลเดอร์ใหญ่สุด
uptime # ดูเวลาที่ Server รันมา
cron — ตั้งเวลารัน Automated Tasks
# cron — ตั้งเวลารันคำสั่งอัตโนมัติ
crontab -e # แก้ไข Cron jobs
crontab -l # ดู Cron jobs ปัจจุบัน
# Format: ┌───────── นาที (0-59)
# │ ┌─────── ชั่วโมง (0-23)
# │ │ ┌───── วันของเดือน (1-31)
# │ │ │ ┌─── เดือน (1-12)
# │ │ │ │ ┌─ วันของสัปดาห์ (0-7, 0/7=อาทิตย์)
# │ │ │ │ │
# * * * * * command
# ตัวอย่าง Cron Jobs:
# ทุก 5 นาที
*/5 * * * * /opt/scripts/check_health.sh
# ทุกวัน ตี 2
0 2 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1
# ทุกวันจันทร์ 9:00 น.
0 9 * * 1 /opt/scripts/weekly_report.sh
# ทุกต้นเดือน
0 0 1 * * /opt/scripts/monthly_cleanup.sh
# ทุกชั่วโมง
0 * * * * /opt/scripts/sync_data.sh
Shell Scripting — เขียน Script อัตโนมัติ
#!/bin/bash
# server_check.sh — Script ตรวจสอบ Server เบื้องต้น
echo "=============================="
echo "Server Health Check"
echo "Date: $(date)"
echo "Hostname: $(hostname)"
echo "=============================="
# 1. ตรวจ CPU Load
echo ""
echo "=== CPU Load ==="
uptime
LOAD=$(uptime | awk -F'average:' '{print $2}' | awk -F, '{print $1}' | tr -d ' ')
echo "Current Load: $LOAD"
# 2. ตรวจ RAM
echo ""
echo "=== Memory Usage ==="
free -h
MEM_USED=$(free | awk '/Mem:/ {printf "%.1f", $3/$2 * 100}')
echo "Memory Used: ${MEM_USED}%"
# 3. ตรวจ Disk
echo ""
echo "=== Disk Usage ==="
df -h | grep -E "^/dev"
DISK_USED=$(df / | awk 'NR==2 {print $5}')
echo "Root Disk Used: $DISK_USED"
# 4. ตรวจ Services สำคัญ
echo ""
echo "=== Services Status ==="
for svc in nginx mysql ssh; do
STATUS=$(systemctl is-active $svc 2>/dev/null || echo "not found")
echo " $svc: $STATUS"
done
# 5. ตรวจ Port
echo ""
echo "=== Open Ports ==="
ss -tulpn | grep LISTEN | awk '{print $5}' | sort
echo ""
echo "=============================="
echo "Check completed!"
รัน Script
# ทำให้ Script รันได้
chmod +x server_check.sh
# รัน
./server_check.sh
# หรือ
bash server_check.sh
# ตั้ง Cron ให้รันทุกชั่วโมง
# crontab -e
# 0 * * * * /opt/scripts/server_check.sh >> /var/log/server_check.log 2>&1
Pipe และ Redirect — ส่งต่อข้อมูล
# Pipe | — ส่ง Output ของคำสั่งหนึ่งเป็น Input ของอีกคำสั่ง
cat /var/log/syslog | grep error | wc -l # นับจำนวน Error
ps aux | sort -k3 -rn | head -5 # Top 5 CPU usage
du -sh /var/log/* | sort -rh | head -10 # Top 10 โฟลเดอร์ใหญ่
# Redirect — ส่ง Output ไปไฟล์
echo "Hello" > file.txt # เขียนทับ (Overwrite)
echo "World" >> file.txt # เขียนต่อท้าย (Append)
ls /nonexist 2> error.log # ส่ง Error ไปไฟล์
ls /tmp > output.log 2>&1 # ส่งทั้ง Output + Error
command > /dev/null 2>&1 # ส่งทิ้งหมด (ไม่แสดงอะไร)
สรุปคำสั่ง Linux สำคัญที่ IT ต้องใช้ทุกวัน
| หมวด | คำสั่ง | หน้าที่ |
|---|---|---|
| Navigation | pwd, ls, cd | รู้อยู่ไหน ดูมีอะไร ไปไหน |
| File Ops | cp, mv, rm, mkdir, touch | คัดลอก ย้าย ลบ สร้าง |
| View Files | cat, head, tail, less | อ่านไฟล์ |
| Search | grep, find | ค้นหาข้อความ/ไฟล์ |
| Permission | chmod, chown | จัดการสิทธิ์ |
| Users | useradd, passwd, usermod | จัดการผู้ใช้ |
| Packages | apt/dnf install, update | ติดตั้ง/อัปเดตโปรแกรม |
| Services | systemctl start/stop/status | จัดการ Services |
| Network | ip, ss, ping, curl | ตรวจสอบเครือข่าย |
| Process | ps, top, kill | จัดการ Process |
| Remote | ssh, scp | เชื่อมต่อระยะไกล |
| Automation | cron, bash script | ตั้งเวลา/เขียน Script |
| Logs | journalctl, tail -f | ดู Log |
ลำดับการเรียนรู้ Linux สำหรับ IT มือใหม่
Week 1-2: ติดตั้ง Linux (WSL2 หรือ VM) → เรียนรู้ Navigation (pwd, ls, cd) → File Operations (cp, mv, rm, mkdir) → อ่านไฟล์ (cat, head, tail, less)
Week 3-4: Permission (chmod, chown) → User Management → Package Management (apt install) → Text Editor (nano) → grep และ find
Week 5-6: systemd (systemctl, journalctl) → Networking (ip, ss, curl, ping) → SSH (ssh, scp, key auth) → Process Management (ps, top, kill)
Week 7-8: Cron Jobs → Shell Scripting → Pipe & Redirect → Log Analysis → ฝึกแก้ปัญหาจริงบน Server
เริ่มต้นจากคำสั่งง่าย ๆ ฝึกใช้ทุกวัน ผิดก็ไม่เป็นไร (ถ้าใช้ VM/WSL ลบแล้วสร้างใหม่ได้) สิ่งสำคัญคือ อย่ากลัว Command Line เพราะเมื่อชำนาญแล้ว คุณจะทำงานเร็วกว่า GUI หลายเท่า!