• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Find My Public IP Address from Linux Command Line

Written by Rahul, Updated on July 23, 2020

Public IP is used for communication between computers over the Internet. A computer running with public IP is accessible all over the world using the Internet. So we can say that it is the identity of the computer on the internet. Now the question is how do we know our public IP?. For computers having GUI can easily get there IP using web tools like this but how to get public IP of the computers having terminal access only. The solution is here – use one of the following commands to find public IP of your system using Linux terminal. These are also useful to use in a shell script.

  • How to Check Local IP Address on Linux

Find Public IP using Linux Command

Command 1 –

Use dig command to find your public IP address. The dig command is a DNS lookup utility for Linux systems to look up your public IP address by connecting to the OpenDNS servers.

dig +short myip.opendns.com @resolver1.opendns.com

Command 2 –

Use wget command to get your Public IP address as below example.

wget http://ipecho.net/plain -O - -q ; echo

Command 3,4,5 –

Use curl command to get your Public address.

curl ipecho.net/plain; echo
curl icanhazip.com
curl ifconfig.me

Get Public IP in Shell Script

We can simply use following commands in our shell script get our computers 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

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

7 Comments

  1. Avatar ipconfig.in Reply
    January 12, 2019 at 10:55 am

    also add, curl ipconfig.in/ip

    or visit http://www.ipconfig.in

  2. Avatar Raquib Reply
    December 13, 2018 at 6:16 pm

    Amazing I have added them in my note, I have got the same ip after checking them at http://whatsmyrouterip.com/ so cool

  3. Avatar Sanix Reply
    October 25, 2018 at 1:14 pm

    Nice, but, you can also in simple way hit this:
    $ curl ipinfo.io

    As simple as it!

  4. Avatar Steven Nunez Reply
    September 17, 2016 at 10:50 pm

    No need for faraday. Use the standard library
    require ‘open-uri’
    open(‘http://icanhazip.com/’).read.chomp

  5. Avatar Mathieu Reply
    May 2, 2016 at 3:03 pm

    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

  6. Avatar Mathieu Reply
    May 2, 2016 at 1:48 pm

    Thanks, great your are providing 5 nice urls like this..

    here a use in Ruby

    require ‘faraday’
    Faraday.get(‘http://icanhazip.com/’).body.chomp

  7. Avatar vicdeveloper Reply
    June 15, 2015 at 3:30 am

    Thanks dude. Added to favorites.

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Ubuntu 20.04 5
  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy