To check the IP address in Linux, you can use one of the following commands:
1. Using ip
command (modern and recommended):
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:
- Updates your package list
- Installs
curl
,wget
, anddnsutils
(which providesdig
) - 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:
- Save the script to a file, e.g.
get_public_ip.sh
- 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 ! 🐧💻