Skip to lesson

IP addresses, ports and DNS

A customer types shoplite.in and the page appears in a second. Network messages travel between numbers, not names. Which number is the computer, which program on it answers, and who turns the name into the number?

IP addresses: IPv4, IPv6, public and private

An IP address is the number that identifies a computer on a network, like a postal address for messages. IPv4 uses four numbers from 0 to 255; IPv6 is much longer.

The first example prints your own addresses, then pings the loopback address.

Example 1 · Your own addresses, and the loopback address

Terminal
ipconfigping -n 2 127.0.0.1
Sample output (Windows, Wi-Fi part only; your addresses differ)
Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . :
   IPv6 Address. . . . . . . . . . . : 2001:db8:61:a6f3:6401:dc59:a830:2db4
   Temporary IPv6 Address. . . . . . : 2001:db8:61:a6f3:41b7:2d53:6d49:3aeb
   Link-local IPv6 Address . . . . . : fe80::3565:30ec:3ab6:e349%22
   IPv4 Address. . . . . . . . . . . : 192.168.0.107
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : fe80::2a87:baff:fe00:d565%22
                                       192.168.0.1

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

What this code does

  • 192.168.0.107 is a private IPv4 address: it works only inside this home network. The default gateway 192.168.0.1 is the Wi-Fi router, which reaches the internet for every device behind it.
  • The IPv6 addresses here are global (public) addresses, though most routers block connections coming in from the internet. (They are shown with the documentation prefix 2001:db8, which is reserved for examples.) fe80:: addresses are link-local: they work only on the local network.
  • ping sends a tiny "are you there?" message and times the reply. 127.0.0.1 answers in under 1 ms because the message never leaves the laptop. On macOS use ping -c 2 127.0.0.1.

An IPv4 address such as 104.20.23.154 has only about 4.3 billion possible values. An IPv6 address such as 2606:4700:10::6814:179a is written as up to eight groups of hex digits, and :: stands for a run of zeros. A public address can be reached from the internet; private addresses (192.168.x.x, 10.x.x.x and 172.16.x.x to 172.31.x.x) work only inside one home or college network, and a router lets every device behind it share one public address. 127.0.0.1 (IPv4) and ::1 (IPv6) are loopback addresses: they always mean "this computer", exactly like the name localhost.

On Windows run ipconfig and look under your Wi-Fi adapter; on macOS run ipconfig getifaddr en0, or open System Settings → Wi-Fi → Details. Windows ping -n 2 sends two messages; on macOS use ping -c 2, or it runs until Ctrl+C.

Ports: one address, many services

A port is a number from 0 to 65535 that picks one server program on a computer. An address and port together are written address:port, such as 104.20.23.154:443.

The next example reaches the same server on port 80 and on port 443.

Example 2 · Same server, port 80 or port 443

Terminal
curl -sv http://example.com -o page.htmlcurl -sv https://example.com -o page.html
Sample output (only the lines about address and port)
* Host example.com:80 was resolved.
*   Trying [2606:4700:10::ac42:93f3]:80...
> GET / HTTP/1.1
< HTTP/1.1 200 OK
...
* Host example.com:443 was resolved.
*   Trying [2606:4700:10::ac42:93f3]:443...
> GET / HTTP/1.1
< HTTP/1.1 200 OK

What this code does

  • Both requests go to the same IPv6 address. Only the port changes: http:// means port 80 and https:// means port 443, so you never had to type a port.
  • IPv6 addresses are written in square brackets before :port, as in [2606:4700:10::ac42:93f3]:443, because the address itself is full of colons. An IPv4 address is written plainly: 104.20.23.154:443.

