Skip to content
Visual explainer 3h to learn 4 steps

IP addressing, routes & sockets

Read interfaces, addresses, routes and listening ports and test connectivity.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 10 minutes to read.

Topic 23 of 42

In short

Every host needs an IP address with a prefix, a default gateway and DNS servers to reach the world. The ip command shows interfaces, addresses and routes, ss shows which ports are listening and connected, and ping, tracepath and curl test reachability layer by layer.

Explained simply

Every machine on a network needs an address, just like every house on a street. An IPv4 address looks like 192.168.1.50: four numbers from 0 to 255. The /24 written after it says how much of the address names the street (the network) and how much names the house (this machine).

To reach a machine on the same network, your server sends to it directly. To reach anything else, such as the internet, it hands the packet to a router called the default gateway. The routing table is the list of rules that decides which way each packet goes.

Programs that accept connections, like a web server or SSH, listen on a port: a number from 1 to 65535 on that address. Port 22 is SSH, 80 is HTTP and 443 is HTTPS. A listening program plus its address and port is called a socket.

When something cannot connect, you test step by step: is the interface up, does it have an address, is there a route, does the far end answer, and is the service listening? The ip, ss, ping, tracepath and curl commands each answer one of those questions.

Press Play on a diagram to walk through it one step at a time.

LayersWhat to check, layer by layer
  1. 1
    Application

    curl -v, service logs: is the service answering?

  2. 2
    Transport

    ss -tulpn: is the port open and listening?

  3. 3
    Network

    ip addr, ip route, ping: addresses and routing.

  4. 4
    Link

    ip link: is the interface up with carrier?

CompareWhat a failed connection tells you
Connection refused
  • Host is reachable
  • Nothing listens on that port
  • Or it listens only on 127.0.0.1
  • Check ss -tlnp on the server
Timeout
  • Packets are silently dropped
  • Firewall drop rule or wrong route
  • Host may be down
  • Check firewall and ip route get
No route to host
  • Firewall rejected it, or
  • Neighbour did not answer ARP
  • Often a reject rule on the server
  • Check firewall-cmd --list-all

In depth

CIDR in practice: counting addresses

An IPv4 address has 32 bits. The prefix length says how many of them are the network part. /24 leaves 8 bits for hosts, so 256 addresses; the first is the network address and the last is the broadcast address, which leaves 254 usable hosts. Each bit you add to the prefix halves the size: /25 has 128 addresses (126 hosts), /26 has 64 (62 hosts), /30 has 4 (2 hosts). A /16 has 65,536 addresses.

Some ranges are special. 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 are private and not routed on the internet. 127.0.0.0/8 is loopback, the machine talking to itself. An address in 169.254.0.0/16 is link-local: a machine gives itself one when it asked for DHCP and got no answer, so seeing it usually means DHCP failed.

  • usable hosts = 2^(32 - prefix) - 2
  • /24 = 255.255.255.0, /16 = 255.255.0.0
  • ipcalc 192.168.1.50/26 shows the range (if installed)

IPv6 basics an admin needs

IPv6 addresses have 128 bits, written as eight groups of hex digits, such as 2001:db8:10:1::50. One run of zero groups can be shortened to ::, and leading zeros in a group can be dropped. ::1 is loopback.

Subnets are almost always /64: the first 64 bits are the network, the last 64 the host. Every IPv6 interface also has a link-local address starting with fe80::, used to talk to neighbours and routers on the same link; it exists even without any configuration. Global addresses usually start with 2 or 3 (2000::/3). Hosts often configure themselves with SLAAC, building an address from the prefix that routers advertise, or get one from DHCPv6.

There is no ARP in IPv6. Neighbour Discovery, carried over ICMPv6, finds MAC addresses and routers instead, so blocking all ICMPv6 breaks IPv6.

  • ip -6 addr shows IPv6 addresses
  • ip -6 route shows IPv6 routes
  • ping -6 ::1 tests the IPv6 stack

How the kernel picks a route

For every packet, the kernel looks for the most specific route that matches the destination, which is called longest prefix match. A route for 10.20.0.0/16 beats the default route 0.0.0.0/0 for 10.20.5.9, and a /24 inside it would beat both. If nothing more specific matches, the default route sends the packet to the gateway.

On the local network the kernel still needs the neighbour's MAC address. It asks with ARP and caches the answer; ip neigh shows that cache. A FAILED or INCOMPLETE entry for the gateway means the gateway itself is not answering on layer 2.

The best debugging command here is ip route get. It asks the kernel which route, interface and source address it would use for one destination, so you do not have to work it out by reading the table.

  • ip route get 8.8.8.8
  • ip neigh show dev ens160
  • ip -s link shows packet and error counters
FlowSending a packet to 10.20.5.9
  1. Step 1
    Look up routes

    10.20.0.0/16 via 192.168.1.254 is the longest match

  2. Step 2
    Pick interface and source

    ens160, source 192.168.1.50

  3. Step 3
    Find next-hop MAC

    ARP for 192.168.1.254, or use the cache

  4. Step 4
    Send the frame

    To the router's MAC, with IP destination 10.20.5.9

Reading ss and what errors mean

ss -tulpn lists TCP (-t) and UDP (-u) sockets that are listening (-l), with the owning process (-p) and numbers instead of names (-n); -p needs root to show other users' processes. The local address column matters: 0.0.0.0:80 or [::]:80 accepts connections on every address, while 127.0.0.1:5432 accepts only connections from the machine itself. That is a very common reason a database works locally but not from another server.

