How to check the IP address in Linux

When troubleshooting network problems, set up a new connection or firewall configuration, understand the device's IP address is very important.

IP addresses can be divided into two categories, public and internal. IP is the only public IP addresses can be accessed from the Internet. Internal IP address reserved for your internal private network use, and not directly exposed to the Internet. In addition, there are two types of IP addresses, IP version 4 (IPv4) and IP version 6 (IPv6).

This article will introduce several different ways to view Linux system of public and internal IP addresses.

Find your internal IP address

Internal IP addresses can not be routed over the Internet, and can only work on the local network. Typically, the router translates a private IP address assigned to each device in the local network. This provides a unique IP address for the local network devices, such as cell phones, laptops, smart TVs, printers, media centers. Devices on the local network to the Internet via NAT (Network Address Translation).

The following IPv4 address ranges are reserved for private networks:

  • 10.0.0.0/8
  • 172.16.0.0/12
  • 192.168.0.0/16

You can determine the internal IP address of the system by using the command to query the network stack, such as: ip, ifconfig or hostname.

In Linux, for displaying and configuring network interface is the standard command ip.

To display a list of all network interfaces and associated IP address, type the following command:

ip addr

Output results are shown below. Internal IP address highlighted.

How to check the IP address in Linux

You can also use the following command to display the internal IP address:

hostname -I
ifconfig

How to check the IP address in Linux

Find your public IP address

Public IP address is assigned to the global network equipment routable IP address, which directly access the Internet. Devices, each device has a unique public IP address assigned by your ISP to them. Public IP address used by the home router, Web servers, mail servers, and so on.

Determine the public IP address of the remote server involves contact through HTTP / HTTPS or DNS protocol, and obtain an IP address from a remote server response.

On the desktop computer, the easiest way to find the public IP address by typing in the browser "My IP is."

If you are on a headless Linux server, or if you want to determine the IP address, you can use the command-line tools, such as shell script variables dig, curl and wget.

Most DNS providers (such as OpenDNS and Google) that allow you to query the server and get your public IP address.

You can use any of the following commands to get your IP:

  • dig ANY +short @ resolver2.opendns.com myip.opendns.com 
  • dig ANY +short @ resolver2.opendns.com myip.opendns.com 
  • dig ANY +short @ ns1-1.akamaitech.net ANY whoami.akamai.net 

There are many online HTTP / HTTPS service can use your public IP address to respond. Here are some of them:

  • curl -s http://tnx.nl/ip
  • curl -s https://checkip.amazonaws.com
  • curl -s api.infoip.io/ip
  • curl -s ip.appspot.com
  • wget -O - -q https://icanhazip.com/

If any of the above command does not work, there may be a problem with the online service.

You can even create an alias in your ~ / .bashrc or ~ / .zshrc file, after your having to type and remember a very long command. For example, you can add the following alias:

alias pubip='dig ANY +short @resolver2.opendns.com myip.opendns.com'

Now, if you need to view your public IP, simply enter pubip your terminal can be.

in conclusion

We show you several different commands and online services, you can use them to find your private and public IP addresses.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159986.htm