The IP address gets the message to the right computer; the port gets it to the right program on it. Some ports are standard, so clients know them without being told:

  • 80: web servers without encryption (http://).
  • 443: web servers with encryption (https://), the normal case today.
  • 3306: MySQL databases.
  • 8080 and 8000: common choices for servers you run while developing.
Only one program can wait on a given port at a time. Starting a second server on a port that is already taken fails with an "address already in use" error.

DNS: from a name to an address

DNS (Domain Name System) is the internet's phone book: it turns a name like example.com into IP addresses.

The example asks DNS for the addresses of example.com, first through the default resolver and then through Google's.

Example 3 · Ask DNS for an address

Terminal
nslookup example.comnslookup example.com 8.8.8.8
Sample output (Windows)
Server:  UnKnown
Address:  2406:7400:a:10::2

Non-authoritative answer:
Name:    example.com
Addresses:  2606:4700:10::6814:179a
	  2606:4700:10::ac42:93f3
	  104.20.23.154
	  172.66.147.243

Server:  dns.google
Address:  8.8.8.8

Non-authoritative answer:
Name:    example.com
Addresses:  2606:4700:10::ac42:93f3
	  2606:4700:10::6814:179a
	  172.66.147.243
	  104.20.23.154

What this code does

  • Server and Address at the top name the resolver that answered: first the internet provider's resolver (it has no name, so Windows prints UnKnown), then Google's public resolver because the second command named 8.8.8.8.
  • Non-authoritative answer means the answer came from a resolver (often straight from its cache), not from example.com's own DNS servers. That is normal and fast.
  • One name has four addresses: two IPv6 and two IPv4. Big sites use several servers, and a client may use any of them. The order can change between lookups.

Your computer asks a resolver, a DNS server chosen by your router or internet provider (or a public one such as Google's 8.8.8.8). The resolver finds the answer by asking other DNS servers, ending with the name's own servers, called authoritative servers.

Looking up every name every time would be slow, so answers are kept in a cache by the resolver, your operating system and your browser. Each answer carries a TTL (time to live): how many seconds it may be cached. When a company moves its server to a new address, it changes one record and clients follow once the old TTL runs out.

Looking up a name: nslookup and dig

A resolver is the DNS server your computer asks first. nslookup and dig ask it, or any server you name, for the addresses of a name.

This example asks for an address with dig and shows the answer together with its TTL.

Example 4 · dig on macOS: the answer and its TTL

Terminal
dig example.comdig +noall +answer github.comdig +noall +answer github.com
Sample output
; <<>> DiG 9.18.50 <<>> example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55534
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;example.com.			IN	A

;; ANSWER SECTION:
example.com.		98	IN	A	104.20.23.154
example.com.		98	IN	A	172.66.147.243

;; Query time: 200 msec
;; SERVER: 192.168.65.7#53(192.168.65.7) (UDP)
;; WHEN: Fri Oct 16 02:23:37 UTC 2026
;; MSG SIZE  rcvd: 83

github.com.		53	IN	A	20.207.73.82
github.com.		46	IN	A	20.207.73.82

What this code does

  • In the ANSWER SECTION each line is one record: the name, the TTL in seconds (98), IN A (an IPv4 address record) and the address. status: NOERROR means the name exists; a missing name gives NXDOMAIN.
  • The last two lines ran about 7 seconds apart. The TTL fell from 53 to 46: the resolver is counting down its cached copy. When it reaches 0, the resolver looks the name up again.
  • SERVER is the resolver that answered (port 53 is the DNS port). On your Mac it is usually your router, such as 192.168.1.1.

nslookup is built into Windows and macOS: nslookup name, or nslookup name 8.8.8.8 to ask a particular server. dig gives more detail and comes with macOS and Linux but not with Windows: dig name, dig +short name for just the addresses, and dig +noall +answer name for the answer lines with their TTL. On Windows, nslookup is the simple choice.

Both tools ask the DNS server directly, so they skip your computer's own cache and hosts file. That makes them good for checking "is the name the problem?".

The hosts file, and two errors that look alike

The hosts file is a small text file on your computer that maps names to addresses by hand. Your computer checks it before asking DNS.

The first example reads the hosts file; the second compares two errors that look alike.

Example 5 · The hosts file and localhost

Terminal
# Windows: show the localhost lines of the hosts filefindstr localhost C:\Windows\System32\drivers\etc\hostsnslookup localhostping -n 1 localhost
Sample output (Windows; the computer name will be yours)
# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
Server:  UnKnown
Address:  2406:7400:a:10::2

*** UnKnown can't find localhost: Non-existent domain

Pinging ASHA-LAPTOP [::1] with 32 bytes of data:
Reply from ::1: time<1ms

Ping statistics for ::1:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

What this code does

  • Lines starting with # in the hosts file are comments. On Windows, localhost is built into the operating system; on macOS, /etc/hosts holds the real lines 127.0.0.1 localhost and ::1 localhost.
  • nslookup localhost fails because nslookup asks the DNS server directly, and DNS servers do not know your localhost. ping localhost works because it uses the computer's normal lookup: hosts file and built-in names first, then DNS.
  • Lesson: when nslookup and the browser disagree about a name, check the hosts file and the caches, which nslookup skips.

Example 6 · Could not resolve host versus connection refused

Terminal
curl https://shoplite-no-such-host.invalid/curl http://localhost:8000/
Sample output (the time in ms will differ)
curl: (6) Could not resolve host: shoplite-no-such-host.invalid
curl: (7) Failed to connect to localhost:8000 after 2248 ms: Could not connect to server

What this code does

  • Error 6: DNS found no address for the name, so curl stopped before connecting to anything. The fix is in the name: spelling, internet connection or DNS settings.
  • Error 7: localhost was resolved to an address, but nothing was waiting on port 8000, so the computer refused the connection. The fix is on the server side: start it, or use the right port.
  • .invalid is a name ending reserved so that it never exists, which makes it safe for this demo.

On Windows the file is C:\Windows\System32\drivers\etc\hosts; on macOS it is /etc/hosts. Editing it needs administrator rights. Developers sometimes add a line such as 127.0.0.1 shop.test so that a friendly name points at their own laptop.

Two errors in curl and browsers look alike but fail at different steps:

  • Could not resolve host (curl error 6; Chrome: DNS_PROBE_FINISHED_NXDOMAIN): the name step failed. No address was found, so no connection was even tried. Check the spelling, your internet, then try nslookup.
  • Connection refused (curl error 7, "Could not connect to server"; Chrome: ERR_CONNECTION_REFUSED): the name worked and the computer was reached, but nothing waits on that port. Check that the server runs and the port is right.

Predict the output

Which step failed?

Read the curl error and name the step that broke.

Round 1 of 2

Which step failed?

$ curl https://shoplite-no-such-host.invalid/
curl: (6) Could not resolve host: shoplite-no-such-host.invalid
Changed a DNS record or the hosts file and still see the old address? A cache still holds the old answer until its TTL runs out. Closing and reopening the browser usually clears the browser's copy.

Next step

Check your understanding

12 quick questions