SSH มีความสามารถขั้นสูงที่นอกเหนือจากการ Login เข้า Server โดยตรง ได้แก่ ProxyJump สำหรับการกระโดดผ่าน Bastion Host และ Port Forwarding สำหรับส่งต่อ Traffic ผ่าน SSH Tunnel ความสามารถเหล่านี้ช่วยให้เข้าถึง Server ที่อยู่ใน Private Network หรือใช้งาน Service ที่ไม่มี Public Port ได้อย่างปลอดภัย
บทความนี้อธิบาย ProxyJump, Local Port Forwarding, Remote Port Forwarding และ Dynamic Port Forwarding พร้อมตัวอย่างการใช้งานจริงใน Production Environment
ProxyJump — กระโดดผ่าน Bastion Host
ในสภาพแวดล้อม Production ที่ดี Server ที่สำคัญจะอยู่ใน Private Network และเข้าถึงได้เฉพาะผ่าน Bastion Host (Jump Host) เท่านั้น ProxyJump ช่วยให้ SSH กระโดดข้ามหลาย Server ได้ในคำสั่งเดียวโดยไม่ต้องเปิด Session แยก
# ProxyJump ผ่าน 1 Hop
ssh -J user@bastion user@private-server
# ProxyJump ผ่านหลาย Hop (เรียงตามลำดับ)
ssh -J user@bastion1,user@bastion2 user@final-server
# ระบุ Port พร้อมกัน
ssh -J user@bastion:2222 user@private-server
ตั้งค่า ProxyJump ใน ~/.ssh/config
การเพิ่ม ProxyJump ใน Config File ทำให้ไม่ต้องพิมพ์ Flag ซ้ำทุกครั้ง
# ~/.ssh/config
# Bastion Host
Host bastion
HostName 203.0.113.1
User admin
IdentityFile ~/.ssh/id_ed25519
# Private Server ที่เข้าถึงผ่าน Bastion
Host prod-db
HostName 10.0.1.50
User deploy
IdentityFile ~/.ssh/id_ed25519_deploy
ProxyJump bastion
Host prod-app
HostName 10.0.1.51
User deploy
ProxyJump bastion
# ใช้งาน
# ssh prod-db (กระโดดผ่าน bastion โดยอัตโนมัติ)
Local Port Forwarding
Local Port Forwarding เปิด Port บนเครื่อง Local แล้วส่ง Traffic ไปยัง Host:Port ผ่าน SSH Tunnel เหมาะสำหรับเข้าถึง Database หรือ Web Interface ที่ Server ไม่ได้เปิด Port สู่ Internet โดยตรง
# รูปแบบ: ssh -L [local_port]:[remote_host]:[remote_port] user@ssh_server
# เข้าถึง MySQL บน private server ผ่าน localhost:3307
ssh -L 3307:localhost:3306 user@db-server
# เข้าถึง Web Admin บน private network
ssh -L 8080:10.0.1.100:80 user@bastion
# รัน Background (-f) และ No Command (-N)
ssh -fN -L 3307:localhost:3306 user@db-server
# หลังจากนั้น connect ได้ที่ localhost:3307
# mysql -h 127.0.0.1 -P 3307 -u root -p
Flag -N บอกว่าไม่ต้องรัน Command บน Remote (เปิด Tunnel อย่างเดียว) และ -f ส่ง SSH ไปทำงาน Background ทำให้ใช้ Terminal ได้ต่อ
Remote Port Forwarding
Remote Port Forwarding เปิด Port บน Remote Server แล้วส่ง Traffic กลับมาที่ Local Machine หรือ Network เหมาะสำหรับ Expose Service ที่อยู่หลัง Firewall หรือ NAT ให้คนอื่นเข้าถึงได้ชั่วคราว เช่น ในระหว่าง Development หรือ Demo
# รูปแบบ: ssh -R [remote_port]:[local_host]:[local_port] user@remote_server
# เปิด Port 8080 บน remote server ให้ forward มาที่ local port 3000
ssh -R 8080:localhost:3000 user@public-server
# ให้ทุก IP บน remote server เข้าถึงได้ (ต้องตั้ง GatewayPorts yes ใน sshd_config)
ssh -R 0.0.0.0:8080:localhost:3000 user@public-server
# รัน Background
ssh -fN -R 8080:localhost:3000 user@public-server
โดยค่าเริ่มต้น Remote Port Forwarding จะ Bind เฉพาะ 127.0.0.1 บน Remote Server ทำให้เฉพาะ Process บน Server นั้นเข้าถึงได้ ถ้าต้องการให้ภายนอก Server เข้าถึงได้ต้องตั้งค่า GatewayPorts yes ใน sshd_config ด้วย
Dynamic Port Forwarding — SOCKS Proxy
Dynamic Port Forwarding สร้าง SOCKS5 Proxy บนเครื่อง Local ที่ส่ง Traffic ทั้งหมดผ่าน SSH Tunnel ต่างจาก Local Forwarding ที่ระบุ Destination ตายตัว SOCKS Proxy สามารถ Route ไปหลาย Destination ได้แบบ Dynamic
# สร้าง SOCKS5 Proxy บน localhost:1080
ssh -D 1080 user@ssh-server
# รัน Background
ssh -fN -D 1080 user@ssh-server
# ใช้ curl ผ่าน SOCKS proxy
curl --socks5 localhost:1080 http://internal-server/
# ใช้กับ Browser: ตั้ง SOCKS5 Proxy = 127.0.0.1:1080
# ทุก HTTP/HTTPS request จะไหลผ่าน ssh-server
SSH Tunnel สำหรับ Database Admin
กรณีใช้งานที่พบบ่อยที่สุดคือการเข้าถึง Database Admin Tool เช่น phpMyAdmin, pgAdmin หรือ Adminer ที่อยู่บน Private Server ผ่าน Local Port Forwarding โดยไม่ต้องเปิด Port สู่ Internet
# เข้าถึง phpMyAdmin บน private server ผ่าน localhost:8888
ssh -fN -L 8888:localhost:80 user@db-server
# เปิด browser แล้วไปที่ http://localhost:8888/phpmyadmin
# เข้าถึง PostgreSQL
ssh -fN -L 5433:localhost:5432 user@pg-server
# psql -h localhost -p 5433 -U postgres
# เข้าถึง Redis
ssh -fN -L 6380:localhost:6379 user@cache-server
# redis-cli -p 6380
ตรวจสอบและปิด SSH Tunnel
# ดู SSH Process ที่รันอยู่ใน Background
ps aux | grep ssh
# ดู Port ที่ Tunnel กำลัง Listen อยู่
ss -tlnp | grep ssh
# ปิด Tunnel โดยหา PID แล้ว kill
kill $(pgrep -f "ssh -fN")
# หรือระบุ Port เฉพาะ
fuser -k 8080/tcp
ตั้งค่า Persistent Tunnel ด้วย autossh
SSH Tunnel ที่รันด้วย -fN จะหยุดทำงานหากการเชื่อมต่อขาด autossh เป็นเครื่องมือที่ Monitor และ Restart Tunnel อัตโนมัติ เหมาะสำหรับ Tunnel ที่ต้องการความต่อเนื่อง
# ติดตั้ง autossh
sudo apt install autossh -y # Ubuntu/Debian
sudo dnf install autossh -y # RHEL/CentOS
# รัน autossh Tunnel (จะ Reconnect อัตโนมัติหาก Connection ขาด)
autossh -M 0 -fN -L 3307:localhost:3306 user@db-server
# -M 0: ปิด autossh monitoring port (ใช้ SSH keepalive แทน)
# ร่วมกับ ServerAliveInterval ใน ~/.ssh/config
Host db-server
HostName 203.0.113.50
User deploy
ServerAliveInterval 30
ServerAliveCountMax 3
สรุป
ProxyJump ช่วยเข้าถึง Private Server ผ่าน Bastion Host ในคำสั่งเดียว Local Port Forwarding ใช้เปิด Tunnel สู่ Service บน Private Network และ Remote Port Forwarding ใช้ Expose Local Service ผ่าน Public Server ส่วน Dynamic Forwarding สร้าง SOCKS Proxy สำหรับ Route Traffic หลาย Destination การตั้งค่าใน ~/.ssh/config ช่วยให้ใช้งานได้สะดวกโดยไม่ต้องพิมพ์ Flag ซ้ำ และ autossh ช่วยให้ Tunnel คงทนเมื่อ Connection ขาด
แนะนำบริการ DE
การฝึก SSH Tunneling และ ProxyJump ต้องการ Server ที่ควบคุมได้เต็มที่ทั้งในแง่ Network และ sshd_config Cloud VPS ของ DE ให้สิทธิ์ root สมบูรณ์พร้อม Network ที่กำหนดค่าได้ เหมาะสำหรับทดลอง Port Forwarding, ProxyJump และ SSH Tunnel ในสภาพแวดล้อมจริง
หากต้องการโฮสต์เว็บไซต์หรือแอปพลิเคชันโดยไม่ต้องจัดการ Network Configuration เอง Cloud Hosting ของ DE มีระบบที่ดูแลด้านความปลอดภัยให้อัตโนมัติ

