3v-Hosting Blog

How to Get Your Linux IP Address in Different Ways

Administration

9 min read


Knowing how to obtain a system's IP address on Linux servers is important for system administrators, developers, and anyone involved in network management. The concept of IP address is fundamental to networking and is critical to network communications, allowing devices to identify and connect to each other. In this article, we will look at several methods to obtain your IP address in a Linux OS environment, both internally and externally, using various commands and tools.

 

 


Internal IP address

An internal IP address (also known as a private IP address) is used on the local network and is not routable on the Internet. They are important for internal communication within the network, but by doing so we save a lot of IP addresses by being able to use the same IP addresses on different networks. For example, your company’s local network has addresses configured from the 192.168.12.0/24 subnet. And in another company, exactly the same network is configured, on the same IP addresses. And these internal gifts are therefore internal, since they do not access the Internet, but these two enterprises communicate through an external, public IP address.

Reserved subnets include:
      - 10.0.0.0 - 10.255.255.255 (subnet mask for classless (CIDR) addressing: 255.0.0.0 or /8)
      - 172.16.0.0 - 172.31.255.255 (subnet mask: 255.240.0.0 or /12)
      - 192.168.0.0 - 192.168.255.255 (subnet mask: 255.255.0.0 or /16)

 

They are very easy to remember and understand that if you see one of the addresses from these subnets anywhere, then this is an internal address.

 

 

Using the ip command

The ip command is a universal tool for setting up and managing a network in Linux. It has largely replaced the old ifconfig command.

Run the command in the terminal:

        ip addr show


This command displays detailed information about all network interfaces. Find the inet entry under the desired interface (for example, eth0, wlan0). The output will show the IP address, subnet mask and other information.

   user@host:~# ip addr show
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
    2: ens0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether f0:c6:f9:ca:d6:c6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.12.7/24 brd 192.168.12.255 scope global dynamic noprefixroute ens0
       valid_lft 70376sec preferred_lft 70376sec

 

 

Using the ifconfig command

Although the ifconfig command has been deprecated in favor of the ip command, it is still available on many distributions.

Run the command in the terminal:

     ifconfig


This command will list all network interfaces with their corresponding IP addresses. The inet addr field shows the IP address of each interface.

   user@host:~# ifconfig
    ens0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.12.7  netmask 255.255.255.0  broadcast 192.168.12.255
    ether f0:c6:f9:ca:d6:c6  txqueuelen 1000  (Ethernet)
    RX packets 358201  bytes 22181271 (0.2 GB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 331273  bytes 20447094 (0.2 GB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

 

Using the hostname command

The hostname command with the -I option can also be used to display the IP addresses associated with a host.

Run the command in the terminal:

           hostname -I


This command displays all IP addresses assigned to the host, separated by spaces.

     user@host:~# hostname -I
      192.168.12.7

 

 

Using nmcli

nmcli is a command line tool for managing NetworkManager and is widely used in various Linux distributions.

Run the command in the terminal:

       nmcli device show


This command displays detailed information about all network interfaces managed by NetworkManager, including IP addresses.

   user@host:~# ifconfig
    GENERAL.DEVICE:                         ens0
    GENERAL.TYPE:                           wifi
    GENERAL.HWADDR:                         F0:C6:F9:Ca:D6:C6
    GENERAL.MTU:                            1500
    GENERAL.STATE:                          100 (connected)
    GENERAL.CONNECTION:                     MyWiFi
    GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/11
    IP4.ADDRESS[1]:                         192.168.12.7/24
    IP4.GATEWAY:                            192.168.12.1
    IP4.ROUTE[1]:                           dst = 192.168.12.0/24, nh = 0.0.0.0, mt = 600
    IP4.ROUTE[2]:                           dst = 0.0.0.0/0, nh = 192.168.12.1, mt = 600
    IP4.DNS[1]:                             192.168.12.1

 

 

Using ip route

You can use the ip Route command to get the IP address of your main network interface.

Run the command in the terminal:

       ip route get 1.1.1.1


Replace 1.1.1.1 with any valid IP address. The command will show the routing information and the source IP address used to reach the destination.

   user@host:~# ip route get 8.8.8.8
    8.8.8.8 via 192.168.12.1 dev ens0 src 192.168.12.7 uid 0

 

 


 

Other articles on Linux administration:


    - Manually remount fstab without rebooting the Linux server

    - Bash If Statement: Syntax, Variations, Use Cases, Commands, and More!

    - 10 most frequently used examples for IPTABLES

    - Cron - Schedule tasks on Linux servers correctly

 


 

External IP address

 

The external IP address (also known as the public IP address) is used to identify your device on the Internet. This IP address is routed across the Internet and assigned by your Internet Service Provider (ISP).


Using the Curl Command

Curl is a command line tool for passing data using URLs. It can be used to contact web services to obtain your public IP address, such as ifconfig.me.

 Run the command in the terminal:

          curl ifconfig.me

 

This command sends a request to ifconfig.me and displays your public IP address.

   user@host:~# curl ifconfig.me
    94.211.13.25

 

 

Using wget

wget is another command line utility for downloading content from web servers. It can also be used to request your public IP address. Let's look at an example with another web service - icanhazip.com

 Run the command in the terminal:

    wget -qO- icanhazip.com

 

This command also extracts the public IP address from ifconfig.me and outputs it to the terminal.

   user@host:~# wget -qO- icanhazip.com
    94.211.13.25

 

 

Using dig

dig (Domain Information Groper) is a powerful utility for searching information from DNS servers. It can be used to find out your public IP address by querying OpenDNS.

 Run the command in the terminal:

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

This command sends a DNS query to OpenDNS, which responds with your public IP address.

   user@host:~# dig +short myip.opendns.com @resolver1.opendns.com
    94.211.13.25

    

 

Using the host command

The host command is another DNS server lookup utility that can be used to find out your public IP address.

 Run the command in the terminal:

        host myip.opendns.com resolver1.opendns.com


This command calls OpenDNS and returns your public IP address.

   user@host:~# host myip.opendns.com resolver1.opendns.com
    Using domain server:
    Name: resolver1.opendns.com
    Address: 208.67.222.222#53
    Aliases: 

    myip.opendns.com has address 94.211.13.25

 

 

 

 

GUI Based Methods

 

If you prefer a graphical user interface, you can use various tools available in almost any Linux distribution to find out your server's IP address.

 

Using the Network Manager

Most Linux distributions come with Network Manager, which provides a graphical interface for configuring your network.

You can go to Network Settings from the taskbar or system settings menu and open information about your active network connection (such as Wi-Fi or Ethernet) to view information such as IP address, subnet mask, and gateway.

 

 

Specialized sites

You can also go to any of the sites that provide information about your public IP address, of which the Internet is full. For example, https://2ip.io/ and you will immediately see your IP address in the browser window. But be careful: if you use any VPN browser application, you will see the IP address of the VPN server to which you are currently connected.

 

 


Conclusion

Obtaining a Linux IP address, internal or external, is a fundamental task for network management and troubleshooting. The methods discussed in this article provide various ways to access this information using command line tools, GUI applications, or a web browser. Whether you prefer to use the ip command, use web services with Curl or Wget, or use graphical interfaces, understanding these techniques will ensure that you can effectively manage and troubleshoot network configurations on Linux servers.