Get Local and Public IP Address
Quickly display both your public and local IP addresses on Linux using a simple Bash function. Perfect for network troubleshooting, server configuration, and SSH connections.
This Bash function retrieves both your public (external) IP address and your local (internal) IP address on Linux. Your public IP is what the internet sees, while your local IP is your address on your internal network. This is useful when configuring firewalls, setting up SSH connections, or troubleshooting network issues.
function myip() {
publicip=$(curl -s https://ipecho.net/plain)
localip=$(ip -o -4 addr show | awk -F '[ /]+' '/global/ {print $4}')
echo "Public IP: " $publicip
echo "Local IP: " $localip
}
Then you just need to type myip into your terminal and it'll show you your public IP address
and your local IP address.
You could add this to your .bashrc file (e.g. nano ~/.bashrc) so you always have it
to use. Remember after adding it to the .bashrc file to source it: . ~/.bashrc or
source ~/.bashrc.
Alternative Methods
There are several other ways to find your IP addresses on Linux. Here are some common alternatives:
Using hostname command
hostname -IThis displays all local IP addresses assigned to your system.
Using ip addr command
ip addr show
This shows detailed information about all network interfaces. Look for the inet lines to find
IPv4 addresses.
Using curl or wget for public IP
curl -s https://api.ipify.org
curl -s https://ifconfig.me
curl -s https://icanhazip.comFrequently Asked Questions
Why does my public IP address change?
Most residential internet connections use dynamic IP addresses assigned by your ISP (Internet Service Provider). These addresses can change when you restart your router, after your DHCP lease expires, or when your ISP reassigns addresses. If you need a stable public IP address, you typically need to request a static IP from your ISP, which may come with an additional fee.
What's the difference between local and public IP?
Your local (private) IP address is used for communication within your home or office network and typically starts with 192.168.x.x, 10.x.x.x, or 172.16.x.x. It's assigned by your router and not visible to the internet. Your public IP address is assigned by your ISP and is what websites and external services see when you connect to them.
Are there other ways to find my IP address?
Yes, there are many methods. For local IP: hostname -I, ip addr, or checking your
router's DHCP client list. For public IP: visiting websites like whatismyip.com, using
curl https://ifconfig.me, or checking your router's WAN settings.