Skip to content
Ercan Ermis
Ercan Ermis

notes for everyone about cloud technology

  • Cloud
    • AWS
    • GCP
  • Container
    • Kubernetes
    • Docker
  • Linux
  • DevOps
  • Privacy Policy
  • Contact
Ercan Ermis

notes for everyone about cloud technology

Unveiling the Mystery: Decoding IP Address Retrieval in Linux

Ercan, May 11, 2023

Understanding how to retrieve an IP address in Linux can be a bit tricky, especially for beginners. The good news? It’s not as complicated as it seems! We’re here to simplify it for you. Let’s dive in.

Getting Started

In a nutshell, an IP address is a unique identifier for your machine on a network. It’s like your computer’s postal address. In Linux, there are several ways to find this address, and two popular commands for doing this are ifconfig and ip.

Understanding the ifconfig Command

ifconfig is one of the oldest tools used to configure network interfaces in Linux. It displays the current network configuration and allows you to set up network interfaces.

Here’s how to use it to find your IP address:

export LOCAL_IP=$(ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}')
echo $LOCAL_IP

Let’s break this down:

  • ifconfig outputs the details of all network interfaces.
  • grep "inet " filters lines containing “inet”, which precedes the IP address.
  • grep -Fv 127.0.0.1 filters out the localhost IP.
  • awk '{print $2}' prints the second column, which is the IP address.

Exploring the ip Command

The ip command is a newer tool that’s intended to replace ifconfig. It provides more extensive features and is now the default command for network management in many Linux distributions.

Here’s how to use it to find your IP address:

export LOCAL_IP=$(ip route get 1 | awk ‘{print $NF;exit}’)
echo $LOCAL_IP

Breaking this down:

  • ip route get 1 finds the IP address that would be used to reach the Internet.
  • awk '{print $NF;exit}' prints the last field, which is the IP address.

Comparing ifconfig and ip

So, what’s the difference between ifconfig and ip?

  1. Age and Support: ifconfig is older and is being deprecated in many new Linux distributions in favor of ip. But, it’s still available and used in many systems.
  2. Functionality: ip offers more extensive functionality. It doesn’t just handle network interfaces – it also manages routing tables, neighbors, and more.
  3. Output: ip provides more detailed and precise output. The ifconfig command’s output can be harder to parse and interpret, especially for complex setups.
  4. Handling of Multiple Addresses: ifconfig can struggle with multiple IP addresses assigned to a single interface, whereas ip handles this smoothly.

Final Thoughts

While both ifconfig and ip can fetch your IP address, ip is a more powerful and versatile tool. But remember, these commands might output multiple IP addresses if you have more than one network interface.

Mastering these commands will give you better control over your Linux system and help you troubleshoot network issues more efficiently. Happy networking!

Share on Social Media
x facebook linkedin reddit
Linux ifconfigipnetwork

Post navigation

Previous post
Next post
  • AWS (45)
    • Serverless (4)
  • Best (9)
  • DevOps (16)
  • Docker (10)
  • GCP (3)
  • Kubernetes (3)
  • Linux (13)
  • Uncategorized (6)

Recent Posts

  • Automating AWS CloudWatch Log Group Tagging with Python and Boto3
  • Automating AWS ECR Tagging with Python and Boto3
  • Automating ECR Image Cleanup with Bash
  • Update ECR Repositories with Bash Script
  • Why Automated Tests Are Essential in Your CI/CD Pipeline and Development Flow
  • Streamline Your AWS ECR Management with This Powerful Bash Script
  • Setting up DKIM for Google Workspace (Gmail) using Terraform and AWS Route 53
  • Automate AWS Site-to-Site VPN Monitoring
  • Optimizing Docker Images: Tips for Reducing Image Size and Build Time
  • Monitoring EC2 Disk Space with a Simple Bash Script and Slack Alerts
  • Securing Docker Containers: Best Practices for Container Security
  • Mastering Dockerfile: Writing Efficient, Scalable Container Builds
  • Migrating a Git Repository from GitLab to GitHub with GPG-Signed Commits
  • Accessing AWS Services in Private Subnets Without 0.0.0.0/0
  • Understanding AWS Regions, Availability Zones, and VPCs: A Comprehensive Guide
©2025 Ercan Ermis | WordPress Theme by SuperbThemes