Open In App
Related Articles

How to Find Your IP Address in Linux | ifconfig Command

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Knowing your IP address is fundamental for network administration, troubleshooting, and various Linux system tasks. In this article, we will explore several methods to find your IP address in a Linux environment. Whether you are a seasoned Linux user or just getting started, understanding these methods will empower you to navigate and manage your network effectively.

How to Find Your IP Address in Linux Using `ifconfig Command:

ifconfig (interface configuration) command is used to configure the kernel-resident network interfaces. It is used at the boot time to set up the interfaces as necessary. After that, it is usually used when needed during debugging or when you need system tuning. Also, this command is used to assign the IP address and netmask to an interface or to enable or disable a given interface.

Syntax of `ifconfig`Command in Linux

ifconfig [interface] [options]

Where:

  • [interface] is the network interface you want to configure or display information for (e.g., eth0, wlan0).
  • [options] are various command-line options that can be used to modify the behavior of ifconfig.

Newer versions of some Linux distributions don’t have ifconfig command pre-installed. So, in case, there is an error “ifconfig: command not found”, Then execute the following command to install ifconfig.

Installing net-tools in Linux

For Debian, Ubuntu, and related Linux distributions.

sudo apt-get install net-tools

For CentOS or RPM(RedHat Package Manager) based Linux

yum install net-tools

or

dnf install net-tools

This will install `ifconfig` along with some other networking commands like arp, route, ipmaddr.

Finding Your Ip Address in Linux Using `ifconfig` Command

To view information about all network interfaces on your Linux system, simply execute the following command:

ifconfig


Finding IP Address in Linux Using `ifconfig`

Finding IP Address in Linux Using `ifconfig`


This command will provide a comprehensive list of all network interfaces along with their respective IP addresses, MAC addresses, and other relevant details.

Options available in `ifconfig` Command in Linux

Here are the most commonly used option in ifconfig command in linux

Option

Description

Syntax

-a

Display all interfaces, including those that are down

ifconfig -a

-s

Display a short list, instead of details

ifconfig -s

-v

Run the command in verbose mode

ifconfig -v

up

Activate the driver for the given interface

ifconfig interface up

down

Deactivate the driver for the given interface

ifconfig interface down

add addr/prefixlen

Add an IPv6 address to an interface

ifconfig interface add addr/prefixlen

del addr/prefixlen

Remove an IPv6 address from an interface

ifconfig interface del addr/prefixlen

[-]arp

Enable/disable the use of ARP protocol on an interface

ifconfig interface [-]arp

[-]promisc

Enable/disable promiscuous mode on an interface

ifconfig interface [-]promisc

[-]allmulti

Enable/disable all-multicast mode for an interface

ifconfig interface [-]allmulti

mtu N

Set the Maximum Transfer Unit (MTU)

ifconfig interface mtusize size

–help

Display help related to the ifconfig command

ifconfig –help

What is Public and Private IP in Linux

In the realm of networking, both in Linux and other operating systems, IP addresses are categorized as either public or private. These designations are crucial for facilitating communication between devices on a network, whether it’s the global internet or a local intranet. Let’s delve into the distinctions between public and private IP addresses in Linux.

1) How to Find Your Public IP Addresses in Linux:

A public IP address is a globally unique identifier assigned to a device on the internet. It serves as the address by which other devices on the internet can find and communicate with it. Public IP addresses are assigned by the Internet Assigned Numbers Authority (IANA) to Internet Service Providers (ISPs) and other organizations that control access to the global internet.

In Linux, you can determine the public IP address of a system by using external services or commands like curl or wget to query a web service. For example:

curl ifconfig.me

This command retrieves your public IP address from a web service.

Public IP addresses are essential for servers, websites, and other devices that need to be directly accessible from the internet. They are globally routable, meaning they can be reached from any location on the internet.

Different Ways to Find Your Public IP Address in Linux

1) Using `wget` with `ifconfig.me` to Find Your IP Address in Linux

Similar to curl, this uses the ifconfig.me service to fetch your public IP address.

wget -qO- ifconfig.me


Using `wget` with `ifconfig.me` to Find Your IP Address in Linux

Using `wget` with `ifconfig.me` to Find Your IP Address in Linux


2) Using `dig` with `resolver1.opendns.com` to Find Your IP Address in Linux

This command uses the OpenDNS resolver to query your public IP address.

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


Using `dig` with `resolver1.opendns.com` to Find Your IP Address in Linux

Using `dig` with `resolver1.opendns.com` to Find Your IP Address in Linux


3) Using `curl` with `icanhazip.com`to Find Your IP Address in Linux

This command queries the icanhazip.com service to obtain your public IP address.

curl icanhazip.com


 Using `curl` with `icanhazip.com`to Find Your IP Address in Linux

Using `curl` with `icanhazip.com`to Find Your IP Address in Linux


4) Using `wget` with `icanhazip.com` to Find Your IP Address in Linux

Similar to the curl command, this uses the icanhazip.com service to fetch your public IP address.

wget -qO- icanhazip.com


 Using `wget` with `icanhazip.com` to Find Your IP Address in Linux

Using `wget` with `icanhazip.com` to Find Your IP Address in Linux


5) Using host with dns.google to Find Your IP Address in Linux

This command utilizes the DNS service provided by Google to resolve your public IP address.

host myip.opendns.com resolver1.opendns.com


Using host with dns.google to Find Your IP Address in Linux

Using host with dns.google to Find Your IP Address in Linux


2) How to Find Your Private IP Addresses in Linux:

Contrastingly, private IP addresses are used within a private network and are not directly accessible from the internet. These addresses are defined in reserved address ranges specified by the Internet Engineering Task Force (IETF) in RFC 1918. The commonly used private IP address ranges are:

  • 10.0.0.0 to 10.255.255.255 (10.0.0.0/8)
  • 172.16.0.0 to 172.31.255.255 (172.16.0.0/12)
  • 192.168.0.0 to 192.168.255.255 (192.168.0.0/16)

These addresses are intended for use in local networks, such as home or corporate intranets. Devices within the same private network can communicate with each other using these private IP addresses, but they rely on a mechanism called Network Address Translation (NAT) to access the internet through a shared public IP address.

In Linux, you can view the private IP addresses of your system using the ifconfig or ip addr commands. For example:

ifconfig

or

ip addr

Different Ways to Find Your Private IP Address in Linux

1) Using `hostname` to Find Your IP Address in Linux

The -I option with the hostname command can be used to display the private IP address of your machine.

hostname -I


Using hostname to Find Your IP Address in Linux

Using hostname to Find Your IP Address in Linux


2) Using `nmcli` (NetworkManager command-line tool) to Find Your IP Address in Linux

If you’re using NetworkManager, this command filters out IPv4 addresses associated with your network interfaces.

nmcli dev show | grep IP4.ADDRESS


Using nmcli to Find Your IP Address in Linux

Using nmcli to Find Your IP Address in Linux


3) Using `awk` with `ifconfig` to Find Your IP Address in Linux

This command uses the awk tool to filter and print only the private IP addresses from the ifconfig output.

ifconfig | awk '/inet / {print $2}'


Using `awk` with `ifconfig` to Find Your IP Address in Linux

Using `awk` with `ifconfig` to Find Your IP Address in Linux


4) Using `grep` with `ip` to Find Your IP Address in Linux

This command uses grep with Perl-compatible regular expressions to extract private IP addresses from the ip command output.

ip addr show | grep -oP 'inet \K[\d.]+'


Using `grep` with `ip` to Find Your IP Address in Linux

Using `grep` with `ip` to Find Your IP Address in Linux


5) Using `ss` (socket statistics) command to Find Your IP Address in Linux

This complex command lists the IP addresses to which the system is listening for incoming connections.

ss -tunapl | grep LISTEN | awk '{print $5}' | cut -d: -f1 | sort -u


Using `ss` (socket statistics) command to Find Your IP Address in Linux

Using `ss` (socket statistics) command to Find Your IP Address in Linux


Frequently Asked Question

1) How can I quickly check my IP address in Linux using the command line?

You can use the `ip` command with the `address` option. Open a terminal and type `ip address` or `ip a`, and look for the line starting with “inet” followed by your IP address.

2) How to specificity display only the public IP address in Linux?

You can use a command like `curl` or `wget` to fetch your public IP from a web service.

For example:

curl ifconfig.me

or

wget -qO- ifconfig.me

3) Can I find my IP address in Linux using GUI tools?

Yes, many Linux distributions come with network management tools that provide a graphical interface. For example, in Ubuntu, you can use the network settings or system monitor to find your IP address.

4) How do I find the IP address of a specific network interface in Linux?

You can use the `ifconfig` or `ip address` command followed by the interface name.

For example:

ifconfig eth0

or

ip address show eth0

This will display details, including the IP address, for the specified interface.

5) How can I display detailed information about all network interfaces in Linux?

You can use the `ifconfig` or `ip address` show command to display comprehensive information about all network interfaces.

For example:

ifconfig

or

ip address show

It will display details such as IP addresses, netmasks, and other network-related information for all available interfaces on your Linux system.

Conclusion

In this article, we’ve explored how to find your IP address in Linux using the ifconfig command. We also discuss what is private and public IP address and how to display both of the IP Address .This essential skill is crucial for effective network management. Whether you’re a seasoned Linux user or a beginner, understanding these simple commands empowers you to navigate and control your network effortlessly.



Last Updated : 12 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads