How do I know your public IP address on a Linux terminal?
A public address is assigned by InterNIC and consists of a class-based network ID or CIDR-based address block (referred to as a CIDR Block). It also guarantees its uniqueness on the global Internet. When a public address is assigned, its route will be recorded in a router on the Internet, so that the traffic to access the public address can reach smoothly. Traffic accessing the target public address can be reached through the Internet. For example, when a CIDR block is assigned to an organization in the form of a network ID and subnet mask, the corresponding [network ID, subnet Mask] is also stored as a route in a router on the Internet. The IP address of the target CIDR block is directed to the corresponding location.
In this article, I will introduce several methods to view your public IP addresses on Linux terminals. This is meaningless for common users, but it is useful for Linux servers (no GUI or users who can only use basic tools to log on. In any case, obtaining a public IP address from a Linux terminal is of great significance in various aspects and may be available one day.
The following two commands are used: curl and wget. You can use it again.
Curl output in plain text format:
curl icanhazip.com
curl ifconfig.me
curl curlmyip.com
curl ip.appspot.com
curl ipinfo.io/ip
curl ipecho.net/plain
curl www.trackip.net/i
Curl JSON output:
curl ipinfo.io/json
curl ifconfig.me/all.json
Curl www.trackip.net/ip? Json (a bit ugly)
Curl XML format output:
curl ifconfig.me/all.xml
Curl to get all IP details (excavator)
curl ifconfig.me/all
Use DYDNS (useful when you use DYDNS)
curl -s 'http://checkip.dyndns.org'| sed 's/.*Current IP Address: \([0-9\.]*\).*/\1/g'
curl -s http://checkip.dyndns.org/ | grep -o "[[:digit:].]\+"
Use Wget to replace Curl
wget http://ipecho.net/plain -O - -q ; echo
wget http://observebox.com/ip -O - -q ; echo
Use the host and dig commands
If yes, you can also directly use the host and dig commands.
host -t a dartsclink.com | sed 's/.*has address //'
dig +short myip.opendns.com @resolver1.opendns.com
Bash script example:
#!/bin/bash
PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo`
echo $PUBLIC_IP
Easy to use.
I am actually writing a script to record all IP changes in my vro every day and save them to a file. I found these useful commands during the search process. I hope it can help others one day.
Via: How to get Public IP from Linux Terminal?
Translator: KevinSJ Proofreader: wxy
This article was originally translated by LCTT and launched with the Linux honor in China
This article permanently updates the link address: