DEV Community

howtouselinux
howtouselinux

Posted on • Edited on • Originally published at howtouselinux.com

different ways to check ip address in Linux

reference:
https://www.howtouselinux.com/post/check-ip-address-in-linux

To check the IP address in Linux, there are several commands you can use.

3 ways to find IP address in Linux - howtouselinux

You can check the private IP address in Linux using commands such as “ifconfig” (deprecated), “ip addr show” command.

favicon howtouselinux.com

Here are a few commonly used methods:

  1. Using the ifconfig command:

    • Open a terminal.
    • Type ifconfig and press Enter.
    • Look for the network interface you are interested in (e.g., eth0, wlan0).
    • The IP address will be listed next to the inet or inet addr field.
  2. Using the ip addr command:

    • Open a terminal.
    • Type ip addr and press Enter.
    • Look for the network interface you are interested in (e.g., eth0, wlan0).
    • The IP address will be listed next to the inet field.
  3. Using the hostname -I command:

    • Open a terminal.
    • Type hostname -I and press Enter.
    • The IP address will be displayed.

These methods should work on most Linux distributions. However, please note that the specific commands and output may vary slightly depending on the distribution and version of Linux you are using.

It's also worth mentioning that some Linux distributions have deprecated the use of the ifconfig command in favor of the ip command. So, it's recommended to use the ip command for checking IP addresses in newer Linux systems[5].

Additionally, you can also check the IP address through the network settings in the graphical user interface (GUI) of your Linux distribution. The steps to access the network settings may vary depending on the desktop environment you are using.

To check the public IP address in Linux, there are several commands you can use. Here are a few commonly used methods:

  1. Using the host command:

    • Open a terminal.
    • Type host -t A myip.opendns.com resolver1.opendns.com and press Enter.
    • The public IP address will be displayed.
  2. Using the curl command:

    • Open a terminal.
    • Type curl ifconfig.me or curl icanhazip.com or curl ipecho.net/plain and press Enter.
    • The public IP address will be displayed.
  3. Using the dig command:

    • Open a terminal.
    • Type dig +short myip.opendns.com @resolver1.opendns.com and press Enter.
    • The public IP address will be displayed.
  4. Using the wget command:

    • Open a terminal.
    • Type wget -qO- ifconfig.me or wget -qO- icanhazip.com or wget -qO- ipecho.net/plain and press Enter.
    • The public IP address will be displayed.

Additionally, you can also check the public IP address through various websites that provide this service. You can access most of these services using the curl or wget utility. The commands to fetch your public IP address from some of these websites are mentioned above.

To automate the process of sending an email notification when the public IP address changes in Linux, you can modify the script mentioned in the search results[1]. Here's an example script that uses the curl command to fetch the public IP address and sends an email notification using the sendemail command:

#!/bin/bash

# File to store the previous public IP address
ip_file="/path/to/ip_file.txt"

# Email configuration
SMTPFROM=yourusername@gmail.com
SMTPTO=yourusername@gmail.com
SMTPSERVER=smtp.gmail.com:587
SMTPUSER=yourusername
SMTPPASS=YourPassword
SUBJECT="Public IP address has changed"

# Fetch the current public IP address using the curl command
WAN_IP=$(curl -s ifconfig.me/ip)

# Check if the IP file exists
if [ ! -f "$ip_file" ]; then
    touch "$ip_file"
fi

# Read the previous IP address from the file
previous_ip=$(cat "$ip_file")

# Compare the current IP address with the previous IP address
if [ "$WAN_IP" = "$previous_ip" ]; then
    echo "Public IP did not change."
else
    echo "Public IP has changed. Writing new value..."
    echo "$WAN_IP" > "$ip_file"
    echo "Sending email notification..."

    # Email message
    MESSAGE="Network configuration change warning! The IP address is now $WAN_IP."

    # Send the email notification using the sendemail command
    sendemail -f $SMTPFROM -t $SMTPTO -u "$SUBJECT" -m "$MESSAGE" -s $SMTPSERVER -xu $SMTPUSER -xp $SMTPPASS -o tls=yes
fi
Enter fullscreen mode Exit fullscreen mode

Save the above code in a file with a .sh extension (e.g., ip_change_notification.sh). Make the file executable by running the following command:

chmod +x ip_change_notification.sh
Enter fullscreen mode Exit fullscreen mode

Now, you can run the script periodically using a cron job to check for changes in the public IP address and send an email notification when it changes. For example, to run the script every hour, you can add the following line to your crontab:

0 * * * * /path/to/ip_change_notification.sh >> /path/to/logfile.log 2>&1
Enter fullscreen mode Exit fullscreen mode

This will run the script every hour and redirect the output to a log file. You can change the frequency and the log file path as per your requirements.

Please note that the specific command and output may vary slightly depending on the distribution and version of Linux you are using. Additionally, you may need to install the sendemail package or any other required packages for sending emails.

Remember to replace the email configuration variables (SMTPFROM, SMTPTO, SMTPSERVER, SMTPUSER, SMTPPASS) with your own email credentials and adjust the file paths as necessary.

Top comments (0)