Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 3 · Networking and hardening · Lesson 01 · 40 minutes

Inspect the network from the shell

After this lesson you can
  • Read the interfaces, addresses, routes and listening ports of your machine
  • Look up a name the way your programs do
  • Run a local web server and say who can reach it
Lab files of this lesson (1)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

A teammate started a small internal status page on a test machine and forgot about it. Your team lead asks: "Is it reachable only from this machine, or from the whole office network?" You get the page as a folder of files. You serve it twice, once on the loopback and once on every address, and you read from the shell what each choice means.

The idea

A machine has one or more network interfaces, each with an IP address. lo is the loopback interface, with the address 127.0.0.1: traffic to it never leaves the machine. A route says which gateway carries traffic to other networks. A program that listens waits on a port (a number from 1 to 65535) at an address. The course "Networking hands-on" explains addresses, ports and names in depth. This lesson only shows you how to read them from the shell.

The address that a service listens on decides who can reach it. 127.0.0.1 is private to the machine. 0.0.0.0 is open to any machine that can reach one of your addresses.

Who can reach a listening server
Office networkThis machineOther machineLoopback serverLocal programWide server1234Office networkThis machineOther machineLoopbackserverLocal programWide server1234
  1. Local program → Loopback server: 127.0.0.1:8000 answers
  2. Local program → Wide server: 0.0.0.0:8011 answers
  3. Other machine → Wide server: 192.0.2.10:8011 answers
  4. Other machine → Loopback server (blocked): 127.0.0.1 is private

A program that needs a name looks it up in a fixed order. It asks the resolver, and the resolver reads a local file first and a DNS server second:

How a program resolves a name
ProgramResolver/etc/hostsDNS serverwhere is krbox.lab?is the name in the file?noIPv4 address (A record)?192.0.2.10192.0.2.10ProgramResolver/etc/hostsDNS server123456
  1. Program → Resolver: where is krbox.lab?
  2. Resolver → /etc/hosts: is the name in the file?
  3. /etc/hosts → Resolver: no
  4. Resolver → DNS server: IPv4 address (A record)?
  5. DNS server → Resolver: 192.0.2.10
  6. Resolver → Program: 192.0.2.10

Worked example

1. Read the interfaces and addresses

ip shows and sets the network settings. ip addr lists the addresses of the interfaces, and -br (brief) gives one short line for each. ip -br link shows the same interfaces with their hardware (MAC) address.

ip -br addr
ip -br link
lo               UNKNOWN        127.0.0.1/8 ::1/128
eth0             UP             192.0.2.10/24 metric 100 fe80::1266:6aff:fe08:e01b/64
lo               UNKNOWN        00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
eth0             UP             10:66:6a:08:e0:1b <BROADCAST,MULTICAST,UP,LOWER_UP>

Your names and numbers differ. Read the eth0 line like this:

One line of ip -br addr
eth0the interfaceUPstate of the link192.0.2.10the address/24first 24 bits name the networkmetric 100priority of its routeeth0the interfaceUPstate of the link192.0.2.10the address/24first 24 bits name the networkmetric 100priority of its route

fe80::... is an IPv6 address that only works on the local link. The words in angle brackets are flags. ip -s link show lo adds counters, for example the bytes and packets sent and received and the errors (RX is received, TX is sent):

ip -s link show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    RX:  bytes packets errors dropped  missed   mcast
          7911      62      0       0       0       0
    TX:  bytes packets errors dropped carrier collsns
          7911      62      0       0       0       0

A rising errors or dropped count on a real interface points to a cable, a driver or a full queue.

2. Read the routes

ip route prints the routing table. The kernel picks the most specific line that fits the destination. ip route get ADDRESS asks which line it would use and shows the result.

ip route
ip route get 192.0.2.50
ip route get 198.51.100.7
default via 192.0.2.1 dev eth0 proto dhcp src 192.0.2.10 metric 100
192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 metric 100
192.0.2.50 dev eth0 src 192.0.2.10 uid 1000
    cache
198.51.100.7 via 192.0.2.1 dev eth0 src 192.0.2.10 uid 1000
    cache

The default route sends everything that no other line matches to the gateway 192.0.2.1. The second line says that the whole /24 network is directly on eth0. The third result shows that 192.0.2.50 is inside that network, so the machine sends the packet straight to it: there is no via. The fourth result shows that 198.51.100.7 is on another network, so the packet goes through the gateway: via 192.0.2.1.

3. Start a local test server

Make a folder with a page and a file. Then start the web server that Python has built in. --bind 127.0.0.1 keeps it on the loopback, and --directory site is the folder that it shows. > server.log 2>&1 writes its output and its errors into a log file. The & runs it in the background, and SERVER=$! saves its process number so that you can stop it.

mkdir -p ~/krlab/linux-advanced/net/site && cd ~/krlab/linux-advanced/net
echo "<h1>krlab test page</h1>" > site/index.html
echo "hello" > site/secret.txt
python3 -m http.server 8000 --bind 127.0.0.1 --directory site > server.log 2>&1 &
SERVER=$!

If the port is busy, the server stops with an error, and you can use 8001.

4. See what listens

ss (socket statistics) lists sockets. -t is TCP (the protocol of web, SSH and most services), -l is listening, -n shows numbers and not names, and -p shows the process (you see your own processes; root sees all).

