How to create a variable that contains your public IP address in Linux

Home > Search > How-to
  by

Another option on Linux is to create a variable that contains your public IP address. To get your public IP address.

[root@server1 ~]# dig +short myip.opendns.com @resolver1.opendns.com
12.34.56.78

 

Add the following to the /root/.bash_profile file.

PUBLIC_IPV4=$(dig +short myip.opendns.com @resolver1.opendns.com)
export PUBLIC_IPV4

 

We need to refresh root's environment variables. Switch to any other account, and then back to root.

[root@server1 ~]# su - john.doe
[john.doe@server1 ~]# su - root

 

You can now get your public IP address by echoing the PUBLIC_IPV4 variable.

[root@server1 ~]# echo $PUBLIC_IPV4
12.34.56.78


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments