Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> สมาร์ทโฟน >> Linux

วิธีหา Public IP Address ของ Linux Server จาก Command Line

คู่มือนี้แนะนำวิธีดู 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

เปรียบเทียบบริการ

การใช้งานจริง

  • Dynamic DNS Update Script
  • Firewall Whitelist อัตโนมัติ
  • Monitoring/Alerting เมื่อ IP เปลี่ยน
  • VPN Connection Verification

แนะนำ: ใช้ curl -s https://ifconfig.me เร็ว สะอาด รองรับ HTTPS และ IPv6

บริการรูปแบบHTTPSRate Limit
ifconfig.mePlain text✓ต่ำ
ipinfo.io/ipPlain text✓ปานกลาง (ฟรี 50k/day)
api.ipify.orgPlain text✓สูง
icanhazip.comPlain text✓ต่ำ
ipinfo.io/jsonJSON (IP, City, Country, Org)✓ปานกลาง