A detailed approach to view the public IP in Linux terminals

Source: Internet
Author: User
Tags bash script example curl json

First, look at the General IP command:


Linux View IP address command--ifconfig

Ifconfig command to view and change the address and parameters of a network interface


lo0:flags=849 MTU 8232

inet netmask ff000000

hme0:flags=863 MTU 1500

inet netmask ffffff00 broadcast

Ether 8:0:20:a7:4d:21

The system displays the name of the network interface, the state of the interface (up or down),

Interface IP address and mask information

Remove the native IP address:

Ifconfig|grep bcast|awk-f: ' {print $} ' |awk-f ' ' {print $} '

Remove the number of native IP addresses:

Ifconfig|grep bcast|awk-f: ' {print $} ' |awk-f ' "' {print $} ' |wc-l

How to know your public IP in Linux terminal

Public addresses are allocated by InterNIC and consist of a class-based network ID or CIDR address block (known as CIDR blocks) and ensure uniqueness in the global Internet. When public addresses are assigned, their routes are recorded in routers on the Internet, so that traffic to the public address can be reached smoothly. Traffic to the destination public address can be reached via the Internet. For example, when a CIDR block is assigned to an organization in the form of a network ID and subnet mask, the corresponding [network ID, subnet mask] is also stored as a route in the router in the Internet. An IP packet that targets a address in a CIDR block is directed to the corresponding location.

In this article I'll show you how to view your public IP addresses in several Linux terminals. This is meaningless for ordinary users, but Linux servers (without GUIs or as users who can only use basic tools) can be useful. In any case, getting a public IP from a Linux terminal is very meaningful in every way, perhaps one day.

Here are the two commands we use mainly, curl and wget. You can do it in a different.

Curl Plain Text Format output:

The code is as follows:

Curl icanhazip.com

Curl Ifconfig.me

Curl curlmyip.com

Curl ip.appspot.com


Curl Ipecho.net/plain

Curl www.trackip.net/i

Curl JSON format output:

The code is as follows:

Curl Ipinfo.io/json

Curl Ifconfig.me/all.json

Curl Www.trackip.net/ip?json

(A little ugly)

Curl XML Format output:

The code is as follows:

Curl Ifconfig.me/all.xml

Curl Get all IP details (excavators)

Curl Ifconfig.me/all

Use Dydns (useful when you use the Dydns service)

The code is as follows:

Curl-s ' http://checkip.dyndns.org ' | Sed ' s/.*current IP address: ([0-9.] *). */1/g '

Curl-s http://checkip.dyndns.org/| Grep-o "[[:d Igit:].] +"

Use Wget instead of Curl

The code is as follows:

wget Http://ipecho.net/plain-O-Q; Echo

wget Http://observebox.com/ip-O-Q; Echo

Using the host and Dig commands

If so, you can also use the host and dig commands directly.

The code is as follows:

Host-t a dartsclink.com | Sed ' S/.*has address//'

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

Bash Script Example:

The code is as follows:


Public_ip= ' wget Http://ipecho.net/plain-O-Q; Echo '

Echo $PUBLIC _ip

Simple and easy to use.

I'm actually writing a script that records all the IP changes in my router every day and saves it to a file. I found these very handy commands during the search. Hopefully one day it will help others.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.