Linux: Get IP / IPv6 (internal/external) on Command Line/Shell
Table of Contents
This is quick tip, howto get internal IP address and external IP address on Linux Shell / Command Line. This guide also show, howto make useful Bash functions to get IP addresses quickly.
Note: All functions could be named as you wish and to make functions permanent, add functions to ~/.bashrc or /etc/bashrc. Also all awk commands should work also with gawk and nawk.
1. Get Internal IP Address(es) on Linux Shell / Command Line⌗
1.1 Get IP and IPv6 Address by Interface⌗
Returns plain IP addresses.
/sbin/ifconfig $1 | grep "inet\|inet6" | awk -F' ' '{print $2}' | awk '{print $1}'
## Example usage ##
/sbin/ifconfig eth0 | grep "inet\|inet6" | awk -F' ' '{print $2}' | awk '{print $1}'
10.20.10.1
0:0:0:0:0:ffff:a14:a01
Create simple bash function (example int-ip) with following command.
function int-ip { /sbin/ifconfig $1 | grep "inet\|inet6" | awk -F' ' '{print $2}' | awk '{print $1}'; }
## Example usage ##
int-ip eth0
10.20.10.1
0:0:0:0:0:ffff:a14:a01
1.2 Get Every Interfaces IP and IPv6 Addresses⌗
Returns every interface IP and IPv6 addresses.
/sbin/ifconfig |grep -B1 "inet\|inet6" |awk '{ if ( $1 == "inet" || $1 == "inet6" ) { print " ",$2 } else if ( $1 != "inet" && $1 != "inet6" ) { print $1 } }'
## Example output ##
lo:
127.0.0.1
::1
--
eth0:
192.168.1.11
fe80::3a2c:4aff:fe48:2a55
Create simple bash function (example int-ips) with following command.
function int-ips { /sbin/ifconfig |grep -B1 "inet\|inet6" |awk '{ if ( $1 == "inet" || $1 == "inet6" ) { print " ",$2 } else if ( $1 != "inet" && $1 != "inet6" ) { print $1 } }'; }
## Example usage ##
int-ips
lo:
127.0.0.1
::1
--
eth0:
192.168.1.11
fe80::3a2c:4aff:fe48:2a55
2. Get External IP Address on Linux Shell / Command Line⌗
I use here ipecho.net service.
2.1 Get External IP Address Using Lynx⌗
Returns plain IP address.
lynx --dump http://ipecho.net/plain
## Example output ##
80.10.10.80
Create simple bash function (example ext-ip) with following command.
function ext-ip () { lynx --dump http://ipecho.net/plain; }
## Example usage ##
ext-ip
80.10.10.80
2.2 Get External IP Address Using Curl⌗
Returns plain IP address.
curl http://ipecho.net/plain; echo
## Example output ##
80.10.10.80
Create simple bash function (example ext-ip) with following command.
function ext-ip () { curl http://ipecho.net/plain; echo; }
## Example usage ##
ext-ip
80.10.10.80