ทำไมต้องรู้ Linux command
99% ของ server บนโลกรัน Linux — ไม่ว่าคุณจะใช้ AWS, Hetzner, DigitalOcean, ใน Docker container, หรือใน Kubernetes pod ทุกอันคือ Linux
แม้ตอน dev ใช้ Mac (Unix) หรือ Windows + WSL ก็ใช้ command เกือบเหมือนกัน
บทความนี้ไม่สอนทุกคำสั่ง รวมเฉพาะตัวที่คนทำ DevOps ใช้แทบทุกวันที่เปิด terminal
ดู directory และไฟล์
pwd # ดู current directory
ls # list ไฟล์
ls -lah # list แบบมี permission, owner, ขนาด
cd /var/www # เปลี่ยน directory
cd - # กลับไปที่อยู่ก่อนหน้า
cd ~ # ไป home directory
mkdir myapp # สร้าง folder
mkdir -p a/b/c # สร้างพร้อม parent ทั้งหมด
cp file.txt copy.txt # copy ไฟล์
cp -r src/ dst/ # copy folder
mv old.txt new.txt # rename / move
rm file.txt # ลบ
rm -rf folder/ # ลบ folder + เนื้อหา (ระวัง!)
cat file.txt # แสดงเนื้อหาไฟล์
less file.txt # อ่านไฟล์ใหญ่ทีละหน้า (q เพื่อออก)
head -20 log.txt # 20 บรรทัดแรก
tail -50 log.txt # 50 บรรทัดท้าย
tail -f app.log # ตาม log แบบ real-time
Permission
chmod 644 file.txt # owner rw, others r
chmod 755 script.sh # owner rwx, others rx (ใช้กับ executable)
chmod +x script.sh # เพิ่ม execute permission
chown user:group file # เปลี่ยน owner
chown -R user:group dir/ # recursive
วิธีจำ:
4= read2= write1= execute- รวมกัน เช่น
7= rwx,6= rw,5= rx
ค้นหา
# ค้นไฟล์ตามชื่อ
find . -name "*.log"
find /var/log -name "*.gz" -mtime +7 # gz เก่ากว่า 7 วัน
# ค้นข้อความใน file
grep "error" app.log
grep -r "TODO" src/ # recursive
grep -i "warning" log.txt # case insensitive
grep -v "DEBUG" log.txt # ซ่อนบรรทัดที่ match
grep -n "import" main.py # แสดงเลขบรรทัด
# ตัวที่ใหม่กว่าและเร็วกว่า — ripgrep
rg "error" .
Process
ps aux # ดู process ทั้งหมด
ps aux | grep node # filter เฉพาะ node
top # real-time process monitor
htop # ดีกว่า top มาก ติดตั้ง: sudo apt install htop
# ฆ่า process
kill <pid> # ส่ง SIGTERM (ขอให้ปิด)
kill -9 <pid> # ส่ง SIGKILL (บังคับ)
killall node # ฆ่าทุก process ชื่อ node
pkill -f "myapp" # ฆ่าตาม pattern
# ดู port ที่เปิดอยู่
ss -tlnp # tcp listening
ss -tulpn # tcp + udp + พอร์ต
lsof -i :3000 # ใครใช้ port 3000
Network
ping google.com # ping
curl https://example.com # request HTTP
curl -I https://example.com # ดูแค่ header
curl -v https://example.com # verbose
# resolve DNS
dig example.com
dig +short example.com
nslookup example.com
# ดู IP เครื่อง
ip a # ทุก interface
ip route # routing table
hostname -I # ip ของเครื่อง
Disk
df -h # ดู disk แต่ละ partition
du -sh /var/log/* # ดูขนาดแต่ละ folder
du -sh * | sort -hr # เรียงจากใหญ่ไปเล็ก
# ลบไฟล์ใหญ่ที่ไม่ใช้
sudo journalctl --vacuum-time=7d # ล้าง systemd log เก่ากว่า 7 วัน
sudo apt autoremove # ลบ package ที่ไม่ใช้
docker system prune -a --volumes # ลบ docker ที่ไม่ใช้
Memory และ CPU
free -h # ดู RAM
uptime # load average
nproc # จำนวน CPU
lscpu # info CPU แบบเต็ม
cat /proc/meminfo # info RAM แบบเต็ม
systemd (จัดการ service)
# ดู status
sudo systemctl status nginx
# start / stop / restart / reload
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx # reload config โดยไม่ตัด connection
# enable = auto-start ตอน boot
sudo systemctl enable nginx
sudo systemctl disable nginx
sudo systemctl enable --now nginx # enable + start ทันที
# ดู service ทั้งหมด
systemctl list-units --type=service
systemctl list-units --type=service --state=running
journalctl (ดู log)
journalctl -u nginx # log ของ nginx
journalctl -u nginx -f # ตาม log realtime
journalctl -u nginx --since "1 hour ago"
journalctl -u nginx --since today
journalctl -u nginx -n 100 # 100 บรรทัดล่าสุด
journalctl -p err -u nginx # เฉพาะ error
Cron (งานตามเวลา)
crontab -e # แก้ไข cron ของ user
crontab -l # ดู cron ที่ตั้งไว้
# ตัวอย่าง schedule:
# ทุก 5 นาที
*/5 * * * * /home/me/check.sh
# ทุกชั่วโมง
0 * * * * /home/me/hourly.sh
# ทุกคืน 02:00
0 2 * * * /home/me/backup.sh
# ทุกวันจันทร์ 09:00
0 9 * * 1 /home/me/weekly.sh
format: minute hour day month day-of-week
ทดสอบ pattern ใน crontab.guru ก่อนตั้ง — งงน้อยกว่าเยอะ
SSH
# login
ssh user@host
ssh -p 2222 user@host # ถ้า port ไม่ใช่ 22
ssh -i ~/.ssh/mykey user@host # ระบุ key
# copy ไฟล์
scp file.txt user@host:/tmp/
scp -r folder/ user@host:/var/www/
# rsync (ดีกว่า scp มาก)
rsync -avz --progress src/ user@host:/var/www/myapp/
# port forwarding
ssh -L 5432:localhost:5432 user@host # local 5432 ↔ remote 5432
ตั้ง alias ใน ~/.ssh/config:
Host prod
HostName 203.0.113.10
User deploy
Port 22
IdentityFile ~/.ssh/prod_key
จากนั้นใช้แค่ ssh prod
Pipe และ redirect
# pipe = ส่ง output ของคำสั่งหนึ่ง ไปเป็น input ของอีกคำสั่ง
cat app.log | grep ERROR | wc -l # นับจำนวน error
# redirect output ลงไฟล์
ls > files.txt # overwrite
ls >> files.txt # append
command 2> error.log # เฉพาะ stderr
command > out.log 2>&1 # ทั้ง stdout และ stderr
command &> all.log # shorthand เหมือนข้างบน
# ทำงาน background
long-task.sh & # รัน background
nohup long-task.sh & # รันแม้ logout
ตัวที่ลงเพิ่มทำให้ชีวิตดี
sudo apt install -y \
htop \
ripgrep \
fd-find \
bat \
jq \
tmux \
ncdu \
fzf
- htop — process monitor ที่ดู interactive ได้
- ripgrep (rg) — grep เร็วกว่า 10-100 เท่า
- fd-find (fd) — find ที่ใช้ง่ายกว่า
- bat —
catมี syntax highlight - jq — แปลง JSON ใน command line
- tmux — terminal multiplexer (รัน command background ใน server)
- ncdu — ดูพื้นที่ disk แบบ interactive
- fzf — fuzzy finder ใช้กับ history, file, folder
เคล็ดลับ
ใช้ tab completion เสมอ — พิมพ์บางส่วนแล้วกด Tab Linux เติมต่อให้ ลด typo เยอะ
ใช้ !! ทำซ้ำคำสั่งล่าสุด:
mkdir /var/www/myapp # permission denied
sudo !! # = sudo mkdir /var/www/myapp
ใช้ Ctrl+R ค้นหา command history:
# กด Ctrl+R แล้วพิมพ์
docker
# จะเด้ง docker command ล่าสุดที่เคยพิมพ์
man <command> — อ่าน manual ของคำสั่ง (กด q เพื่อออก, /keyword เพื่อค้น)
สรุป
ไม่ต้องท่องทั้งหมด — ใช้ครั้งแรกอาจ Google ทุกครั้ง พอใช้บ่อยจะติดมือเอง
ตัวที่ควรใช้ให้คล่องที่สุด: ssh, tail -f, grep, ps aux | grep, systemctl, journalctl, df -h, du -sh * — ใช้แทบทุกครั้งที่เปิด server
ถัดไปเรียนรู้ระดับเซต — ดู Deploy Node.js ขึ้น VPS เริ่ม config server จริง