ss -tlnp
State  Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0      5          127.0.0.1:8000      0.0.0.0:*    users:(("python3",pid=9309,fd=3))
LISTEN 0      4096      127.0.0.54:53        0.0.0.0:*
LISTEN 0      4096   127.0.0.53%lo:53        0.0.0.0:*
LISTEN 0      4096         0.0.0.0:22        0.0.0.0:*
LISTEN 0      4096            [::]:22           [::]:*

Your list has other lines. Look at the address column. 127.0.0.1:8000 is private to the machine. The two lines on port 53 are the local name resolver, systemd-resolved (lesson 2), and %lo binds it to the loopback. 0.0.0.0:22 is the SSH server, open to every network that reaches you, and [::]:22 is the same on IPv6. A line without a process name belongs to another user. To see one port only, give ss a filter: ss -tln 'sport = :8000'.

5. Talk to it with curl

curl makes an HTTP request and prints the answer. -s hides the progress bar, -I asks for the headers only, -o /dev/null throws the page away, and -w prints a value after the transfer.

curl -s http://127.0.0.1:8000/
curl -sI http://127.0.0.1:8000/ | head -2
curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:8000/nothing
<h1>krlab test page</h1>
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.12.3
404

200 means success and 404 means "not found". The Server: header tells every visitor what software and version you run. The log of the server shows each request:

cat server.log
127.0.0.1 - - [21/Nov/2026 11:15:34] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2026 11:15:34] "HEAD / HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2026 11:15:34] code 404, message File not found
127.0.0.1 - - [21/Nov/2026 11:15:34] "GET /nothing HTTP/1.1" 404 -

Stop the server and check that the port is closed. $? is the exit code of the last command: 0 means success.

kill $SERVER
curl -s http://127.0.0.1:8000/; echo "curl exit: $?"
curl exit: 7

Exit code 7 means "failed to connect": nothing listens any more.

6. Listen on every address

Start the same folder on 0.0.0.0, which means "every address of this machine", and look at the listener. Then ask for the page through the machine's own address. Use the address that ip -br addr showed for eth0; here it is 192.0.2.10.

python3 -m http.server 8011 --bind 0.0.0.0 --directory site > wide.log 2>&1 &
WIDE=$!
ss -tln 'sport = :8011'
curl -s -o /dev/null -w "%{http_code}\n" http://192.0.2.10:8011/
kill $WIDE
State  Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0      5            0.0.0.0:8011      0.0.0.0:*
200

This is the answer to the question of your team lead: with 0.0.0.0 every machine that can reach 192.0.2.10 reads secret.txt.

7. Look up names

A name becomes an address through the resolver. getent hosts NAME asks the same way as your programs do, so it reads /etc/hosts first. dig +short NAME sends a DNS question and prints only the answer. @127.0.0.53 names the DNS server to ask; this address is the local resolver of Ubuntu. The file /etc/resolv.conf says which DNS server the system uses.

getent hosts localhost
dig +short @127.0.0.53 localhost
head -3 /etc/hosts
grep nameserver /etc/resolv.conf
::1             localhost ip6-localhost ip6-loopback
127.0.0.1
127.0.1.1	krbox
127.0.0.1	localhost
::1		localhost ip6-localhost ip6-loopback
nameserver 127.0.0.53

dig is in the package dnsutils, which the set-up lesson installed. Lesson 2 changes these two files.

Your turn

Your teammate's status page is a folder of three files. Run this block once. It builds the data. It is not the task.

mkdir -p ~/krlab/linux-advanced/net-task/site/notes && cd ~/krlab/linux-advanced/net-task
printf 'service: ok\nversion: 4.2\n' > site/status.txt
echo "<h1>intranet</h1>" > site/index.html
echo "todo: rotate keys" > site/notes/todo.txt

There are no steps here. Serve the folder site on port 8010, only on the loopback, in the background. Then ask the server for /status.txt and for /private.txt (a file that does not exist), read the headers of /status.txt, and look at the listening sockets. Answer from your own terminal.

Stop the server and check that the port is closed. Then serve the same folder on port 8012 on 0.0.0.0 and read the listener again.

Stop the second server too.

Check yourself

ss shows "0.0.0.0:8000" for your test server. What does that change?

Any machine that can reach you can now connect and read every file in the folder. 127.0.0.1:8000 was only for your own machine. The address in the listing is the first thing to check.

ip route get shows "via 192.0.2.1" for one address and no "via" for another. What is the difference?

A line without via means that the destination is on a network that is directly on the interface: the machine sends the packet straight to it. A line with via sends the packet to a gateway, which carries it on.

getent finds a name but dig does not. Where can the name come from?

From /etc/hosts. getent follows the order of the system, with the local file first. dig asks a DNS server only, and ignores the file.

A listener shows "[::]:8000" and not "0.0.0.0:8000". Is it still open to the network?

Yes, on IPv6, and on most systems also on IPv4. [::] means every IPv6 address. Check both families before you say that a service is closed.

Field notes

  • Check both IPv4 and IPv6. A service can be closed on 0.0.0.0 and open on [::]. ss -tln shows both.
  • A listening port is not an open port. A firewall can block it (lesson 3). To know what the outside sees, test from another machine.
  • ss -tlnp shows the process of other users only for root. Run it with sudo when you audit a system.
  • The old tools ifconfig, route and netstat come from the package net-tools. They are no longer installed by default. ip and ss replace them.
  • The Server: header and the error pages leak the software and its version. Lesson 8 hides them for Apache.