If you’re using a Linux machine and want to find your public IP address without visiting a website, you can do so easily from the command line. In this tutorial, we’ll go through the steps to finding your public IP address using a few different methods.
Finding Your Public IP Address
Method 1: Using the dig Command
The dig command is used for querying DNS servers, but it can also be used to find your public IP address. Open a terminal and type the following command:
dig +short myip.opendns.com @resolver1.opendns.com
You should see your public IP address printed on the screen.
Method 2: Using the curl Command
The curl command is used for transferring data from or to a server. It can also be used to find your public IP address. Open a terminal and type the following command:
curl ipecho.net/plain; echo
curl icanhazip.com
curl ifconfig.me
You should see your public IP address printed on the screen.
Method 3: Using the wget Command
The wget command is used for downloading files from the web. It can also be used to find your public IP address. Open a terminal and type the following command:
wget -qO- ifconfig.me
You should see your public IP address printed on the screen.
Getting Public IP in Shell Script
We can simply use the following commands in our shell script to get our computer’s public IP and store them in a variable to use anywhere in a shell script.
1 2 3 4 | #!/bin/bash PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo` echo $PUBLIC_IP |
Conclusion
These are just a few of the many methods available for finding your public IP address from the Linux command line. Depending on your system’s configuration and your network setup, some of these methods may work better than others. Experiment with different methods to find the one that works best for you.
8 Comments
Thank you Rahul, and everyone who added sites. I put seven sites in a bash function. The last one gives more info. The first few times I ran this, opendns reported a different IP. I have helped someone whose public IP was diverted, and her logins were hacked in realtime so it was bad to log in anywhere until the method was discoverred. https://pastebin.com/DiPUdqPJ
myip() {
( for site in ipaddr.pub/cli ipecho.net/plain icanhazip.com ifconfig.me \
ipconfig.in/ip diagnostic.opendns.com/myip
do echo “$site ”
wget -qO- $site
echo
done ) | sed -n -E ‘/^$/d;H;${g;s/^[\n]+//;s/( )\n/ /g;p;}’
wget -qO- ipinfo.io | sed ‘1s/.*/ipinfo.io:/;$d;’
}
also add, curl ipconfig.in/ip
or visit http://www.ipconfig.in
Amazing I have added them in my note, I have got the same ip after checking them at http://whatsmyrouterip.com/ so cool
Nice, but, you can also in simple way hit this:
$ curl ipinfo.io
As simple as it!
No need for faraday. Use the standard library
require ‘open-uri’
open(‘http://icanhazip.com/’).read.chomp
made a small ruby script which pick a random service url from the 4 you provided in your blog post
#!/usr/bin/env ruby
require ‘faraday’
# http://tecadmin.net/5-commands-to-get-public-ip-using-linux-terminal/
urls = %w[
http://ipecho.net/plain
http://observebox.com/ip
http://icanhazip.com
http://ifconfig.me
]
url = urls[(rand * urls.size).to_i]
STDERR.puts “I will be using #{url} to find out your external IP”
puts Faraday.get(url).body
Thanks, great your are providing 5 nice urls like this..
here a use in Ruby
require ‘faraday’
Faraday.get(‘http://icanhazip.com/’).body.chomp
Thanks dude. Added to favorites.