What's my local internet address?
Tags: webdev, testing, beginners, productivity
Published: 2022-03-07 09:34:48

Sometimes I want to find out my internet address. So I type

`ifconfig | grep inet -w`

... exactly like you would have done, right? No?
What do we actually mean when we talk about "internet addresses" anyway?

## "What's my internet address"?

It depends? So my computer has more than one internet address?

![sketch of a computer inside a circle called local network, the world wide web circled as WAN, and a router at the intersection of both zones](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5x0xt0i9ln7jci3xttfe.png)

Most computers are not directly connected to the world wide web (*shown as wide area network or* **WAN** *on the right side of my sketch*). Connecting to a wi-fi hotspot makes my computer part of a local area network (**LAN**, *sketched on the left*), accessing the web via a router.

## Global vs. local internet addresses

So my public external internet address (IP address) is actually the same for all devices accessing the internet via the same router. It can change at any time, and usually that does not matter.

As part of the local network, my computer has another IP address, which is unique to that computer, but only unique inside this very local network.

### Historically complicated

Why the complication? When people started to build the internet as a decentralized world wide network, there were only few places that used computers, like universities, finance, and military.

It was beyond their imagination how we use computers today.
Here is a famous quote from nearly 80 years ago:

> **"I think there is a world market for maybe five computers."**
> <cite>Thomas Watson, president of IBM, 1943 ([source](https://en.wikipedia.org/wiki/Thomas_J._Watson#Famous_attribution))</cite>

In a world based on similar assumptions, scientists started to conceive and build the [foundations of the internet](https://en.wikipedia.org/wiki/History_of_the_Internet) that we use today.

Classic internet address notation, also known as [IPv4](https://en.wikipedia.org/wiki/IPv4), can "only" provide [4,294,967,296 unique public addresses](https://stackoverflow.com/questions/2437169/what-is-the-total-amount-of-public-ipv4-addresses#answer-2437185). Some of those numbers are [reserved for use in local networks](https://en.wikipedia.org/wiki/IP_address#Private_addresses).

### Local IP address blocks

* `10.0.0.0/8`
* `172.16.0.0/12`
* `192.168.0.0/16`

### localhost

* `127.0.0.1` always means `localhost` (your machine)

This means if you see an address starting with `172.` or `192.168` in the list of your current IP addresses, that's not the external one used by your router.

### Why not simply use one unique IP address for every device?

This is what [IPv6](https://en.wikipedia.org/wiki/IPv6_address) tries to do. And don't bet on IPv4 to give you privacy and anonymity on the internet. Apart from your IPv6 address, your computer has a [*mostly* --unique-- MAC address](https://www.howtogeek.com/228286/how-is-the-uniqueness-of-mac-addresses-enforced/) and there are [(super) cookies](https://www.eff.org/deeplinks/2014/11/verizon-x-uidh) and [browser fingerprinting](https://www.eff.org/deeplinks/2018/06/gdpr-and-browser-fingerprinting-how-it-changes-game-sneakiest-web-trackers). So you can be tracked and receive personal information meant only for you, otherwise it wouldn't be possible to use online banking or any other service where you are logged in as a user.

### How to find out my IP and why would I care?

There are a lot of websites that show your current public IP(v4) address. To find out one of the local addresses, type `ifonconfig | grep inet` into your console. If your shell does not have an `ifconfig` command, there might by `ipconfig` or [other ways to find out](https://www.howtogeek.com/117371/how-to-find-your-computers-private-public-ip-addresses/).

![Screen showing the result of ifconfig and a mobile phone with the matching local address](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shy2p52u1z3ppg0kgxtp.JPG)

In this screenshot, you can see two devices that I want to connect. There is a computer where I am running a web server on `localhost` to test the website that I am currently working on. Using `http://localhost:3000/` only works on the same computer, but to test on another device (like a mobile phone) or on a browser inside a virtual box, I need to replace `localhost` with the local IP address to make it work.

As [ifonfig](https://manpages.org/ifconfig) has a lot of options and output, I restrict the results to all lines showing internet ("`inet`") addresses using [grep](https://manpages.org/grep). There is still more than I need to know (and enough to get confused):

```
ifconfig | grep inet
inet 172.19.0.1  netmask 255.255.0.0  broadcast 172.19.255.255
inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
inet 127.0.0.1  netmask 255.0.0.0
inet6 ::1  prefixlen 128  scopeid 0x10<host>
inet 192.168.1.170  netmask 255.255.255.0  broadcast 192.168.1.255
inet6 2a01:c23:5dd3:6e00:e435:2db2:b694:1bc6  prefixlen 64  scopeid 0x0<global>
inet6 2a01:c23:5dd3:6e00:d8cb:4519:7308:d191  prefixlen 64  scopeid 0x0<global>
inet6 fe80::3927:9180:9b8:ccf4  prefixlen 64  scopeid 0x20<link>
```

As I'm not interested in IPv6 addresses ("`inet6`"), I can restrict the results matching only "`inet`" as a single **w**ord using `-w`.

```
ifconfig | grep inet -w
inet 172.19.0.1  netmask 255.255.0.0  broadcast 172.19.255.255
inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
inet 127.0.0.1  netmask 255.0.0.0
inet 192.168.1.170  netmask 255.255.255.0  broadcast 192.168.1.255
```

As I learned that `127.0.0.1` is always `localhost` and experience tells me that `192.168.` is a very common sub-network used by wifi routers, I'm going to assume and test the last one, `192.168.1.170`, which actually works.

Mission accomplished! But what about the `172` networks? You can tell me in the comments, as I did not find any useful information apart from the fact that all of those are local addresses and that the ones starting with `192.168` are most commonly used by modern routers.

### Privacy concerns

And there is another aspect of the interconnection. When I'm able to access my localhost from any other device in the same network, so can anybody else sitting next to me in the same café or a train? How many other users are there in a long distance train all logged in to the same network?

![Infographic about "rolling hotspots"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8r5tcbidhusjfldkqroa.png)
In their info graphic "rolling hotspots", Deutsche Bahn railway states that the on-board wi-fi services will be shared between "up to 800 users" on a single train. Even if everyone had one phone plus one laptop each, that would still mean I'm exposing my local webserver to up to 398 other people. Which might be okay for a simple website without write access, but I better use password protection if my local server allows users to write anything into a database. 

## Conclusion

Now that you know how to find out your internet addresses and access your computer from another device, be careful what software you run on your local computer. Apart from the obvious web servers started using `npm` or `docker`, there are a lot of other servers and services possibly listening for incoming connections all of the time, like WhatsApp messenger. Learning about the basics of internet networking might help you stay safe and use computers in a more conscious way in the future.