Intermediate · Software and networking · topic 21 of 57 · 35 min
Network basics: IP addresses, routes, DNS and the ip command
Explanation
The four settings every server needs
- IP address: the server's own address, like 192.168.10.25 (IPv4) or 2001:db8::25 (IPv6).
- 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.
- Default gateway: the router that forwards everything that is not local, for example 192.168.10.1.
- DNS servers: they turn names like repo.example.com into IP addresses.
Prefixes in practice
| Prefix | Netmask | Addresses | Usable hosts |
|---|---|---|---|
| /24 | 255.255.255.0 | 256 | 254 |
| /25 | 255.255.255.128 | 128 | 126 |
| /26 | 255.255.255.192 | 64 | 62 |
| /16 | 255.255.0.0 | 65,536 | 65,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
$ 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 errorsInterface 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
$ 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))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
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.
$ 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 unreachablePractice (0/3 done)
ping 8.8.8.8 works but ping example.com says "Name or service not known". What is most likely broken?
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.
ss -tlnp shows "LISTEN 127.0.0.1:5432 ... postgres". Another server cannot connect to port 5432. Why?
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- NetworkManager with nmcli, the hostname and name resolutionSoftware and networking · same module
- Installing software with dnf and rpmSoftware and networking · same module
- Linux networking for DevOpsDevOps: Git, Docker, Kubernetes and CI/CD · Linux for DevOps
- Clients, servers, IP addresses and URLsBackend Development · How the web and backends work
- VPC basics: CIDR, subnets and gatewaysCloud Computing (AWS, Azure, Google Cloud) · Networking and databases: VPC and RDS
- IP addresses, subnets and CIDRCyber Security: from zero to SOC analyst · Networking for security
Questions people ask about this
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.
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.
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.
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.
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
- 2 more topics in “Software and networking” before its exam: NetworkManager with nmcli, the hostname and name resolution, firewalld: zones, services and ports.
- Module 4 exam: Software and networking · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- 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