This brief tutorial explains how you can quickly determine your IP address via the Linux command line.
Sometimes I need to find out what the public IP address of a particular machine is. If I have access to a web browser on that machine I just use a service like https://www.whatismyip.com to find this information. However, on a remote Linux server it is not practical to use the web browser method. So instead I use the following hack. Note: this also works in macOS if you install wget.
I enter the following line into a Linux command prompt:
# wget -q -O – checkip.dyndns.org|sed -e ‘s/.*Current IP Address: //’ -e ‘s/<.*$//'
Pretty useful stuff this. If you need to use this command often it might be a good idea to create a script with the command so that you need not have to remember it. Create a new file using your favorite text editor. Enter the following lines:
wget -q -O – checkip.dyndns.org|sed -e ‘s/.*Current IP Address: //’ -e ‘s/<.*$//'
Now save the file as something like /usr/bin/myipaddress. Grant it executable privileges like this:
# chmod +x /usr/bin/myipaddress
If you’re using macOS, copy the myipaddress file to /usr/local/bin instead of /usr/bin/ and make sure to grant it executable privileges too.
Now you have a script that you can use by running myipaddress in the command line. It will output the current public IP address your Linux machine is using. This can be quite useful if you use things like IP-based authentication, or if you use a dynamic DNS service.