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: " $publicipecho "Local IP: " $localip}
Then you just need to type myip into your terminal / Bash shell and it'll show you your public IP address and your local IP address.
You could add this to your .bashrc file (eg. nano ~/.bashrc) so you always have it to use. Remember after adding it to the .bashrc file to source it to load in your changes: . ~/.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 -I
This 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 ifconfig (older systems)
ifconfig
Note: ifconfig is deprecated on modern Linux systems. Use ip addr instead.
Using curl or wget for public IP
curl -s https://api.ipify.org\ncurl -s https://ifconfig.me\ncurl -s https://icanhazip.com
Frequently 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. Business internet plans often include static IPs by default.
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. All devices on your local network share the same public IP address when accessing the internet, with your router using NAT (Network Address Translation) to manage the connections.
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. The method shown in this guide combines both in a convenient shell function. You can also use network tools like nmcli on systems with NetworkManager, or check /sys/class/net/ for interface information.