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.
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
Ordig 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
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
Various command to find your EC2 or Lightsail VM instant public IP address running on AWS cloud
aws ec2 describe-instances --instance-ids {YOUR_ID_HERE} --query 'Reservations[*].Instances[*].PublicIpAddress' --output text
Conclusion
You learned how to obtain the static IP addresses of AWS EC2 or Lightsail instance running either Linux or Unix/FreeBSD operating system.
- How to find Public IP address AWS EC2 or Lightsail VM
- Find My Public IP Address From Command Line On a Linux
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 0 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |