Home >Operation and Maintenance >Linux Operation and Maintenance >5 Commands to Get Public IP Using Linux Terminal
Public IP is used for communication between computers over the Internet. Computers running with a public IP can be accessed from around the world using the Internet. So we can say it is the identity of the computer on the internet.
The question now is, how do we know our public intellectual property rights? For a computer with a GUI, you can easily get the IP using a web tool like this, but how do you get the public IP of a computer that only has terminal access.
Solution: You can find your system’s public IP with a Linux terminal using one of the following commands. These are also useful in shell scripts.
Use Linux commands to find the public IP
Command 1:
Use the dig command to find the public IP address. The dig command is a DNS lookup utility used on Linux systems to find public IP addresses by connecting to an OpenDNS server.
$ dig +short myip.opendns.com @resolver1.opendns.com
Command 2:
Use the wget command to obtain the public IP address, as shown in the example below.
$ wget http://ipecho.net/plain -O - -q ; echo
Commands 3, 4 and 5:
Use the curl command to get the public address.
$ curl ipecho.net/plain; echo
$ curl icanhazip.com
$ curl ifconfig.me
Get public IP in shell script
We can simply use the following command in shell script to get the public IP of the computer and store them in variables so that in any shell script Location usage.
#!/bin/bash PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo` echo $PUBLIC_IP
This article has ended here. For more other exciting content, you can pay attention to the Linux Tutorial Video column of the PHP Chinese website!
The above is the detailed content of 5 Commands to Get Public IP Using Linux Terminal. For more information, please follow other related articles on the PHP Chinese website!