Peter Stuifzand

Find your local IP address in Linux

Today I created this simple script that will find your local IP address or you Linux machine.

#!/bin/sh
# Shows ip address of eth0
/sbin/ifconfig | awk '/^eth0/,/^$/' | awk '/inet addr/ { print $2 }' | cut -d: -f2

It uses the output of ifconfig. First it finds the part that contains the information for eth0. Then it find the line with the inet addr, which contains your IP address. At the end it cuts the line in two parts and only prints the second part.

If you want to print the IP address of another interface, then you need to change the name eth0 to that interface name.

© 2023 Peter Stuifzand