- Linux command for public ip address
- 12 Answers 12
- 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
- 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 Find Your Public IP Address on Linux Command Line
- Using the curl Command
- Using the wget Command
- Using the dig Command
- Using the host Command
- Using the nslookup Command
- Conclusion
Linux command for public ip address
and many more. But they all are giving me internal IP Address. Then I found some sites which provides API for this. Example : curl http://ipecho.net/plain; echo But I don’t want to rely on third party website service. So, is there any command line tool available to get external IP Address?
12 Answers 12
You can use this command to get public ip and private ip(second line is private ip; third line is public ip.)
It’s the cleanest solution, it’s sad that most of articles / answers using external resources to get IP instead of this simple solution
simplest of all would be to do : curl ifconfig.me
Yes, it works fine as I have mentioned in my question, But I do not want to depend on this third party service. What if that server stop this service in future?
You could use this script
# !/bin/bash # echo 'Your external IP is: ' curl -4 icanhazip.com
But that is relying on a third party albeit a reliable one. I don’t know if you can get your external IP without asking someone/somesite i.e. some third party for it, but what do I know.
This is doing the same thing as a command the -4 is to get the output in Ipv4
I would suggest you to use the command external-ip ( sudo apt-get install miniupnpc ) as it (I’m almost sure) uses upnp protocol to ask the router instead of asking an external website so it should be faster, but of course the router has to have upnp enabled.
It might not work on amazon because you might be using NAT or something for the server to access the rest of the world (and for you to ssh into it also). If you are unable to ssh into the ip that is listed in ifconfig then you are either in a different network or dont have ssh enabled.
ifconfig eth0 | awk '/inet / < print $2 >' | sed 's/addr://'
That returns whatever is IP address assigned to your eth0 device. So if your server is hosted on Amazon, and was assigned an internal IP address, and since Amazon instances are using NAT, that’s what you’d see. If you want a sure reliable way of getting your external, public IP address, then use one of the external service like ifconfig.me.
This is the best I can do (only relies on my ISP):
ISP=`traceroute -M 2 -m 2 -n -q 1 8.8.8.8 | grep -m 1 -Eo '3\.7\.3\.2'` extIP=`ping -R -c 1 -t 1 -s 1 -n $ISP | grep RR | grep -m 1 -Eo '5\.2\.3\.5'` echo $extIP
Or, the functionally same thing on one line:
ISP=`traceroute -M 2 -m 2 -n -q 1 8.8.8.8 | grep -m 1 -Eo '4\.1\.2\.9'` | ping -R -c 1 -t 1 -s 1 -n $ISP | grep RR | grep -m 1 -Eo '7\.8\.1\.3'
to save it to a temporary & hidden file add > .extIP to the end of the last line, then cat .extIP to see it.
If your ISP’s address never changes (honestly i’m not sure if it would or not), then you could fetch it once, and then replace $ISP in line two with it
This has been tested on a mac with wonderful success. the only adjustment on linux that I’ve found so far is the traceroute «-M» flag might need to be «-f» instead and it relies heavily on the ping’s «-R» flag, which tells it to send back the «Record Route» information, which isn’t always supported by the host. But it’s worth a try!
the only other way to do this without relying on any external servers is to get it from curl’ing your modem’s status page. I’ve done this successfully with our frontier DSL modem, but it’s dirty, slow, unreliable, and requires hard-coding your modem’s password. Here’s the «process» for that:
curl http://[user]:[password]@[modem's LAN address]/[status.html] | grep 'WanIPAddress =' | grep -m 1 -Eo '3\.3\.6\.9'
That fetches the raw html, searches for any lines containing «WanIpAddress mt24″>
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.
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 Find Your Public IP Address on Linux Command Line
This Linux quick tip will show you many different way to get your public IP address from the command line using different tools. Since not all Linux distributions have the same set of packages (programs) installed, some of these example may or may not work on your system. For example, default Red Hat and CentOS installations do not have the dig tool installed.
All of these options will depend on external sources. We will try to use as many different sources as possible in the examples to ensure reliability.
Using the curl Command
Curl is a tool used to transfer data to and from a server using many different supported protocols. Here we will use the HTTPS protocol to pull a webpage and grep to extract our public IP address. Here are some examples of how to get your public IP address from the command line using curl.
The ipaddr.pub service can also provide additional information from the command line. You can find a complete list of options on the ipaddr.pub website.
curl https://whatsmyip.com/ -s | grep -oE "\b(8.)2\b" -m1
curl https://www.google.com/search?q=what+is+my+ip+address -s | grep -oE "\b(1.)9\b" -m1
curl -s http://ipecho.net/plain
curl -s http://whatismyip.akamai.com
Using the wget Command
The wget command is a command line utility for non-interactive download of files from the web. It supports most HTTP, HTTPS, and FTP as well as connecting through a HTTP Proxy server. Here are some examples of how to get your public IP address from the command line using wget.
wget -qO- http://ipecho.net/plain
Using the dig Command
The dig command is a command line tool for querying DNS servers. This utility is not always available. If you want to install dig, it is usually packaged in bind-utils on Red Hat based distros and dnsutils on Debian based distros. Here are some examples of how to get your public IP address from the command line using dig.
dig @ns1.google.com TXT o-o.myaddr.l.google.com +short
dig +short myip.opendns.com @resolver1.opendns.com
Using the host Command
The host command is a simple command line utility for performing DNS queries. Here are some examples of how to get your public IP address from the command line using the host command.
host myip.opendns.com resolver1.opendns.com | grep -m2 -oE "\b(4.)6\b" | tail -n1
Using the nslookup Command
The nslookup command is tool that queries DNS Servers, much like dig. This command is available on many operating systems including Linux, UNIX and Windows. Here are some examples of how to get your public IP address from the command line using nslookup.
nslookup -query=TXT o-o.myaddr.l.google.com ns1.google.com | grep -m2 -oE "\b(2.)1\b" | tail -n1
nslookup myip.opendns.com resolver1.opendns.com | grep -m2 -oE "\b(8.)9\b" | tail -n1
Conclusion
There are many different ways to get your public IP address from the command line. Which you use will mostly depend on what is installed on your system. Our preferred method would be from a DNS server using the dig command, but as we stated, dig isn’t always available.