Linux Basics Lesson 34 of 38
Nothing gets through from outside
10 minutes
Learning goals
- Narrow down an unreachable service from the inside out with curl, ss and systemctl.
- Find where a service takes its listening address from.
- Bind the service to every address, restart it, and confirm it with ss.
Work from the inside out: does the app answer on the machine itself, where does it listen, where does that address come from? Then fix it.
The app runs as the service app.service under the user app and reads its configuration from /srv/app/app.conf. The team rolled it out with the account lerner, so you may change that file without sudo. Restarting the service needs sudo, and on this training machine it asks for no password.
cat, grep, kill and journalctl are not loaded on this page and answer with command not found or a hint. systemctl status shows the last lines of the journal for the service, and with -l it does not cut them off.
Steps
Step 1/4: Start on the inside. Does the app answer on the server itself? Ask it for /api/health on port 8080 of the loopback. Step 2/4: Where does the app listen? Show the listening TCP sockets. Step 3/4: Where does this address come from? Look at the service app and find out which file it reads it from. Step 4/4: Fix it. The app should listen on port 8080 on every address. Change /srv/app/app.conf and restart the service, so that ss shows 0.0.0.0:8080. Scenario solved
The new API is live, and on the server it answers. From her laptop, your colleague gets curl: (7) Failed to connect to 203.0.113.10 port 8080 after 0 ms: Could not connect to server, straight away and not after a long wait. She has already checked the name and ping, both fine. So it is on you: why does nothing get through from outside?
-
Step 1 done solved before
Start on the inside. Does the app answer on the server itself? Ask it for /api/health on port 8080 of the loopback.
Hint 1: curl takes the address and the path in one, without a scheme in front.Hint 2: Type curl 127.0.0.1:8080/api/healthSolution
curl 127.0.0.1:8080/api/healthWhy: {"status": "ok"}. The app runs and answers. So it has not crashed, and the problem lies somewhere between the app and the outside.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 2 done solved before
Where does the app listen? Show the listening TCP sockets.
Hint 1: The four letters from Lesson 32, t, l, n and p.Hint 2: Type ss -tlnpSolution
ss -tlnpWhy: 127.0.0.1:8080. The app listens on the loopback only, and no way leads there from outside. That explains the refused connection. With sudo ss -tlnp you also see that it is python3 with PID 2207, the process of the user app.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 3 done solved before
Where does this address come from? Look at the service app and find out which file it reads it from.
Hint 1: systemctl status shows the last lines that the service wrote. -l does not cut them off.Hint 2: Type systemctl status app -lSolution
systemctl status app -lWhy: The app says it itself when it starts: config /srv/app/app.conf: bind = 127.0.0.1:8080. So nothing is wrong with the network. The app does exactly what its configuration tells it.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 4 done solved before
Fix it. The app should listen on port 8080 on every address. Change /srv/app/app.conf and restart the service, so that ss shows 0.0.0.0:8080.
Hint 1: echo with > writes the new line into the file. The app reads it only when it starts, so systemctl restart follows, with sudo.Hint 2: Type echo 'bind = 0.0.0.0:8080' > /srv/app/app.conf, then sudo systemctl restart appSolution
echo 'bind = 0.0.0.0:8080' > /srv/app/app.conf sudo systemctl restart appWhy: ss -tln now shows 0.0.0.0:8080, and curl from the laptop gets an answer as well. Changing the file alone would not have been enough, because the running app keeps the address it read at its start. In production, a reverse proxy in front of an app on 127.0.0.1 is often the cleaner way.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
Scenario solved
Why each step mattered
- Step 1: {"status": "ok"}. The app runs and answers. So it has not crashed, and the problem lies somewhere between the app and the outside.
- Step 2: 127.0.0.1:8080. The app listens on the loopback only, and no way leads there from outside. That explains the refused connection. With sudo ss -tlnp you also see that it is python3 with PID 2207, the process of the user app.
- Step 3: The app says it itself when it starts: config /srv/app/app.conf: bind = 127.0.0.1:8080. So nothing is wrong with the network. The app does exactly what its configuration tells it.
- Step 4: ss -tln now shows 0.0.0.0:8080, and curl from the laptop gets an answer as well. Changing the file alone would not have been enough, because the running app keeps the address it read at its start. In production, a reverse proxy in front of an app on 127.0.0.1 is often the cleaner way.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
Loading terminal …
Esc, then Tab leaves the terminal
Step 1 of 4
Cards
-
Card 1 of 2
Tap the card or press Space to turn it over.
-
Card 2 of 2
Tap the card or press Space to turn it over.
Cheat sheet
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 19 KB)
- View as image (PNG, 66 KB)