As part of an installation script, I want to make an "educated guess" about the machines public IP address. It should be as robust as possible, working on a variety of hosts and platforms. Here is an example:
https://gist.github.com/4669037
This script uses 3 methods and then defaults to the value of /etc/hostname
. I have only tested this on Ubuntu so far. Will this be likely to work on other distributions? And are there additional methods that I could add?
To find out the IP address of Linux/UNIX/*BSD/macOS and Unixish system, you need to use the command called ifconfig on Unix and the ip command or hostname command on Linux. These commands used to configure the kernel-resident network interfaces and display IP address such as 10.8. 0.1 or 192.168. 2.254.
You can also see your public IP address in the Terminal: just type curl ifconfig.me and your public IP will pop up.
Type the following dig (domain information groper) command on a Linux, OS X, or Unix-like operating systems to see your own public IP address assigned by the ISP: $ dig +short myip.opendns.com @resolver1.opendns.com.
Bash command for showing the public IP address of Linux and Unix systems. We can use the host command or dig command as follows to display the public IP address of your Linux or Unix system: host myip.opendns.com resolver1.opendns.com. OR. dig +short myip.opendns.com @resolver1.opendns.com. How To Find My Public IP Address From Command Line On ...
Bash Shell Command to Find or Get IP address. If no arguments are given to ifconfig command, it displays the status of the currently active interfaces. It shows an Ethernet IP address, Mac address, subnet mask, and other information. Type the following /sbin/ipconfig command to display IP address and releated networking information:
The ip command display information about ip address, manipulate routing, network devices, interfaces, tunnels and much more. The following ip command will show all ip address assigned to your system:
It is recommend that you use the ip command under Linux based systems. The following command will show all ip address: # ip addr show. To see information about eth0 ip address, enter: # ip addr show eth0. Sample outputs:
curl ipinfo.io/ip
Or
wget -q -O - ipinfo.io/ip
Or
lynx -source ipinfo.io/ip
get public ip address
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With