online posts 16
linux and the command line 101

task 8.2 · chapter 8 of 8: the network from the command line · 25 min

Refused

objective

srv1’s web service works when you ask it from srv1, but ws1 gets connection refused. Find out why with curl -v and ss -tlnp, fix the service’s config with sudo nano, restart it, and prove it from ws1.

you edit, in the labanswers/8.2.ymlsrv1: /etc/labweb.conf

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
LessonThe network from the command line
The servicelabweb on srv1, port 8080. Its settings: /etc/labweb.conf. Restart: sudo labweb restart
Log inssh ws1 and ssh srv1: two terminals make it easier
Your answersanswers/8.2.yml
new to this? step by stepbeginner
  1. On ws1: curl -v http://srv1:8080/. Read the line that says failed.
  2. On srv1: curl http://localhost:8080/ works. ss -tlnp: which address is port 8080 on?
  3. sudo nano /etc/labweb.conf, change BIND, then sudo labweb restart, and ss -tlnp again.
  4. On ws1: curl http://srv1:8080/ gets the page. Fill in answers/8.2.yml, then make check T=8.2.

more in meet the lab

1 explain

Is it the network, or the service?

One error, many causes

“It doesn’t work” from another machine can be DNS, a route, a firewall, the service being down, or the service not listening where you think it is. The trick is to split the problem in two: try it from the server itself first. If it works there and not from outside, the service is fine and the question is the path to it, or how it’s listening.

curl -v shows each step

curl -v prints what it’s doing, line by line, before it prints the page:

Lines startingTell you
* Host srv1:8080 was resolved / * IPv4: ...DNS worked, and the address it used
* Trying 192.168.20.10:8080...it’s opening a TCP connection
* Connected to ...TCP worked: something is listening there
> GET / HTTP/1.1what curl sent
< HTTP/1.0 200 OKwhat came back

Where those lines stop is where the problem is. Connection refused means the packet got there, and the machine answered nothing is listening on that port. That’s different from silence (a timeout), which usually means a firewall or a route.

ss: who’s listening, and where

ss -tlnp lists tCP sockets that are listening, with numbers instead of names, and the process (that last part needs sudo for other users’ processes). The Local Address:Port column says which address the service accepts connections on:

Local addressAccepts connections
127.0.0.1:8080only from the machine itself (the loopback)
192.168.20.10:8080on that one address
0.0.0.0:8080on every IPv4 address the machine has

in one sentence Try it on the server first; curl -v shows where it stops; ss -tlnp shows where a service listens.

2 show

A refusal, step by step

curl -v from ws1 to a port where nothing listens on srv1:

netops@ws1:~$ curl -v http://srv1:8081/
* Host srv1:8081 was resolved.
* IPv6: (none)
* IPv4: 192.168.20.10
*   Trying 192.168.20.10:8081...
* connect to 192.168.20.10 port 8081 from 192.168.10.10 port 57930 failed: Connection refused
* Failed to connect to srv1 port 8081 after 16 ms: Could not connect to server
* closing connection #0
curl: (7) Failed to connect to srv1 port 8081 after 16 ms: Could not connect to server

DNS worked (192.168.20.10), the route worked (the refusal came back), and the port is closed. The last line is curl’s summary: newer versions say Could not connect to server, and the -v line above it says why.

4 challenge

Make labweb answer ws1

  1. From ws1, curl -v http://srv1:8080/. Which word does the failed line end with? That’s curl_said.
  2. On srv1, try the same with curl http://localhost:8080/. Then find out which address labweb is listening on (listen_before).
  3. Fix it in /etc/labweb.conf, restart labweb, and check again with ss: that’s listen_after.
  4. From ws1, curl http://srv1:8080/ prints Hello from srv1.

Then make check T=8.2.

hint

ss -tlnp on srv1. Find :8080 in the Local Address:Port column, and read the address in front of it.

hint

/etc/labweb.conf belongs to root, so sudo nano /etc/labweb.conf. cat it first: the setting has a name that says what it is.

hint

A service reads its config when it starts, so a change does nothing until sudo labweb restart.

show the solution
netops@srv1:~$ ss -tlnp
State  Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess
LISTEN 0      32         127.0.0.1:53         0.0.0.0:*
LISTEN 0      32     192.168.20.10:53         0.0.0.0:*
LISTEN 0      5          127.0.0.1:8080       0.0.0.0:*
LISTEN 0      128          0.0.0.0:22         0.0.0.0:*
LISTEN 0      4096      127.0.0.11:38163      0.0.0.0:*
LISTEN 0      128             [::]:22            [::]:*

labweb is on 127.0.0.1: it only accepts connections from srv1 itself. (127.0.0.11 is Docker’s own DNS helper, and port 53 is srv1’s DNS server.)

sudo nano /etc/labweb.conf      # BIND=127.0.0.1  becomes  BIND=0.0.0.0
sudo labweb restart
ss -tln
curl_said: refused
listen_before: 127.0.0.1
listen_after: 0.0.0.0

BIND=192.168.20.10 works too: it listens on srv1’s network address only. This is one of the most common reasons a service works on the server and nowhere else, and it’s worth checking before you open a firewall ticket.

5 check: run it, then prove it

$ make check T=8.2

when it's right, you'll see

╭─ task 8.2 · refused
│  ✓ where labweb listened at first (challenge): ...
│  ✓ the word curl -v used for the failure (challenge): ...
│  ✓ where labweb listens now (challenge): ...
│  ✓ curl http://srv1:8080 from ws1 gets the page (challenge): 200
╰─ 4/4 checks passed  verified