How to find Public IP address AWS EC2 or Lightsail VM

ADVERTISEMENTS



Find Public IP address AWS EC2 or Lightsail VM

Open the terminal application and login using ssh: ssh ec2-user@my-aws-instanace-name To get public IPv4 address assigned by the AWS for EC2/Lightsail vm, run:

dig +short myip.opendns.com @resolver1.opendns.com

Or dig TXT +short o-o.myaddr.l.google.com @ns1.google.com You should see your IP address on the screen. It is the fastest way to find out your public IP address without using 3rd party site. Another option, run curl http://checkip.amazonaws.com

I need to get public IPv4 address on current EC2 or Lightsail VM Linux/Unix instance. How do I find Public IP address AWS EC2 or Lightsail VM?One can easily find out their public or private IPv4 IP address on a Linux or Unix-like operating systems running on AWS EC2 or Lightsail VM. This page explains how to find out your IP address using command-line options for AWS EC2 instance.

Let us see all commands and options in details.

Use dig command to find EC2 or Lightsail VM IP address

Type the following dig command:

dig +short myip.opendns.com @resolver1.opendns.com

## or ##

dig TXT +short o-o.myaddr.l.google.com @ns1.google.com

My IP address of AWS EC2 vm is as follows:

3.227.102.13

We can use the host command too:

$ host myip.opendns.com resolver1.opendns.com

Get public IP address of current EC2 instance

One can use the following curl command to get private IPv4:

curl http://169.254.169.254/latest/meta-data/local-ipv4

And, to get public IPv4, run:

curl http://169.254.169.254/latest/meta-data/public-ipv4

curl http://checkip.amazonaws.com

Of course, you can use third party web sites too:

curl icanhazip.com

curl ifconfig.me

My aws Lightsail IP address:

3.227.102.13

Find and store public IP address on current EC2 instance

Here is how to store is using bash shell:

my_ip=$(curl http://checkip.amazonaws.com)

Display it back using the echo command/printf command:

echo "AWS EC2 IP - $my_ip"

Finding public IP address AWS EC2 or Lightsail VM using aws cli

First, make sure your aws cli configured, run:

aws configure

You need to input your AWS API access token and key. See this page about aws cli. For Lightsail VM, try the following command to obtain information about all static IPs in your account:

aws lightsail get-static-ips

We can get information about a specific static IP. For example, find info about a static IP named “my-www-static-ip1”

aws lightsail get-static-ip --static-ip-name my-www-static-ip1



aws ec2 describe-instances --instance-ids { YOUR_ID_HERE } --query 'Reservations[*].Instances[*].PublicIpAddress' --output text

Conclusion

For EC2 instance try the following (feel free to replace {YOUR_ID_HERE} with actual EC2 instance ID):

You learned how to obtain the static IP addresses of AWS EC2 or Lightsail instance running either Linux or Unix/FreeBSD operating system.



1 of 2 in the Find IP Address Tutorial series. Keep reading the rest of the series: How to find Public IP address AWS EC2 or Lightsail VM Find My Public IP Address From Command Line On a Linux This entry isofin theseries. Keep reading the rest of the series: