- Methods to detect public IP address in bash
- 14 Answers 14
- 4 Ways to Find Server Public IP Address in Linux Terminal
- 1. Using dig Utility
- 2. Using host Utility
- 3. Using wget Command Line Downloader
- 4. Using cURL Command Line Downloader
- How to Get Public IP from Terminal on Ubuntu 22.04
- Get Public IP from Terminal on Ubuntu 22.04
- 1: Use Dig command to get public IP address
- 2: Use host command to get public IP address
- 3: Use wget command to get IP address
- 4: Use curl command to get IP address
- Conclusion
- About the author
- Awais Khan
Methods to detect public IP address in bash
As part of an installation script, I want to make an «educated guess» about the machines public IP address. It should be as robust as possible, working on a variety of hosts and platforms. Here is an example: https://gist.github.com/4669037 This script uses 3 methods and then defaults to the value of /etc/hostname . I have only tested this on Ubuntu so far. Will this be likely to work on other distributions? And are there additional methods that I could add?
The answer below curl ifconfig.me is great. But I want fallback methods in case curl is not available or the ifconfig.me site is unavailable.
@ruakh the example script already has 2> /dev/null in the curl line. Wouldn’t that hide the curl error?
See this answer which has a script trying various DNS servers and HTTP services until it gets a reply. It uses dig or curl or wget (in that order) depending on which one it finds in the path. The list of servers is good as of Feb. 2017.
14 Answers 14
You can find other ip reporting websites instead of ipinfo.io as well. To name a few:
Also, what is my ip shows more information about that ip.
Thanks these are great. Now I just need a local fallback method in case these hosts are unavailable during installation.
curl ifconfig.me would be the best choice, in case you don’t have curl:
If on an AWS EC2 you can use:
NOTE: this service can be used from any web client.
How much public public IP are you looking for? What if the machine is behind NAT?
- curl / wget / netcat ( nc ) which contains requester’s address: should work most of the time, but may the site may be unreachable from the machine (be it firewall or temporary/permanent unavailability). You’ll get the most public IP you can.
- ifconfig : must run as root, otherwise you’d have to try /sbin/ifconfig or /usr/sbin/ifconfig as well. What if the machine has more NICs? How do you tell what IP is the right one? What if only IPv6 is used on the machine’s LAN? In any case, you’ll get the least public IP you can (and possibly a wrong one if more interfaces are configured on the machine — which often is the case these days with omnipresent virtualization using network tap devices and/or).
- /etc/hostname : does not need to exist, on many systems it is /etc/HOSTNAME , and it does not contain IP address rather it should contain the hostname (usually the FQDN).
The point is, that the ways in which it can fail are numerous and you probably should consider either a) specifying more precisely what systems you are targeting or b) whether you really need to know the IP at all — is a solution that seems to work in simple cases worth using when it fails miserably in slightly more complicated setup? If you think you need the IP, prepare a way to handle failures gracefully in cases where you either don’t get the IP at all or you get a wrong one.
4 Ways to Find Server Public IP Address in Linux Terminal
In computer networking, an IP (Internet Protocol) address is a numerical identifier assigned permanently or temporarily to every device connected to a network that uses the Internet Protocol for communication. Its two major functions are to identify a network or host on a network and also serve for location addressing.
There are currently two versions of IP addresses: IPv4 and IPv6, which can either be private (viewable within an internal network) or public (can be seen by other machines on the Internet).
Additionally, a host can be assigned a static or dynamic IP address depending on the network configurations. In this article, we will show you 4 ways to find your Linux machine or server public IP address from the terminal in Linux.
1. Using dig Utility
dig (domain information groper) is a simple command line utility for probing DNS name servers. To find your public IP addresses, use the opendns.com resolver as in the command below:
$ dig +short myip.opendns.com @resolver1.opendns.com 120.88.41.175
2. Using host Utility
host command is an easy-to-use command line utility for carrying out DNS lookups. The command below will help to display your systems public IP address.
$ host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '' 120.88.41.175
Important: The next two methods employ third party websites to display your IP address on the command line as described below.
3. Using wget Command Line Downloader
wget is a powerful command line downloader that supports various protocols like HTTP, HTTPS, FTP and many more. You can use it with third party websites to view your public IP address as follows:
$ wget -qO- http://ipecho.net/plain | xargs echo $ wget -qO - icanhazip.com 120.88.41.175
4. Using cURL Command Line Downloader
curl is a popular command line tool for uploading or downloading files from a server using any of the supported protocols (HTTP, HTTPS, FILE, FTP, FTPS and others). The following commands displays your public IP address.
$ curl ifconfig.co $ curl ifconfig.me $ curl icanhazip.com 120.88.41.175
That’s It! You may find these following articles useful to read.
That’s all! If you have any questions or other methods to share in relation to this topic, use the feedback form below to write back to us.
How to Get Public IP from Terminal on Ubuntu 22.04
An Internet Protocol commonly referred as IP address is a numerical representation of a device identification number connected with the Internet. It’s a unique ID that separates one device from another connected to the network thus is different for each device. There are usually two types of IP address; private and public. The private IP address is the address that router assigns to each device while public IP address is assigned by Internet Service Provider (ISP).
In this guide, we will check out how to get public IP from a terminal on Ubuntu 22.04.
Get Public IP from Terminal on Ubuntu 22.04
There are lots of ways to get the IP address of a device which is connected to the Internet. Here we are finding the public IP from the terminal on Ubuntu.
So, let’s discuss several ways through which you can get public IP from the terminal on Ubuntu 22.04.
1: Use Dig command to get public IP address
Dig is an excellent utility that allows Linux users to get public IP from a terminal. The Dig along with the OpenDNS will help you in getting your public IP address on the terminal. The below-given command will be used for this purpose:
2: Use host command to get public IP address
You can also find the IP address using the host command that gives you a public IP address through DNS lookups. Run the following host command to get a public IP address:
3: Use wget command to get IP address
You can also get a public IP address using the wget piped with xarg command which is shown below:
Or use the following command incanzip service to get a public IP address:
There is another service as well that utilizes the wget command to provide you the public IP address:
You can also utilize wget command with ifconfig.me service to get the public IP address:
4: Use curl command to get IP address
Another way to get a public IP address from the terminal on Ubuntu is using the curl command. However, first you will need to install curl using the following command:
Once curl is installed, you can then use the following command with ifconfig.co to get a public IP address:
You can also use the curl command with ifconfig.me and it will give the public IP address on the terminal:
Or you can use the following curl command with icanhazip service to get public IP on your Ubuntu 2.04 terminal:
Conclusion
The public IP address is an address that you received from the internet service provider. It is always known to the IPS. There are numerous methods to find the public IP address, all are discussed in this guide. Try all those commands as they need a third-party service to provide your public IP address.
About the author
Awais Khan
I’m an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.