Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»Finding Your Public IP Address from the Linux Command Line

    Finding Your Public IP Address from the Linux Command Line

    By RahulFebruary 16, 20232 Mins Read

    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.

    Advertisement
    • How to Check Local IP Address on Linux

    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.

    ip public ip
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Top 10 JQ Commands Every Linux Developer Should Know

    How to Check if a Program Exists in Linux

    How to Create a Directory If It Does Not Exist in Linux

    View 8 Comments

    8 Comments

    1. Bob Dodds on November 26, 2020 6:27 am

      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;’
      }

      Reply
    2. ipconfig.in on January 12, 2019 10:55 am

      also add, curl ipconfig.in/ip

      or visit http://www.ipconfig.in

      Reply
    3. Raquib on December 13, 2018 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

      Reply
    4. Sanix on October 25, 2018 1:14 pm

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

      As simple as it!

      Reply
    5. Steven Nunez on September 17, 2016 10:50 pm

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

      Reply
    6. Mathieu on May 2, 2016 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

      Reply
    7. Mathieu on May 2, 2016 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

      Reply
    8. vicdeveloper on June 15, 2015 3:30 am

      Thanks dude. Added to favorites.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.