Die globale Internet Adresse in der Shell ausgeben, lokale IPv4 und IPv6 Adressen im Terminal abfragen

Show my IP address on the Internet


Output the global Internet address in the shell, query local IPv4 and IPv6 addresses in the terminal

Determining the global IP with this one is surfing the Internet, this is also possible in the command line, for Windows, Linux and macOS.

Run the following command in the Linux terminal to query the public global IP address:

curl ipline.ch

Determine the local private IP address with the following command:

$ /sbin/ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'

For Linux operating systems installed in German:

$ /sbin/ifconfig eth0 | grep 'inet Adresse' | cut -d: -f2 | awk '{print $1}'

Note: ipconfig is deprecated, for current distributions, such as Ubuntu 18.04 or CentOS 7 and Fedora, the ip command is applied, this command returns IPv4 addresses:

$ ip -4 addr

The slightly clearer and also more colorful version as follows:

$ ip -4 addr | grep -oP '(?<=inet\s)\d+(\.\d+){3}'

Only IPv6 addresses should be output:

$ ip -6 addr | grep -oP '(?<=inet6\s)[\da-f:]+'

Also, the hostname command can provide useful values:

$ hostname -i | awk '{print $3}'

Show IP address on macOS

macOS returns the IP address from the terminal with the following command.

$ /sbin/ifconfig en0 | awk '/inet /{print $2}'

And the hostname command is also available on macOS:

$ hostname -I

Find Windows IP address

Windows uses ipconfig to output the IP addresses.

C:\> ipconfig /all

Also only IPv4 addresses can be queried:

C:\> ipconfig | findstr /i "ipv4"

To reduce the amount of information that ipconfig generates, command wmic can help.

C:\> wmic NICCONFIG GET IPAddress

This query e.g. outputs the IP address of each interface.

C:\> netsh interface ipv4 show address

View IP addresses in the PowerShell with Get-NetIPAddress, useful also in scripts

PS C:\> Get-NetIPAddress | Format Table

Again, only IPv4 addresses can be queried:

PS C:\> Get-NetIPAddress -AddressFamily IPv4 | Format Table

By typing Get-NetIPAddress -? all parameters to the cmdlet are output.

View public IP address and local private IP address in powershell

$GlobalIP = Invoke-RestMethod -Uri http://ipline.ch/echo
$PrivatIP = $(Get-NetIPAddress -InterfaceIndex 11 -AddressFamily IPv4).IPAddress
Write-Host "Meine öffentliche IPv4 Adresse ist:" $GlobalIP
Write-Host "Meine private IPv4 Adresse ist:" $PrivatIP

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published.