Linux Basics Lesson 32 of 38
Who listens, and what answers
9 minutes
Learning goals
- Read ss -tlnp and say which program listens on which address and port.
- Know why the process column needs sudo for other users' programs.
- Fetch a page with curl, show only the headers with -I, and read the status code.
ss: who listens where
ss shows the sockets of the machine. Four letters make up the line you will type most: t for TCP, l for listening, n for numbers instead of names, p for the process:
ss -tlnp
Purpose: Shows which program listens on which address and port.
Syntax: ss -tlnp | ss -tuln
Options:
Example:
ss -tlnp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 0.0.0.0:22 0.0.0.0:*
LISTEN 0 4096 0.0.0.0:80 0.0.0.0:*
LISTEN 0 4096 127.0.0.1:8080 0.0.0.0:* users:(("python3",pid=2114,fd=3))Three programs wait for connections. The column Local Address:Port is the one to read. 0.0.0.0:22 means port 22 on every address of the machine, so SSH can be reached from outside. 127.0.0.1:8080 means port 8080 on the loopback only.
The column Process shows a name only for your own processes. python3 app.py belongs to lerner, so you see it. sshd and nginx belong to root, and their column stays empty. With sudo you see all of them:
sudo ss -tlnp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=612,fd=3))
LISTEN 0 4096 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=701,fd=3))
LISTEN 0 4096 127.0.0.1:8080 0.0.0.0:* users:(("python3",pid=2114,fd=3))
On this training machine sudo asks for no password. On a real server it asks for yours.
ss -tuln adds UDP and leaves out the processes, a quick overview of everything that is open:
ss -tuln
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 4096 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 4096 0.0.0.0:80 0.0.0.0:*
tcp LISTEN 0 4096 127.0.0.1:8080 0.0.0.0:*
In older guides you will find netstat -tlnp. It belongs to net-tools, like ifconfig, and new Debian, Ubuntu and RHEL systems no longer install it. ss takes the same letters. On a real server you would often narrow the list down with ss -tlnp | grep 8080. grep, cat and kill are not loaded on this page and answer with command not found, and the list here is short enough to read.
Terminal task 1/3
Mission
Find out with ss which process listens on port 8080, and on which address.
Task 1/3: Find out with ss which process listens on port 8080, and on which address.
Mission
Find out with ss which process listens on port 8080, and on which address.
Task complete
Solution
ss -tlnp Why: python3 app.py, PID 2114, on 127.0.0.1:8080. It listens, but only on the loopback, so nobody outside the machine can reach it.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
A port that listens is not yet a port that can be reached. A firewall can still stand in between, ufw on Debian and Ubuntu, firewalld on RHEL and Rocky Linux. That comes later in the course. For now, ss tells you what the machine itself offers.
curl: what answers
curl fetches a web address from the shell, without a browser, and prints the body of the answer. Without a scheme in front of the address, curl uses HTTP, and -s hides the progress meter:
curl -s shop.example.net/
Purpose: Fetches a web address from the shell and shows the answer.
Syntax: curl [-I] [-s] [-o FILE] URL
Options:
Example:
curl -s shop.example.net/
<h1>shop</h1>The page came back, so the server answers. How it answered is in the status code. curl -I shop.example.net/ shows only the status line and the headers, and the status line reads HTTP/1.1 200 OK. Try it in the terminal. 2xx means it worked, 3xx sends you elsewhere, 4xx means the request was wrong, for example 404 for a page that does not exist, and 5xx means the server has a problem.
Scripts usually want the number alone. -o writes the body into a file, and -w prints only what you ask for, here the status code:
curl -s -o page.html -w '%{http_code}\n' shop.example.net/missing
404
404: the server is there, but the page is not. That is something different from a server that does not answer at all.
Practice
Terminal task 2/3
The process column is empty for port 22 and port 80. Show the listening TCP sockets with the processes of every user.
Task 2/3: The process column is empty for port 22 and port 80. Show the listening TCP sockets with the processes of every user.
The process column is empty for port 22 and port 80. Show the listening TCP sockets with the processes of every user.
Task complete
Solution
sudo ss -tlnp Why: sshd with PID 612 on port 22, nginx with PID 701 on port 80. Reading needs root here because the processes are not yours, and on a real server sudo would ask for your password first.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 3/3
Does shop.example.net/missing exist? Find the status code of the answer.
Task 3/3: Does shop.example.net/missing exist? Find the status code of the answer.
Does shop.example.net/missing exist? Find the status code of the answer.
Task complete
Solution
curl -I shop.example.net/missing Why: 404 Not Found. The server is there and answers, but there is no page under this path. A 4xx is never a sign that the server is down.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Fill the gap
ss shows 0.0.0.0:22. That means: port 22 on addresses of the machine.
ss shows 0.0.0.0:22. That means: port 22 on … addresses of the machine.
Check works once you have typed an answer.
Predict the output
Command
curl -s -o /dev/null -w '%{http_code}\n' shop.example.net/Before the command, in your home folder ~
- ~/.bashrc
- ~/.profile
- ~/.secret
- ~/Documents/
- ~/logs/
- ~/notes.txt
- ~/projects/
- ~/archive/
Check works once you have chosen an answer.
Next up: names, and the other machines.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 3 quiz questions
What you can do now: ss -tlnp, curl -s shop.example.net/
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: done
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
ss -tlnplistening TCP sockets with your processes -
sudo ss -tlnpthe same with the processes of every user -
ss -tulnTCP and UDP, everything that is open -
curl -I <url>status line and headers only -
curl -s -o <file> <url>body into a file, no progress meter
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)