There are occasions when you need to know what a computer’s IP address is. This should be a reasonably simple thing to do, how hard can it be?
What’s an IP address?
IP addresses are what computers use to communicate with each other over an IP network such as the Internet. Traditionally, these have been four sets of numbers separated by dots; for example
192.0.2.123. However, IPv6 has jumbled things up a bit with much longer IP addresses made up of up to 8 groups of hexadecimal numbers separated by colons; for example
2001:db8::73d4:128e:6ad5:85bb. It’s a good idea to be specific about what sort of IP address one means: IPv4 may still be the most prevalent but IPv6 is catching up and will one day replace IPv4 entirely. We have, after all, run out of IPv4 addresses.
Pressure on ISPs caused by the scarcity of IPv4 addresses has led to NAT (Network Address Translation) becoming ubiquitous in computer networks. This means your computer will almost certainly have a different IPv4 address within your computer network (your internal IPv4 address) than it appears to have when accessing web sites on the Internet (your external IPv4 address). Not only that, but many or all of the computers on your network will appear to share the same IP address when accessing the Internet. NAT requires complex software in your router that rewrites the IP addresses in network packets before sending them out your Internet connection, and does the same rewriting when receiving responses, while keeping track of all the translations it needs to make. Because IPv6 makes the address space so much larger, NAT for IPv6 is not needed and routers can avoid performing this complicated task.
In effect you need to know whether you need your internal or external IPv4 address, or your IPv6 address.
The easy way out
One of the easiest ways to find out your IP address is to ask Google “what is my IP address?“; a box should show up at the top of the results with the answer. This uses the IPv4 or IPv6 external public address that connected to Google’s servers when making the request. The search results also include a number of other web sites that can tell you both your IPv4 and IPv6 addresses at the same time (if you have IPv6), but none of these can see your internal LAN IPv4 address—for that you will need to reach for other tools.
Your local IP addresses
Your computer will almost certainly have several different IPv4 and IPv6 addresses even if IPv6 connectivity is not available on your network. For example, every computer has a
localhost IP address (
127.0.0.1 for IPv4 and
::1 for IPv6) which refers to this computer; everyone’s localhost is the same. IPv6 also has a concept of link-local addresses which are only valid on your local network; unless you have gone out of your way to disable IPv6, you will have an IPv6 link local address even if your router doesn’t understand IPv6.
On Linux, you can find out the IP addresses on your local machine using the
ip address list command, or
ip a l for short:
$ ip a l 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:61:b7:8e brd ff:ff:ff:ff:ff:ff inet 18.104.22.168/24 brd 22.214.171.124 scope global eth0 valid_lft forever preferred_lft forever inet6 2a03:9800:10:12b::2/64 scope global valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe61:b78e/64 scope link valid_lft forever preferred_lft forever
The highlighted lines show this particular system’s internal IPv4 address and IPv6 address. In this particular case, the internal IPv4 address also happens to be its external public address; this is the web server that hosts our web site. The first two IP addresses (on the
lo interface) are the
localhost addresses discussed above, and the IPv6 address that starts
fe80:: is the IPv6 link-local address for that network connection.
Note that the two IP addresses of interest are labelled
scope global. You can filter for those exclusively by adding
scope global to the command:
$ ip a l scope global 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:61:b7:8e brd ff:ff:ff:ff:ff:ff inet 126.96.36.199/24 brd 188.8.131.52 scope global eth0 valid_lft forever preferred_lft forever inet6 2a03:9800:10:12b::2/64 scope global valid_lft forever preferred_lft forever
Similarly, you can request only the IPv4 or IPv6 addresses by adding -4 or -6 to the command, for example:
$ ip -4 a l 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 184.108.40.206/24 brd 220.127.116.11 scope global eth0 valid_lft forever preferred_lft forever
A remote server’s external address
Sometimes we find ourselves on a client’s remote system, connected over SSH, and we need to find out what IP address(es) it presents to the outside world. We usually connect to our clients’ systems over a VPN so the address is not simply the one we used to connect in! Similarly, because these are servers, we cannot simply open a web browser and ask Google.
$ curl ifconfig.co 2a03:9800:10:12b::2 $ curl -4 ifconfig.co 18.104.22.168