ss -tan shows all TCP connections with their state: ESTAB for open connections, TIME-WAIT for recently closed ones, SYN-SENT for a connection still waiting for an answer.

Client errors are clues too. Connection refused means the packet reached the host but nothing listens on that port. A timeout means packets are dropped on the way, usually by a firewall or a routing problem. No route to host often means a firewall sent a reject, or the host is down on the local network.

  • ss -tlnp 'sport = :443' filters by port
  • ss -s prints a summary of socket counts
  • curl -v --connect-timeout 5 http://host:port tests a service

Learn it in 4 short steps

About 45m each. Track them with a timer in your plan.

  1. 1
    Explain IPv4, CIDR and IPv6 basics~45m
  2. 2
    Inspect addresses and routes with ip~45m
  3. 3
    List listening ports with ss~45m
  4. 4
    Test with ping, tracepath and curl~45m

Key points

  • 192.168.1.50/24 means a 24-bit network prefix, so 254 usable hosts.
  • ip route shows the default gateway as the default via line.
  • ss -tulpn lists listening TCP and UDP sockets with their processes.
  • Changes made with ip addr add are temporary; persistent config goes through NetworkManager.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
ip -br addr
ip route
ss -tulpn
ping -c 3 192.168.1.1
tracepath example.com
curl -sI https://example.com | head -1

Check addresses, routes, open ports and reachability.

The database works locally but not from the app server

PostgreSQL on db01 answers psql on the same machine, but the app server times out or is refused. Step 1: on db01, check what address the database listens on with ss. It shows 127.0.0.1:5432, so it only accepts local connections. Step 2: change listen_addresses in postgresql.conf and restart. Step 3: check ss again: now 0.0.0.0:5432. Step 4: from the app server, test the port with curl's telnet mode or bash's /dev/tcp. If it now times out instead, the next suspect is the firewall.

bash
# on db01
sudo ss -tlnp | grep 5432
# LISTEN 0 200 127.0.0.1:5432 ... users:(("postgres",pid=812,...))
# set listen_addresses = '*' in postgresql.conf, then:
sudo systemctl restart postgresql
sudo ss -tlnp | grep 5432
# on the app server
timeout 3 bash -c '</dev/tcp/db01/5432' && echo open || echo closed

Which way does traffic to the VPN go?

A server has two interfaces, and traffic to 10.50.0.0/16 should go through the VPN router 192.168.1.254 instead of the default gateway. Step 1: ask the kernel with ip route get; it picks the default route. Step 2: add a specific route (temporary, for testing). Step 3: check again; the /16 is now the longest match. Step 4: make it permanent with NetworkManager so it survives a reboot.

bash
ip route get 10.50.3.7
# 10.50.3.7 via 192.168.1.1 dev ens160 src 192.168.1.50
sudo ip route add 10.50.0.0/16 via 192.168.1.254 dev ens160
ip route get 10.50.3.7
# 10.50.3.7 via 192.168.1.254 dev ens160 src 192.168.1.50
sudo nmcli con mod ens160 +ipv4.routes "10.50.0.0/16 192.168.1.254"
sudo nmcli con up ens160

Quick reference

  • ip -br addr and ip -br link give a one-line summary per interface
  • ip route get 8.8.8.8 shows which route, interface and source address the kernel would use
  • ip neigh shows the ARP cache; FAILED for the gateway means layer 2 trouble
  • sudo ss -tulpn lists listening TCP and UDP ports with processes
  • 127.0.0.1:PORT in ss means only local clients can connect
  • Refused: nothing listening. Timeout: dropped by a firewall or routing. Check each differently
  • /24 has 254 hosts, /25 has 126, /26 has 62, /30 has 2
  • An address in 169.254.0.0/16 means DHCP gave no answer

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1How many usable host addresses does a /26 network have?

Question 2

2ss shows a service on 127.0.0.1:8080. What does that mean?

Question 3

3A client gets Connection refused on port 443. What is the most likely cause?

Question 4

4Two routes match 10.20.5.9: 0.0.0.0/0 and 10.20.0.0/16. Which does the kernel use?

Question 5

5Which IPv6 address is link-local?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Find your server's IPv4 address with prefix, its default gateway, and which interface reaches the internet.

  2. Exercise 2medium

    List every TCP port that is listening on all addresses (not only loopback), with the process name.

  3. Exercise 3hard

    Write a short script that, given host and port, reports in order: DNS result, route used, whether the port is open within 3 seconds, and the HTTP status if it is a web port.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is the difference between ifconfig and ip?
ifconfig comes from the old net-tools package and is no longer installed by default on many distributions. ip from iproute2 is the modern replacement and shows everything, including multiple addresses and routing rules.
Why does ping fail when the website works?
Many networks and cloud providers block ICMP, the protocol ping uses. Test the real service with curl or a TCP check instead.
What is the difference between netstat and ss?
Both list sockets. ss is part of iproute2, is faster, and is the recommended tool; netstat belongs to the old net-tools package.
Why do changes made with ip disappear after a reboot?
ip changes the running kernel only. To keep settings, configure them in NetworkManager with nmcli, or in netplan on Ubuntu.

Go deeper

ShareXLinkedInWhatsApp