คู่มือนี้แนะนำวิธีดู Public IP Address ของเซิร์ฟเวอร์ Linux จากบรรทัดคำสั่ง (ไม่ต้องใช้ Browser)
วิธีที่ 1: ใช้ wget + sed (Classic)
wget -q -O - checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
ผลลัพธ์: 122.172.9.222
วิธีที่ 2: ใช้ curl (แนะนำ - เร็วกว่า ทันสมัยกว่า)
# บริการที่แนะนำ (รองรับ IPv4/IPv6)
curl -s https://ifconfig.me
curl -s https://ipinfo.io/ip
curl -s https://api.ipify.org
curl -s https://icanhazip.com
curl -s https://ident.me
# ดูรายละเอียดเพิ่มเติม
curl -s https://ipinfo.io/json
วิธีที่ 3: ใช้ dig (DNS Query)
# Query DNS resolver ที่คืนค่า IP ของเรา
dig +short myip.opendns.com @resolver1.opendns.com
dig +short TXT +noall +answer @resolver1.opendns.com myip.opendns.com
สร้าง Script สำหรับใช้งานง่าย
# สร้างไฟล์
cat > /usr/local/bin/myip << 'EOF'
#!/bin/bash
curl -s https://ifconfig.me
echo
EOF
# ให้สิทธิ์รัน
chmod +x /usr/local/bin/myip
# ใช้งาน
myip
สำหรับ macOS
# ติดตั้ง wget (ถ้ายังไม่มี)
brew install wget
# หรือใช้ curl (มีอยู่แล้ว)
curl -s https://ifconfig.me
เปรียบเทียบบริการ
| บริการ | รูปแบบ | HTTPS | Rate Limit |
|---|---|---|---|
| ifconfig.me | Plain text | ✓ | ต่ำ |
| ipinfo.io/ip | Plain text | ✓ | ปานกลาง (ฟรี 50k/day) |
| api.ipify.org | Plain text | ✓ | สูง |
| icanhazip.com | Plain text | ✓ | ต่ำ |
| ipinfo.io/json | JSON (IP, City, Country, Org) | ✓ | ปานกลาง |