a person holding black and yellow penguin sticker

Command to Check IP address in linux

To check the IP address in Linux, you can use one of the following commands:

ip addr show

or more simply:

ip a

Look for inet under the correct network interface (like eth0, wlan0, etc.).

2. Using hostname command:

hostname -I

This will show the IP address(es) assigned to the machine.

3. Using ifconfig (older method, may not be installed by default):

ifconfig

Look for inet under your network interface.

To check your public IP address in Linux, you can use one of the following commands that query external services:

1. Using curl (if installed):

curl ifconfig.me

or:

curl icanhazip.com

2. Using wget:

wget -qO- ifconfig.me

3. Using dig (from dnsutils package):

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

Here’s a Bash script that:

  1. Updates your package list
  2. Installs curl, wget, and dnsutils (which provides dig)
  3. Checks your public IP using all three methods
#!/bin/bash

# Update package list
echo "Updating package list..."
sudo apt update -y

# Install required tools
echo "Installing curl, wget, and dnsutils..."
sudo apt install -y curl wget dnsutils

# Display Public IP using curl
echo -e "\nPublic IP using curl (ifconfig.me):"
curl ifconfig.me

# Display Public IP using wget
echo -e "\nPublic IP using wget (ifconfig.me):"
wget -qO- ifconfig.me

# Display Public IP using dig
echo -e "\nPublic IP using dig (OpenDNS):"
dig +short myip.opendns.com @resolver1.opendns.com

echo -e "\nDone!"

How to use it:

  1. Save the script to a file, e.g. get_public_ip.sh
  2. Make it executable :
chmod +x get_public_ip.sh

Run it:

./get_public_ip.sh

This works with Ubuntu and Mint distros – the ones I know ! 🐧💻

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.