Sub topic 2
Inspecting what the server is using now
Before you change a server's network settings, record what it is using now. You need that record for two reasons. It shows you what to change, and it gives you something to compare against if the change goes wrong. This chapter covers the read-only commands for addresses, routes, the hostname, and DNS resolver settings, and how to read their output.
This chapter assumes you can use a Linux shell with sudo, and that you know what an IPv4 address, a prefix length, and a default gateway are. It does not assume you know which tool configures the network. That is the next chapter.
Why inspect first
A server's network state has two layers:
- Running state is what the kernel and the resolver are using right now. The commands in this chapter show it.
- Configured state is what the configuration files say the server should use. The next chapters cover it.
The two layers can disagree. A file may say static while the interface still holds a DHCP lease. A file may have a typo that was never applied. A temporary ip addr add may exist that no file mentions. Always check the running state first. It is the state that decides whether your SSH session works.
Collecting the baseline
Run these four commands together and save the output.
ip -br address show
ip route show default
hostnamectl
resolvectl statusTo keep a copy for later comparison, redirect the output to a file in your home directory:
{ ip -br address show; ip route show default; hostnamectl; resolvectl status; } > ~/network-before.txt 2>&1None of these commands change anything, and none needs root. The sections below take them one at a time.
Addresses and links
ip -br address show prints one line per interface. The -br flag stands for brief.
lo UNKNOWN 127.0.0.1/8 ::1/128
enp1s0 UP 192.168.1.23/24 fe80::5054:ff:fe12:3456/64The columns are the interface name, its operational state, and its addresses.
lois the loopback interface. It always has127.0.0.1/8. Ignore it when you look for the server's real address.enp1s0is an Ethernet interface. The name is predictable, which means udev derives it from the hardware location (here PCI bus 1, slot 0). Older systems may showeth0.UPmeans the link is active.DOWNmeans the interface is disabled or has no cable or carrier.UNKNOWNis normal forlo.192.168.1.23/24is an IPv4 address followed by its prefix length. A/24means the first 24 bits identify the network, so the netmask is255.255.255.0. Addresses from192.168.1.1to192.168.1.254are on the same link, and the kernel sends traffic to them directly.fe80::...is an IPv6 link-local address. It exists on nearly every interface and is not a sign of a configured IPv6 network.
To see only IPv4, use ip -4 -br address show. To look at one interface, add dev enp1s0.
Spotting a DHCP address
The brief view hides how an address was obtained. The full view shows more:
ip address show dev enp1s02: 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.1.23/24 metric 100 brd 192.168.1.255 scope global dynamic enp1s0
valid_lft 86212sec preferred_lft 86212secLook at the word dynamic and at valid_lft. A dynamic address came from a DHCP lease, and valid_lft counts down the seconds until the lease must be renewed. A static address shows valid_lft forever. This is the quickest way to tell which kind of address an interface has before you open any configuration file.
An interface can hold several addresses. A secondary address added for a service appears as an extra inet line. Check for these before you assume which address clients use.
The flags in angle brackets
The flags on the second line of the full view tell you about the link itself:
| Flag | Meaning |
|---|---|
| `UP` | The interface is administratively enabled. |
| `LOWER_UP` | The physical link has a carrier, such as a cable or a virtual link that works. |
| `NO-CARRIER` | The interface is enabled but nothing is connected or the link is down. |
An interface with UP but not LOWER_UP points to a cable, switch port, or hypervisor problem, not a configuration problem.
Routes and the default gateway
An address alone does not let the server reach other networks. For that it needs a route. Show the default route with:
ip route show defaultdefault via 192.168.1.1 dev enp1s0 proto dhcp src 192.168.1.23 metric 100Read it from left to right:
defaultmatches any destination that no more specific route matches.via 192.168.1.1is the default gateway. The server sends packets for other networks to this address, which must be on the local subnet.dev enp1s0is the interface used to reach the gateway.proto dhcpshows where the route came from. A route written by a static configuration showsproto static. Routes the kernel creates itself showproto kernel.src 192.168.1.23is the source address the server uses when it sends through this route.metric 100is the route's priority. When two default routes exist, the lower metric wins.
If this command prints nothing, the server has no default route. It can still talk to its own subnet, but it cannot reach anything beyond it. That is a common cause of "the server can ping neighbours but not the internet."
To see the whole routing table, drop the word default:
ip route showdefault via 192.168.1.1 dev enp1s0 proto dhcp src 192.168.1.23 metric 100
192.168.1.0/24 dev enp1s0 proto kernel scope link src 192.168.1.23 metric 100The second line is the connected route. The kernel creates it automatically from the address and prefix length. This is why a wrong prefix length matters. If you set /16 where the network is really /24, the kernel believes a much larger range is directly reachable. It will try to reach those addresses without the gateway and fail.
You can also ask the kernel which route it would use for a given destination. This is covered further when you verify the configuration, but it is useful during inspection too:
ip route get 9.9.9.9The hostname
hostnamectl prints the hostname and some facts about the machine:
Static hostname: web01
Icon name: computer-vm
Chassis: vm
Machine ID: 4f2c1e0a7b8d4c3e9a1b2c3d4e5f6a7b
Boot ID: 9d8c7b6a5f4e3d2c1b0a9f8e7d6c5b4a
Virtualization: kvm
Operating System: Ubuntu 24.04 LTS
Kernel: Linux 6.8.0-45-generic
Architecture: x86-64A machine can have up to three hostnames:
- Static hostname is the name stored in
/etc/hostname. It is the one that survives a reboot, and the one you set later in this tutorial. - Transient hostname is a name handed out at runtime, for example by DHCP. It appears as an extra
Transient hostname:line only when it differs from the static one. - Pretty hostname is a free-form label for display, which servers rarely use.
If the Static hostname line shows localhost or n/a, no name has been set. If a transient hostname is present, DHCP or another service is overriding the name on the running system, which can surprise you after a reboot.
To print just the static name, run hostnamectl --static. The short commands hostname and hostname -f also exist. The second one prints the fully qualified name, and it depends on /etc/hosts and the resolver. If hostname -f fails or prints only a short name, the hostname and /etc/hosts are not consistent. The chapter on the hostname and /etc/hosts fixes that.
The resolver
resolvectl status shows what the systemd-resolved service is using for DNS. On systems that run it, such as current Ubuntu, this is the authoritative view.
Global
Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub
Link 2 (enp1s0)
Current Scopes: DNS
Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 192.168.1.1
DNS Servers: 192.168.1.1 9.9.9.9
DNS Domain: example.comThe output has a Global section and one section per link. DNS settings are mostly per link. The fields to read are:
DNS Serverslists the servers the link can query, in order.Current DNS Serveris the one in use now.DNS Domainlists the search domain. Withexample.comhere, a lookup fordb01also triesdb01.example.com.resolv.conf modeshows how/etc/resolv.confrelates to the service.stubmeans the file points applications to a local listener at127.0.0.53, and the service forwards the queries.
Because of the stub, cat /etc/resolv.conf on such a system shows nameserver 127.0.0.53 and not your real DNS servers. That is expected, not a misconfiguration. Use resolvectl status to see the real servers.
Reading the four outputs together
The four outputs describe one picture. Check that they agree:
- The address and prefix from
ip addressshould match the subnet in which the gateway sits. - The gateway in
ip routeshould be inside that subnet. - The DNS servers from
resolvectlshould be reachable through the route. A server on the local subnet is reached directly. Any other is reached through the gateway. - The hostname from
hostnamectlshould be the name you expect other systems to use.
For example, an address of 192.168.1.23/24 with a gateway of 192.168.2.1 is inconsistent. The gateway is outside the subnet, so the route cannot work.
Machine-readable output
Scripts should not parse the human-readable columns. The ip command can print JSON with -j. With jq installed, you can extract a field:
ip -j -4 address show dev enp1s0 | jq -r '.[0].addr_info[0].local'This prints only the IPv4 address of enp1s0. JSON output does not change when the display format changes, so scripts that use it break less often.
Exercise
On your server, ip -br address show lists enp1s0 UP 10.0.5.40/24, and ip route show default prints default via 10.0.6.1 dev enp1s0 proto static. The server can reach other hosts at 10.0.5.x but nothing else. What is wrong, and which output proves it?
Show answer
The default gateway 10.0.6.1 is outside the interface's subnet 10.0.5.0/24. The kernel can only send packets directly to hosts on the connected subnet, so it cannot reach a gateway on a different network. The two outputs together show it: the address line gives the subnet 10.0.5.0/24, and the route line gives a gateway in 10.0.6.x. The fix is a gateway inside 10.0.5.0/24, such as 10.0.5.1, or a correct prefix length if the network really is larger.
Exercise
cat /etc/resolv.conf on an Ubuntu server shows only nameserver 127.0.0.53. Where do you find the DNS servers that are really in use, and how can you tell whether an address was assigned by DHCP?
Show answer
Run resolvectl status and read the DNS Servers lines under the interface. The address 127.0.0.53 is the local stub listener of systemd-resolved, which forwards queries to those servers. For the address, run ip address show dev <interface>. A DHCP address is marked dynamic and has a finite valid_lft, while a static one shows valid_lft forever. The route line also shows proto dhcp when the gateway came from DHCP.