Open In App

How to Find Your IP Address in Linux | ifconfig Command

Last Updated : 15 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Why is this important?

  • Network Configuration: ifconfig allows users to configure network interfaces, including setting IP addresses, netmasks, and broadcast addresses.
  • Network Troubleshooting: It provides detailed information about network interfaces, which is essential for diagnosing connectivity issues, monitoring network traffic, and checking interface statuses.
  • Interface Management: Users can enable or disable network interfaces, which is crucial for managing network connections and controlling which interfaces are active.
  • MAC Address Modification: ifconfig enables users to change the MAC address of a network interface, which can be useful for security purposes or bypassing network restrictions.
  • Temporary Network Changes: It allows for temporary network configuration changes without editing configuration files, useful for testing and troubleshooting.

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

Linux ifconfig Command Examples

Display Specific Network Interface

This command shows detailed information about the specified interface, eth0.

ifconfig eth0

Enable a Network Interface

This command activates the specified network interface, eth0.

ifconfig eth0 up

Disable a Network Interface

This command deactivates the specified network interface, eth0.

ifconfig eth0 down

Assign an IP Address

This command assigns the IP address 192.168.1.10 to the specified network interface, eth0.

ifconfig eth0 192.168.1.10

Set a Netmask

This command sets the netmask for the specified network interface, eth0.

ifconfig eth0 netmask 255.255.255.0

Set a Broadcast Address

This command sets the broadcast address for the specified network interface, eth0.

ifconfig eth0 broadcast 192.168.1.255

Change the MAC Address

This command changes the MAC address of the specified network interface, eth0.

ifconfig eth0 hw ether 00:1a:2b:3c:4d:5e

Add an Alias to a Network Interface

This command adds an alias with IP address 192.168.1.20 to the specified network interface, eth0.

ifconfig eth0:0 192.168.1.20

Remove an Alias from a Network Interface

This command removes the alias eth0:0 from the specified network interface.

ifconfig eth0:0 down

ifconfig Gateway

To set a gateway using the ifconfig command, you typically need to use route because ifconfig itself does not configure gateways. Here’s how to do it in a simple way:

Set IP Address and Netmask (if needed):

ifconfig eth0 192.168.1.10 netmask 255.255.255.0

Set Default Gateway:

route add default gw 192.168.1.1

Here, 192.168.1.1 is the IP address of the gateway.

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.

Find your IP Address in Linux – FAQs

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.

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

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.

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.

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.



Next Article

Similar Reads

three90RightbarBannerImg
  翻译: