Skip to content

Intermediate · Software and networking · topic 21 of 57 · 35 min

Network basics: IP addresses, routes, DNS and the ip command

Explanation

A server needs four things to talk on a network: an IP address, a prefix (netmask) that says which addresses are local, a default gateway for everything else, and DNS servers to turn names into addresses. The ip command shows addresses (ip addr) and routes (ip route); ss shows which ports are listening.

The four settings every server needs

  1. IP address: the server's own address, like 192.168.10.25 (IPv4) or 2001:db8::25 (IPv6).
  2. Prefix (netmask): how much of the address is the network part. /24 (255.255.255.0) means the first three numbers name the network, so 192.168.10.1 to 192.168.10.254 are neighbours you reach directly.
  3. Default gateway: the router that forwards everything that is not local, for example 192.168.10.1.
  4. DNS servers: they turn names like repo.example.com into IP addresses.
Think of it like this: Think of a housing society. Your flat number is your IP address. The society's name and block (the prefix) tells you who your neighbours are: you can walk to them directly. For anything outside the society, you hand the parcel to the security gate (the gateway). And the directory at the gate (DNS) tells you which flat "Sharma ji" lives in.

Prefixes in practice

PrefixNetmaskAddressesUsable hosts
/24255.255.255.0256254
/25255.255.255.128128126
/26255.255.255.1926462
/16255.255.0.065,53665,534

In each IPv4 network, the first address is the network address and the last is the broadcast address, so usable hosts = addresses minus 2. Private ranges that never appear on the internet are 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Most company servers use them.

Looking at the network with ip

bash
$ ip -br addr
lo               UNKNOWN        127.0.0.1/8 ::1/128
enp1s0           UP             192.168.122.50/24 fe80::5054:ff:fe12:3456/64
$ ip addr show enp1s0
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.50/24 brd 192.168.122.255 scope global noprefixroute enp1s0
$ ip route
default via 192.168.122.1 dev enp1s0 proto static metric 100
192.168.122.0/24 dev enp1s0 proto kernel scope link src 192.168.122.50 metric 100
$ ip -s link show enp1s0     # packet counters and errors
enp1s0 is the interface (network card) name. "default via" is the gateway.

Interface names like enp1s0 or ens192 describe where the card sits in the machine, so they stay the same across reboots. lo is the loopback interface: 127.0.0.1 always means "this machine". The old ifconfig and route commands are not installed by default; use ip.

Testing connectivity, step by step

bash
$ ping -c 3 192.168.122.1          # 1. can I reach the gateway?
$ ping -c 3 8.8.8.8                # 2. can I reach outside by IP?
$ getent hosts repo.example.com    # 3. does the name resolve?
192.168.122.10  repo.example.com
$ dig +short repo.example.com      # (from bind-utils) asks DNS directly
$ tracepath 8.8.8.8                # 4. where does the path stop?
$ ss -tlnp                         # 5. which TCP ports are listening here, and by which program?
State  Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*     users:(("sshd",pid=842,fd=3))
LISTEN 0      511                *:80              *:*     users:(("httpd",pid=1301,fd=4))
Test from the inside out. Each step tells you which layer is broken.

ss options: -t TCP, -u UDP, -l listening, -n numbers instead of names, -p show the process (needs root for other users' processes). 0.0.0.0:22 means "listening on all IPv4 addresses"; 127.0.0.1:5432 would mean only this machine can connect.

Common mistakes

  • Wrong prefix: 192.168.10.25/16 instead of /24 makes the server think far-away addresses are neighbours, so it never sends them to the gateway.
  • No default gateway: the server can reach its own subnet but nothing else.
  • Blaming the network when only DNS is broken. If ping 8.8.8.8 works but ping google.com does not, look at DNS.
  • A service listening only on 127.0.0.1. Other machines cannot connect, no matter what the firewall says. Check with ss -tlnp.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Network basics: IP addresses, routes, DNS and the ip command · 1/6

IP · prefix · gateway · DNS

A server needs an IP address, a prefix, a gateway and DNS servers.

Prefer a video by a teacher? Find free videos on “linux ip command ip addr ip route ss networking basics” on YouTube ↗

In real life: The new branch office that could not reach the head office

A bank opens a branch in Nagpur. The new RHEL server can ping the branch printer but not the head office in Mumbai. ip route shows no "default via" line: the installer was given an address and prefix but no gateway. The engineer adds the gateway with nmcli (next lesson), and the head office is reachable at once.

bash
$ ip route
10.20.5.0/24 dev ens192 proto kernel scope link src 10.20.5.15 metric 100
$ ping -c1 10.1.0.10
ping: connect: Network is unreachable

Practice (0/3 done)

Task 1 · Question

ping 8.8.8.8 works but ping example.com says "Name or service not known". What is most likely broken?

Task 2 · Code · Python

Write prefix_info(prefix) for an IPv4 prefix length 0 to 32. Return a tuple (netmask, usable_hosts). netmask is dotted text like "255.255.255.0". usable_hosts is 2**(32 - prefix) - 2, except /31 gives 2 and /32 gives 1 (special point-to-point and single-host cases). Do the maths yourself, without the ipaddress module.

Task 3 · Question

ss -tlnp shows "LISTEN 127.0.0.1:5432 ... postgres". Another server cannot connect to port 5432. Why?

Quick quiz

Question 1

1.Which command shows the default gateway?

Question 2

2.How many usable hosts are in a /24 network?

Question 3

3.What does 127.0.0.1 always refer to?

Question 4

4.Which is a private IPv4 range?

Question 5

5.In ss -tlnp, what does -l mean?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Which command shows the default gateway?

Answer: ip route

The "default via" line in ip route is the gateway.

How many usable hosts are in a /24 network?

Answer: 254

256 addresses minus network and broadcast.

What does 127.0.0.1 always refer to?

Answer: This machine (loopback)

It is the loopback address.

Which is a private IPv4 range?

Answer: 10.0.0.0/8

10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 are private.

In ss -tlnp, what does -l mean?

Answer: Listening sockets only

-l shows listening sockets.

ss -tulpn shows postgres on 127.0.0.1:5432. An app on another server cannot connect. Why?Related

Answer: PostgreSQL only listens on localhost, so other machines cannot reach it

127.0.0.1 is only reachable from the same machine. It must listen on the private IP or 0.0.0.0.

From Linux networking for DevOps

What does DNS do?Related

Answer: Turns a domain name into an IP address

DNS works like a contacts list: you give it a name and it finds the IP address of the server.

From Clients, servers, IP addresses and URLs

An app server in a private subnet must download OS updates but must not accept connections from the internet. What does its route table need?Related

Answer: 0.0.0.0/0 to a NAT gateway

A NAT gateway allows outbound connections from private subnets while blocking inbound ones.

From VPC basics: CIDR, subnets and gateways

Which address is private?Related

Answer: 172.20.1.5

172.16.0.0/12 covers 172.16.x.x to 172.31.x.x, so 172.20.1.5 is private; 172.32.1.5 is not.

From IP addresses, subnets and CIDR

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 2 more topics in “Software and networking” before its exam: NetworkManager with nmcli, the hostname and name resolution, firewalld: zones, services and ports.
  2. Module 4 exam: Software and networking · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Storage: partitions, file systems and LVM. 57 topics left in the course before the final exam.

← Previous: Repositories, BaseOS, AppStream and module streams