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:
1 2 |
curl ipline.ch |
Determine the local private IP address with the following command:
1 2 |
$ /sbin/ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}' |
For Linux operating systems installed in German:
1 2 |
$ /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.2 and Fedora 28, the ip command is applied, this command only returns IPv4 addresses:
1 2 |
$ ip -4 addr |
The slightly clearer and also more colorful version as follows:
1 2 |
$ ip -4 addr | grep -oP '(?<=inet\s)\d+(\.\d+){3}' |
Only IPv6 addresses should be output:
1 2 |
$ ip -6 addr | grep -oP '(?<=inet6\s)[\da-f:]+' |
Also, the hostname command can provide useful values:
1 2 |
$ hostname -i | awk '{print $3}' |
macOS IP address

macOS returns the IP address from the terminal with the following command.
1 2 |
$ /sbin/ifconfig en0 | awk '/inet /{print $2}' |
And the hostname command is also available on macOS:
1 2 |
$ hostname -I |
Find Windows IP address

Windows uses ipconfig to output the IP addresses.
1 2 |
C:\> ipconfig /all |
Also only IPv4 addresses can be queried:
1 2 |
C:\> ipconfig | findstr /i "ipv4" |
To reduce the amount of information that ipconfig generates, command wmic can help.
1 2 |
C:\> wmic NICCONFIG GET IPAddress |
This query e.g. outputs the IP address of each interface.
1 2 |
C:\> netsh interface ipv4 show address |
View IP addresses in the PowerShell with Get-NetIPAddress, useful also in scripts
1 2 |
PS C:\> Get-NetIPAddress | Format Table |
Again, only IPv4 addresses can be queried:
1 2 |
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
1 2 3 4 5 |
$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 |